Saurabh Gupta
Saurabh Gupta

Reputation: 345

(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.

Upvotes: 0

Views: 2545

Answers (2)

Rodrigo Gomez-Palacio
Rodrigo Gomez-Palacio

Reputation: 904

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

Upvotes: 4

Sam
Sam

Reputation: 1139

I find this in the doc:

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

Maybe your app is not working on background.

Or the problem is you need to call ur function after the startinit :

this.onesignal.startInit("YOUR_APPID")
this.onesignal.handleNotificationReceived() 

Upvotes: 1

Related Questions