FCM push notifications for Android have stopped working - need help problem solving

Viewed 108

I have now added a more specific question here for just messages to tokens: FCM push notifications for Android have stopped working for message direct to token

Original post:

These used to work and now do not. It works fine for iOS. Unfortunately I can't pin point the exact date they stopped working but if the FCM analytics are accurate it may have been last working on the 25th July 2022 (5 days ago). However it does show a few "opens" after that date.

I send the notifications to topics or tokens from a firebase cloud function. I have been using the token to test although neither work.

This is the error I get:

Unhandled error { Error: The registration token is not a valid FCM registration token
    at FirebaseMessagingError.FirebaseError [as constructor] (/workspace/node_modules/firebase-admin/lib/utils/error.js:44:28)
    at FirebaseMessagingError.PrefixedFirebaseError [as constructor] (/workspace/node_modules/firebase-admin/lib/utils/error.js:90:28)
    at new FirebaseMessagingError (/workspace/node_modules/firebase-admin/lib/utils/error.js:256:16)
    at Function.FirebaseMessagingError.fromServerError (/workspace/node_modules/firebase-admin/lib/utils/error.js:289:16)
    at Object.createFirebaseError (/workspace/node_modules/firebase-admin/lib/messaging/messaging-errors-internal.js:35:47)
    at /workspace/node_modules/firebase-admin/lib/messaging/messaging-api-request-internal.js:79:51
    at process._tickCallback (internal/process/next_tick.js:68:7)
  errorInfo:
   { code: 'messaging/invalid-argument',
     message:
      'The registration token is not a valid FCM registration token' },
  codePrefix: 'messaging' } 

I sent a new campaign from the FCM console to a specific topic and this worked. So to me this would signify the issue is with the cloud function, but I am not 100% convinced on that. UPDATE: I just tried sending a test message from the FCM console to a specific token that is failing and this DOES NOT WORK.

Other things I have tried:

  1. Reverted the cloud function back to a previous version that worked (manually edited) (deleted function first).
  2. Tried a message to the topic from the cloud function. Did not work. No error.
  3. Tried a message to the token from the cloud function. Did not work. See error above.
  4. Tried a message to the topic from the FCM console. Worked.
  5. Tried a message to the token from the FCM console. Did not work.
  6. Checked the google-service.json file against a latest version from the firebase project. Looks fine. Note that previously I accidentally released a version of the Android app with a google-service.json file from the development project, I was convinced this was the cause of the error but after fixing this the issue was not resolved.

Cloud function code:

const notification: admin.messaging.Notification = {
      title: title,
      body: body
  }

  const message: admin.messaging.Message = {
    notification,
    token,
    android:{
      notification:{
        sound: 'default',
        icon: 'push_logo',
        color: '#000000',
      }
    },
    apns:{
        payload:{
          aps: {
            sound: 'default'
          }
        }
    }
  }

  return admin.messaging().send(message)

Which creates:

{ notification: { title: 'test', body: 'test' },
  token:
   'eyJhbGciOiJFUzI1NiIsInR5cCI6IkpXVCJ9.eyJhcHBJZCI6IjE6MTAxMzMxMzU1NjY5NDphbmRyb2lkOmQyODI0NGY1MWIzYTkyYTMwN2Y5NzciLCJleHAiOjE2NTk3NTExNTAsImZpZCI6ImV3YkF0c1psUm5xZ2Mzb0tQRWs0VnYiLCJwcm9qZWN0TnVtYmVyIjoxMDEzMzEzNTU2Njk0fQ.AB2LPV8wRQIgbdIAgIU76ziJc84g5gcNFNzFyid2xeDTcAywjecKFKoCIQD1KkflpXmfOSvp28XVmTtm4JtWaaVcycQRMXtKSNUM0Q',
  android:
   { notification: { sound: 'default', icon: 'push_logo', color: '#000000' } },
  apns: { payload: { aps: [Object] } } } 

UPDATE:

The topic is now working from the cloud function by sending direct to a token from the cloud function or FCM console is not working. I have not made any code changes since last testing.

1 Answers

Turns out it is due to the change from Capacitor 2 to Capacitor 3. Need to use:

PushNotifications.addListener('registration', async ({ value }) => {
  let token = value // Push token for Android

  // Get FCM token instead the APN one returned by Capacitor
  if (Capacitor.getPlatform() === 'ios') {
    const { token: fcm_token } = await FCM.getToken()
    token = fcm_token
  }
  // Work with FCM_TOKEN
})

See here: https://github.com/capacitor-community/fcm/issues/99

Related