2

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.

Imad Archid
  • 25
  • 1
  • 5
  • 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 Aug 24 '15 at 01:47
  • But I need to start the push notifications service directly after device boot. – Imad Archid Aug 24 '15 at 16:01
  • Yes, you can start service automatically after device reboot. Please read about service in Android: http://stackoverflow.com/questions/4562734/android-starting-service-at-boot-time – Hanh Le Aug 25 '15 at 01:40
  • Thank you ! But in cordova, I can't access Java classes. – Imad Archid Aug 25 '15 at 11:23
  • 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 Aug 26 '15 at 01:37

1 Answers1

1

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.

Community
  • 1
  • 1
jkasten
  • 3,899
  • 3
  • 32
  • 51
  • 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 Aug 26 '15 at 23:41
  • 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 Aug 26 '15 at 23:47
  • It looks like the Intel XDK is still using Cordova 4.x instead of 5.x per [this post](https://software.intel.com/en-us/forums/topic/584297). 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](https://github.com/one-signal/OneSignal-Cordova-SDK/tree/PGB-Compat) branch. – jkasten Aug 27 '15 at 00:44