Hi! First of all, I’m half-way done with my startup’s first mobile app using Expo and I’m loving it. Thanks for your great work!
Today I’ve been trying to implement push notifications; the app is successfully sending its push token to the server, but then when the server tries to send a message I get an error I don’t understand and I don’t know how to debug.
Client-side, for now, I’m just using the same code in the documentation.
Server-side, I save the tokens and send a message every X seconds:
This saves the tokens, verifying they are well-formed ExpoPushTokens:
// Save the tokens
const tokensSet = new Set([])
const registerToken = token => {
if (!Expo.isExpoPushToken(token)) {
console.error(`Push token ${token} is not a valid Expo push token`);
}
else if (!tokensSet.has(token)) {
console.log(`- New token: ${token}`)
tokensSet.add(token)
}
}
app.post("/api2/push", (req, res) => {
if (req.body.token)
registerToken(req.body.token)
res.sendStatus(501)
})
And here is the function that fails. It produces Messages just like I saw in exponent-server-sdk-node’s documentation, creates a chunk and sends it:
// Write a message for all tokens
const writeToAll = async msg => {
const tokensArray = Array.from(tokensSet)
if (tokensArray.length > 0) {
const messages = tokensArray.map(token => ({
to: token,
sound: 'default',
body: msg,
data: { msg },
}))
console.log(`writing to ${tokensArray.length} users`)
console.log(messages)
const chunk = expo.chunkPushNotifications()
// try {
const receipts = await expo.sendPushNotificationsAsync(chunk)
console.log(receipts)
//}
// catch (e) {
// console.error(`- error sending the chunk :( ${e}`)
// }
}
else {
console.log(`cant write, ${tokensArray.length} users`)
}
return tokensArray.length
}
I’m just running this function every 30s:
setInterval(async () => { writeToAll(`Hey !!`) }, 30*1000)
Finally, I’m getting this cryptic error:
(node:79055) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 15): TypeError: Cannot read property 'Symbol(Symbol.iterator)' of undefined
The log shows apparently-sane info:
- New token: ExponentPushToken[REDACTED]
::1 - - [15/Feb/2018:16:15:50 +0000] UNKNOWN "POST /api2/push HTTP/1.1" 200 2
...
writing to 1 users
true
[ { to: 'ExponentPushToken[REDACTED]',
sound: 'default',
body: 'Hey !!',
data: { msg: 'Hey !!' } } ]
I have no idea what to do, and I don’t dare to create an issue in Github since probably it’s my fault. Is there anything I’m misunderstanding here?
By the way, my app uses:
"expo": "^25.0.0",
"react": "16.2.0",
"react-native": "0.52.0",
The server uses expo-server-sdk
version 2.3.3, and node 8.7.
Am I doing anything wrong? Can anyone make sense of this error? I’ve been reading expo-server-sdk’s code and nothing has popped to my eye…
Thanks a lot!