Search code examples
androidcordovanotificationsbackground-serviceonesignal

cordova - Android push notifications not received when app is closed


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.


Solution

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