2
votes

I'm currently working on Android (Cordova) App and I'm using OneSignal push notifications. First of all, push notifications were not delivered if I close the app, so I had to add a Cordova plugin to keep my app running in the background :

cordova.plugins.backgroundMode.enable();

The problem is, when I boot my phone I cannot receive push notifications (Because deviceready is not fired). Until I open the app.

Is there a solution to immediately start push notifications service after device boot, like something running in the background ?

My code after deviceready :

    cordova.plugins.backgroundMode.enable();

    var notificationOpenedCallback = function(jsonData) {
    console.log('didReceiveRemoteNotificationCallBack: ' + JSON.stringify(jsonData));
  };

    window.plugins.OneSignal.init( "my-api-key",
                                        {googleProjectNumber: "7-cant-share-it"},
                                        app.didReceiveRemoteNotificationCallBack);
    }

Thanks.

1
That right. When Android app is background then WebView will be stop(all JS code stop too, only native code implement in service running) until it open again. - Hanh Le
But I need to start the push notifications service directly after device boot. - Imad Archid
Yes, you can start service automatically after device reboot. Please read about service in Android: stackoverflow.com/questions/4562734/… - Hanh Le
Thank you ! But in cordova, I can't access Java classes. - Imad Archid
I don't think so. You can implement Java service (code by Java not JS) and do anything, you don't care about cordova. Cordova provide way to call Java code from JS, you should read document to understand about cordova architecture. - Hanh Le

1 Answers

1
votes

How are you closing your app? Make sure you are using our OneSignal Cordova SDK 1.7.5 or newer since a back button issue was fixed related to receiving notifications. You can see the current version your project is using by running cordova plugin list.

I did some testing on an Android 5.0.2 and a 4.3 device and notifications are received every time on boot as long as the app wasn't placed into a "Force Stop" state.

When your app is placed into the "Stopped State" all BroadcastReceivers in your app will stop receiving intents. This includes GCM intents that fire when a notification message is received from Google. This is a known and expected behavior for Android 3.1 and newer.

See the following links for more details on how Force Stop effects apps:

To recap, OneSignal notifications will always displayed/received by your app as long as it isn't put into a force stop state from either pressing the "Force Stop" button in Settings>App or from an aggressive 3rd party Task Manager that also performs the same action.

Thanks.