How to properly handle push notification errors

I am not certain as to what the proper way to handle push notification errors.

Lets say I have a user “user1”, who has 3 devices each with a unique push token stored on the backend. Later user1 uninstalls the app from one of his devices.

When it is time to send a notification to user1, the developer queries the backend for user1’s push tokens and receives 3 push tokens (one of these push tokens is invalid because user1 has since uninstalled the app on one of his devices). Using the expo node sdk, we send a message like so

    let message = {
        to: pushTokens,
        message: 'Hello!',
        data: {
            userId
        }
    }

    const res = await expo.sendPushNotificationsAsync([message]);
    const receiptIds = res.filter(r => r.status === 'ok').map(r => r.id);

    const receipts = await expo.getPushNotificationReceiptsAsync(receiptIds);

Now one of the receipts indicates that the push notification failed

{“data”:{“id”:“e3bd4ac4-026d-42fc-be93-709f690f722a”,“status”:“error”,“message”:“The recipient device is not registered with FCM.”,“details”:{“error”:“DeviceNotRegistered”,“fault”:“developer”}}}

By parsing the error, I now know that one of the push tokens is no longer valid, because there is no device associated with that token (“DeviceNotRegistered”). My question is, how do I know which is the invalid token so that I can remove it from my data store?? I feel that the returned error should also include the associated pushToken.

2 Likes

I want know this too.
Is any solution?

I can’t use getReceipts cause breaking order. And tickets may not contains all messages due errors.
for (let chunk of chunks) {
try {
let ticketChunk = await expo.sendPushNotificationsAsync(chunk);
console.log(ticketChunk);
tickets.push(…ticketChunk);
} catch (error) {
// Skip push tickets
console.error(error);
}
}