phonegap / phonegap-plugin-push

Register and receive push notifications
MIT License
1.94k stars 1.91k forks source link

Not receiving push notifications when APP is cleared from foreground #2621

Open melwinVincent opened 5 years ago

melwinVincent commented 5 years ago

Expected Behaviour

Need to receive push notifications when app is running , when is app is in foreground and also when app is cleared from foreground (clearing from the list of running apps)

Actual Behaviour

I'm receiving notifications when app is running and when app is in foreground. But not receiving when app is cleared from the list of foreground running apps. But everything works as expected when I go to the settings->Battery Optimization and choose the option "Don't Optimize". Then I start getting push notifications even when the app is not in the foreground apps list. And if I choose again to 'Optimize' the app, then surprisingly it works in the background as expected.

Reproduce Scenario (including but not limited to)

Screenshot of Don't Optimize in Settings image

Steps to Reproduce

Just create a sample ionic (version-1) mobile app

ionic start myproject ionic platform add android@6.3.0 or ionic platform add android@latest cordova plugin add phonegap-plugin-push@2.1.2

Platform and Version (eg. Android 5.0 or iOS 9.2.1)

Android 8

(Android) What device vendor (e.g. Samsung, HTC, Sony...)

I'm using OnePlus 3T

Cordova CLI version and cordova platform version

Tested with both versions of android -> "cordova-android": "^7.1.2" and "cordova-android": "^6.3.0", Cordova version is -> 7.1.0

Plugin version

phonegap-plugin-push version ->

<plugin name="phonegap-plugin-push" spec="^2.1.2">
     <variable name="FCM_VERSION" value="11.0.1" />
</plugin> 

Sample Push Data Payload

{
  "registration_ids": ["<my-device-id>"],
  "data": {
    "title": "Test Push",
    "message": "Testing",
    "info": "super secret info",
    "content-available": "1"
  }
}

Sample Code that illustrates the problem


     const push =  PushNotification.init({
        android: {
            senderID: "<my-sender-id>", 
            sound: "true",
            vibrate: "true",
            icon: "notification_icon",
            iconColor: "#002060",
            clearBadge : true
        },
        browser: {
            pushServiceURL: 'http://push.api.phonegap.com/v1/push'
        },
        ios: {
            alert: "true",
            badge: "true",
            sound: "true"
        },
        windows: {}
      });

      PushNotification.hasPermission(function(data) {
        if(data.isEnabled) {
          console.log('isEnabled');
        }
      },function(err){
        console.log(err)
      });

      push.on('registration', function(data){
        console.log('push success data',data);

      });

      push.on('notification', function(data){
        console.log('push notification data=',data);
        if(data.additionalData.coldstart){
            //Notification was received on device tray and tapped by the user.
            console.log(JSON.stringify(data));
            alert("yes");
          }else{
            //Notification was received in foreground. Maybe the user needs to be notified.
            console.log(JSON.stringify(data));
          }
      });

      push.on('error', function(e){
        console.log('error',e);
      });
melwinVincent commented 5 years ago

@macdonst : Hi Simon , Please expedite. Our production release is on hold because of this issue and it's getting hard to convince the client. Please help.

Thanks and regards, Melwin Vincent

jcesarmobile commented 5 years ago

Not sure what you expect, but if a phone setting affects the push notifications there is nothing we can do about it.

melwinVincent commented 5 years ago

@jcesarmobile : Yes you are absolutely right. I need to check this in other manufacturers phones and lets see if the same issue exists.

macdonst commented 5 years ago

@melwinVincent I think the root cause of this issue is that the One Plus 3T battery optimization kills the service that receives the push notifications for your app when it's not running. I don't think there is anything I can do to fix this short of saying don't turn on the Optimize function for the app.

Have you tried running your app on a non-One Plus 3T device? I have a feeling it would work as intended on most phones. However, please read: https://github.com/phonegap/phonegap-plugin-push/blob/master/docs/PAYLOAD.md#chinese-android-phones

melwinVincent commented 5 years ago

@macdonst : Hi Simon. Thank you for your support. Everything works as expected in Samsung and Huawei devices with android 8. This issue is something specific with one plus phones.