Messaging/registration-token-not-registered when sending push notification via Firebase Cloud Messaging

I’m trying to get push notifications working on my Ionic 6 app. I followed the instructions here Push Notifications Capacitor Plugin API | Capacitor Documentation and saved the generated token in Firestore. Using cloud functions, I run the following to send notification to that token when a document is updated/created…

// recipients = the generated token
function sendPushNotification(recipients, title, body, objParams) 
{
    var payload = {
        notification:{
            title: title,
            body: body,
            sound: 'default',
            badge: '1',
            click_action:"FCM_PLUGIN_ACTIVITY"
        },
        data: objParams
    }
   
    return admin.messaging().sendToDevice(recipients, payload)
        .then((response)=>
        {
            console.log("Notification success: ", JSON.stringify(response));
            return true;
        })
        .catch((error)=>{ console.log("Error sending notification: " + JSON.stringify(error)); console.log("Payload: " + JSON.stringify(payload));
        });//sendtodevice
}

and get the following error

{“error”:{“code”:“messaging/registration-token-not-registered”,“message”:“The provided registration token is not registered. A previously valid registration token can be unregistered for a variety of reasons. See the error documentation for more details. Remove this registration token and stop using it to send messages.”}}

Yet if I copy the generated token and send it a test notification via Engage > Messaging > New Campaign > Notifications in the Firebase console, it sends the notification and the device receives it.
This is the case for both iOS and Android.

What am I doing wrong??

My entire app.component.ts is below (I want the token generated and stored when the app opens)

import { Component } from '@angular/core';
import {
  ActionPerformed,
  PushNotificationSchema,
  PushNotifications,
  Token,
} from '@capacitor/push-notifications';
import { Device } from '@capacitor/device';
import { AlertController } from '@ionic/angular';
import { StorageService } from '@services/storage.service';


@Component({
  selector: 'app-root',
  templateUrl: 'app.component.html',
  styleUrls: ['app.component.scss']
})
export class AppComponent {
  pushNotificationToken: any; // I save the token to database because i need it in my backend code.

  constructor(private storage: StorageService,
    private alertController: AlertController) {
      this.initPush();
  }

  initPush() {
    Device.getInfo().then(res => {
      if (res.platform !== 'web') {
        this.registerPush();
      }
    });
  }

  private registerPush() {
    PushNotifications.requestPermissions().then((permission) => {
      if (permission.receive) {
        // Register with Apple / Google to receive push via APNS/FCM
        PushNotifications.register();
      } else {
        // No permission for push granted
      }
    });

    PushNotifications.addListener(
      'registration',
      async (token: Token) => {
        console.log('My token: ' + JSON.stringify(token));
        this.pushNotificationToken = JSON.stringify(token.value);
        await this.storage.set('deviceToken', token.value);
                        
      }
    );

    PushNotifications.addListener('registrationError', (error: any) => {
      console.log('Error: ' + JSON.stringify(error));
    });

    PushNotifications.addListener(
      'pushNotificationReceived',
      async (notification: PushNotificationSchema) => {
 
console.log({notification})
      }
    );



    PushNotifications.addListener(
      'pushNotificationActionPerformed',
      async (notification: ActionPerformed) => {

        console.log({notification})
  
      }
    );
  }

  resetBadgeCount() {
    Device.getInfo().then(res => {
      if (res.platform !== 'web') {
        PushNotifications.removeAllDeliveredNotifications();
      }
    });
  }
}
1 Like