0

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.

Community
  • 1
  • 1
Saurabh Gupta
  • 315
  • 1
  • 5
  • 14

2 Answers2

4

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

  • 1
    Thanks for the reply, but what if the user doesn't click the notification and opens the app directly and the notification itself gets dismissed upon opening the app? what then? – Saurabh Gupta Jun 10 '19 at 07:10
  • Unfortunately, the user has to click the notification for it to be handled in that case Saurab – Rodrigo Gomez-Palacio Jun 11 '19 at 16:48
1

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() 
Sam
  • 1,129
  • 2
  • 10
  • 24
  • I have edited the question above, actually i missed to surround the `handleNotificationReceived()` with `openOneSignalMessage()`"{ }. – Saurabh Gupta Mar 28 '19 at 10:42
  • As you told i am doing the same. Probably the app is not running in the background after killing it from the recent apps. – Saurabh Gupta Mar 28 '19 at 10:44
  • I found this solution https://github.com/OneSignal/OneSignal-Cordova-SDK/issues/257 hope it can help you. – Sam Mar 28 '19 at 12:34
  • the problem is you can't call `handleNotificationReceived()` if the app is not working on background. – Sam Mar 29 '19 at 09:26
  • why is that happening, I mean it is supposed to be running in the background. – Saurabh Gupta Apr 01 '19 at 08:12
  • Try this plugin to see if your plugin work on background and if not , just activate it :https://github.com/katzer/cordova-plugin-background-mode – Sam Apr 01 '19 at 08:23
  • thanks, I tried this and it actually crashed application saying the app keeps on stopping – Saurabh Gupta Apr 02 '19 at 09:06
  • Sry but I really don't know how help you more , maybe someone will find a solution for you. – Sam Apr 02 '19 at 09:53