简体   繁体   中英

Ionic push notifications with Capacitor, registering and getting token but not receiving notifications Android

I have an Ionic 5 app with Capacitor 3 and I'm trying to receive notifications using Firebase Cloud messaging, on an Android device. I followed the configurations,(I downloaded the google JSON file and put my app id name correctly ) and I'm getting correctly the device token. Once my app is open I get the token successfully without any error and then I send a notification sharing my token to the Firebase test message, the notification never arrived, and also I never get an error of push notification in my logger. This is the code that I use for push notification.

export class PushNotificationsService {

  constructor(private readonly http: HttpClient, private notificationState: NotificationsStore) { }

   public initPush() {
  if (Capacitor.getPlatform() !== 'web') {
      this.registerPush();
     }
  }

  private registerPush() {
    PushNotifications.requestPermissions().then(async result => {
      if (result.receive === 'granted') {
        // Register with Apple / Google to receive push via APNS/FCM
        console.log('granted');
       await PushNotifications.register();
      } else {
        // Show some error
        console.log('errorr');
      }
    });

    // On success, we should be able to receive notifications
    PushNotifications.addListener('registration',
      (token: Token) => { (I get through this step and access the token successfully)
        console.log('Push registration success, token: ' + token.value);
        this.notification state.setToken(token.value);
      }
    );

    // I never get this step error
    PushNotifications.addListener('registrationError',
      (error: any) => {
        console.log('Error on registration: ' + JSON.stringify(error));
      }
    );

    PushNotifications.addListener('pushNotificationReceived',
      (notification: PushNotificationSchema) => {
        console.log('Push received: ' + JSON.stringify(notification));
      }
    );

    PushNotifications.addListener('pushNotificationActionPerformed',
      (notification: ActionPerformed) => {
        console.log('Push action performed: ' + JSON.stringify(notification));
      }
    );
  }

I also have capacitor.config.json like this:

  "PushNotifications": {
      "presentationOptions": ["badge", "sound", "alert"]
    }

Also, I checked that my device and app have permission for notifications and are enabled both. I tried and test this issue with my app open and closed and open only in the background and the notification never arrives. What it could be? Any clue? Thank you

Android Phone Please Create the channel Like this, Test or Add screenshot for console Error

    if (Capacitor.getPlatform() === 'android') {
   PushNotifications.createChannel({
    id: 'fcm_default_channel',
    name: 'app name',
    description: 'Show the notification if the app is open on your device',
    importance: 5,
    visibility: 1,
    lights: true,
    vibration: true,
  });
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM