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.

upvote
  flag
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
upvote
  flag
But I need to start the push notifications service directly after device boot. – Imad Archid
upvote
  flag
Yes, you can start service automatically after device reboot. Please read about service in Android: //allinonescript.com/questions/4562734/… – Hanh Le
upvote
  flag
Thank you ! But in cordova, I can't access Java classes. – Imad Archid
upvote
  flag
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 11

up vote 1 down vote accepted

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.

upvote
  flag
Thank you for your answer :) I forgot to mention that I'm using IntelXDK to build my app. Unfortunetly, I don't know how if I can add OneSignal plugin from npmjs plugins list. – Imad Archid
upvote
  flag
I also forgot to mention that I'm using a 3rd party plugin named "Admob Pro". Is it possible that it has a conflict with OneSignal SDK ? ( When I build, com.google.playservices is also installed as a plugin) – Imad Archid
upvote
  flag
It looks like the Intel XDK is still using Cordova 4.x instead of 5.x per this post. 5 or higher is required to use npmjs plugins. Since your using Admob and it already contains Google Play services you will need to use or compat version of our plugin so the 2 plugins don't conflict on build. You can do so by using the PGB-Compat branch. – jkasten

Not the answer you're looking for? Browse other questions tagged or ask your own question.