Ionic Native Push: push.on('notification') when app is CLOSED


#1

Does anyone now, how you can trigger the on.notification handler on Android, when the app is closed?

I use Firebase Cloud Messaging and here is the message that I send:

       'message': {
            'data': {"data": data},
            'android': {
                'priority': "high",
                'data': {
                    "androidData": data,
                    'content-available': '1'
                }
            }
        }

As you can see I’m sending silet notifications.
And here is my code in the app:

initPush() {
    console.log("Init push!");
    this.options = this.initPushOptions();
    this.pushObject = this.push.init(this.options);

    this.pushObject.on('notification').subscribe((notification: any) => {
      console.log('Received a notification', notification);
      if(this.platform.is('ios')) {
        this.handleIOSNotification(notification, this.pushObject);
      } else if(this.platform.is('android')) {
        this.handleAndroidNotification(notification);
      }
    });

    this.pushObject.on('registration').subscribe(
      (registration: any) => {
        console.log('Device registered', registration);
        this.appDataProv.keepOrUpdateFcmToken(
          registration.registrationId
        ).then(() => {
          console.log(
            `Updated Fcm Token: ${registration.registrationId}`
          );
        }).catch(err => console.log(err));
      }
    );

    this.pushObject.on('error').subscribe(
      error => console.error('Error with Push plugin', error)
    );
  }

On Android the on notification only gets called, when the app is running, but not when the app is closed… Does anyone know how what you should do to trigger this when the app is closed?


#2

I had the similar problem a few days back . Firstly, You cannot trigger the event when the app is close (and not in background) ,You can trigger it when the user clicks on the notification and open the app.
Read this link its pretty useful

Secondly I need to know a bit about your architecture . Where is your service placed or are you using it in app.component.ts.