Phonegap-plugin-push on("notification") event is not firing when app is in background

I am using following plugin for push notification in Ionic2

Expected Behaviour:
When app is closed, And notification received, And when user tap the notification, on(“notification”) event should fire after app opens.

Actual Behaviour:
I am getting notification successfully. But When Application is in background or closed, at that time when I receive notification and I tap the notification, on(“notification”) event is not firing.

Cordova version 7.0.1
Android version 6.2.3

My code:

  this.platform.ready().then(() => {
        this.pushsetup();
    });

    private pushOptions: PushOptions;
    private pushObject: PushObject;
    pushsetup() {
        // to check if we have permission
        this.push.hasPermission()
            .then((res: any) => {
                if (res.isEnabled) {
                    console.log('We have permission to send push notifications');
                    // configuration of push notification
                    this.pushOptions = {
                        android: {
                            senderID: 'XXXXXXXXXXX',
                            icon: 'icon_notification'
                        },
                        ios: {
                            alert: 'true',
                            badge: true,
                            sound: 'false',
                            senderID: 'XXXXXXXXXXX'
                        },
                        windows: {}
                    };
                    this.pushObject = this.push.init(this.pushOptions);

                    // attach push events
                    this.storage.get('isPushRegistered')
                        .then(isPushRegistered => {
                            if( !isPushRegistered ){
                                this.pushObject.on('registration').subscribe((registration: any) => {
                                    console.log('Device registered', registration)
                                    this.storage.set('isPushRegistered', true)
                                });
                            }
                        })
                    
                    
                    this.pushObject.on('notification').subscribe((notification: any) => {
                        console.log('Received a notification', notification)
                    });
                    this.pushObject.on('error').subscribe(error => console.error('Error with Push plugin', error));
                }
            });
    }

So, on my code, you can see this.pushObject.on(‘notification’) event. that is not firing when app is closed.

Thank you for your time and support.

+1 to this question since I’m running into the same problem, my code is similar to the code in the ionic native push plugin docs:

registerPush() {
    this.push.hasPermission().then((res: any) => {
      const options: PushOptions = {
        android: {
          senderID: 'XXXXXXXXXXXX',
        },
        ios: {
          alert: 'true',
          badge: 'true',
          sound: 'false'
        }
      };

      const pushObject: PushObject = this.push.init(options);

      pushObject.on('registration').subscribe((registration: any) => {
        // Fires when device finishes registering to the push service
        console.log('Device registered', registration);
      });

      pushObject.on('notification').subscribe((notification: any) => {
        // Fires when app is open and receives push or
        // when app is opened after user tapping a notification
        console.log('Received a notification', notification);
      });

      pushObject.on('error').subscribe(error => {
        // handle any plugin error here
        console.error('Error with Push plugin', error);
      });

    });
  }

Additionally, while notifications appear correctly if my app is running in the background, if I force close the app and send a new notification through firebase, the device never displays a new notification.

I’ve tested on different android devices and got the same result. Everything works fine in iOS.

Same cordova version.

1 Like

Got the same in my app and was glad to find the discussion … but no responses yet.
Any suggestions or have you found a solution?

Thank in advance for any hint!