Search code examples
ionic-frameworkionic2ionic3onesignalandroid-push-notification

(Ionic) handleNotificationReceived does not get called if the app is killed


I am working on an ionic app and I am using onesignal for push notifications.

The problem I am facing is that the handleNotifiactionRecieved() does not get triggered upon receiving a push notification when the app is not killed (that is removed from the recent apps). Though it works as expected when the app's running but in the background(not inFocus).

setup code is something like this:

if (this.platform.is('cordova')) {
        this.oneSignal.startInit('APP_ID');
        this.oneSignal.inFocusDisplaying(this.oneSignal.OSInFocusDisplayOption.None);
        this.openOneSignalMessage();
        this.oneSignal.endInit();
      }

my code is something like this:

openOneSignalMessage() {
this.oneSignal.handleNotificationReceived().subscribe((data) => {
      this.MsgBody = data.payload.body
      console.log('newMessageOneSignal MsgBody', this.MsgBody);
 });
}

actual result: handleNotificationReceived() doesn't get called when the app is not running, that is killed.

expected result: handleNotificationReceived() should be called every time a push notification arrives even when the app is not open.

How do I trigger the method every time?

Thanks in advance.


Solution

  • Thanks for pointing out the issue in the docs. They have been updated with the correct info:

    handleNotificationReceived (builder method)

    Sets a notification received handler. Only called if the app is running in the foreground at the time the notification was received.

    If you want to handle from a killed state, make sure to use the handleNotificationOpened method