ToothlessGear / node-gcm

A NodeJS wrapper library port to send data to Android devices via Google Cloud Messaging
https://github.com/ToothlessGear/node-gcm
Other
1.3k stars 206 forks source link

Removing no longer valid FCM tokens #360

Closed ghost closed 2 years ago

ghost commented 2 years ago

In the example given in the Readme, it filters the tokens based on response.results[index].error != null in order to delete those. But not all errored tokens should be deleted. Some might have failed once but could still be valid for later ?

Is there a list of possible options for the field error ?

    sender.send(gcmMessage, { registrationTokens: fcmTokens }, (error, response) => {
      if (error) {
        console.log(error);
      } else {
        const failedTokens = fcmTokens.filter((_, index) => response.results[index].error != null);
        if (failedTokens.length) {
          // If there are failed tokens, check these to know whether we should delete them.

          this.clearUserFcmTokens(userID, failedTokens);
        }
      }
    });
eladnava commented 2 years ago

Hi @charlesrostaing, Thanks so much for bringing this to our attention!

The README code sample on how to detect & remove expired / unregistered device tokens could be improved by ensuring we only flag device tokens which FCM labels NotRegistered as unregistered.

I've gone forth and updated the code sample in the README in commit https://github.com/ToothlessGear/node-gcm/commit/924a4f8c2117d1bb21e0b2b7ef05878f556d3e98:

var failed_tokens = registrationTokens.filter((token, i) => response.results[i].error === 'NotRegistered');

This is the only error code for which FCM recommends removing the token from the app server, and avoiding sending any additional notifications to it.

For a full list of possible FCM errors, please review the FCM downstream message error response codes: https://firebase.google.com/docs/cloud-messaging/http-server-ref#error-codes