DroidOS
DroidOS

Reputation: 8890

Silently handling push notifications in the background

I am in the process of creating a Phonegap app - currently for Android - that uses Onesignal to handle push notifications. Creating the core app has not been too difficult. In the context of the app there is really no need for the user to view the notification.

What I do require is to be able to handle the message payload in app JavaScript. This is not an issue if I am willing to wait for the user click the notification icon at which point the [notificationopenedcallback] event is triggered and I am at liberty to do what I like with the payload (see documentation).

Nice but not enough. What I need to do is manipulate the payload in JavaScript when the notification arrives - not at some point in the future when the user clicks the notification icon.

It is not clear to me that this can be done - perhaps because there is no way to execute JS code for an app that has been backgrounded. If that is indeed the case can I somehow get round the issue by writing a native app?

Upvotes: 4

Views: 1256

Answers (2)

Roy Falk
Roy Falk

Reputation: 1741

Disclaimer: I've just developed a plugin in Cordova, but haven't looked at push notifications in ages.

In any case, the quick answer is to implement the push notifications in a plugin. Call the plugin from JS and start a background service to listen for and handle the payload.

You would do that by writing a plugin that contains a class that is dereived from BroadcastReceiver. you would handle the incoming notification in the onReceive.

As for other plugins, my limited experience is that you should keep using the cordova framework. This means that OneSignal should be added using cordova. I'm basing this on my use of support V4 plugin so YMMV.

Edit: On the other hand and on second thought, if you're using something standard like Google notifications, you probably don't need OneSignal.

One thing I haven't done is generate a manifest with Service and intent in the plugin. I actually did this in an AAR that was attached to the plugin. So I'm not certain if the AAR manifest will be appended to the main manifest. It probably should, but I haven't looked at this. If this is the case, you don't even need to call the native code from the JS. The broadcast receiver will get the notification. Otherwise, you would need to call registerReceiver from JS.

Finally, I think you're right. If the app is in the background, it's stopped and resources like WebView are released. This means the JS will not run.

Upvotes: 3

SamG
SamG

Reputation: 855

I think you'll have to write a native Android app if you want to reliably handle push notifications as they arrive.

I've been using phonegap-plugin-push to handle push notifications in my Cordova app and it does not currently wake up the application under all circumstances on newly received push notifications. See for example this issue #806

For my purposes (which are non-critical), I rely on the user to click on the notification to wake up the app under all circumstances. The good thing about phonegap-plugin-push is that it supports iOS, Android and Windows in case you want to support other platforms in the future.

Upvotes: 2

Related Questions