Search code examples
cordovabackgroundpush-notificationphonegap-plugins

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?


Solution

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