Skip to content
This repository has been archived by the owner on Sep 4, 2020. It is now read-only.

I did not recieve any background notifications when app is not running #707

Closed
bastian-meier opened this issue Mar 16, 2016 · 25 comments
Closed

Comments

@bastian-meier
Copy link

I did not Recieve push Notifications when the app is not Running. It works perfect for my ios device and also an Android 4.4 and a Android 5.1 Device.
But on my Huawei Mate7-L09 with Android 6.0 (Kernel 3.10.86) i recieve push-notifications when the app is active or in Background, but not when the app is not Running.

I am using Phonegap Build with Phonegap Version 5.1.0 and Version 1.6.0 of the push-plugin.

<plugin name="phonegap-plugin-push" source="npm" spec="1.6.0">
    <param name="SENDER_ID" value="XXXXXXXXXXXX" />
</plugin>

This is the "init-code" of my App:

push = PushNotification.init({
    "android": {
        "senderID": "XXXXXXXXXXXX"
    },
    "ios": {
        "alert": "true",
        "badge": "true",
        "sound": "true",
        "clearBadge": "true"
    },
    "windows": {}
});

Below is the Message i sent from Backend:

{ 
   "registration_ids": [recipient], 
   "data" : 
      {
         'message' : message, 
         'data' : data, 
         'title' : 'Jabbit', 
         'style': "inbox", 
         "summaryText" : "Du hast %n% neue Benachrichtungen"
      }
}

This is the adb logcat when recieving an GCM Push when the app is not running:

03-16 09:31:54.332  6218 18981 I GCM     : GCM message APP_ID 0:1458117116927762%d5bec4aff9fd7ecd
03-16 09:31:54.373  6218 18981 I SendBroadcastPermission: action:com.google.android.c2dm.intent.RECEIVE, mPermissionType:0
03-16 09:31:54.373  6218 18981 I SendBroadcastPermission: action:com.google.android.c2dm.intent.RECEIVE, mPermissionType:0
03-16 09:31:54.387  6218  6218 W GCM-DMM : broadcast intent callback: result=CANCELLED forIntent { act=com.google.android.c2dm.intent.RECEIVE pkg=APP_ID(has extras) }
03-16 09:31:54.396  4208  4483 I HwCustPowerManagerServiceImpl: handleMessage C2DM_ACQUIRE_EVENT, mC2DMWakeLock.acquire!

I hope you can help me.

Thanks
Bastian

@jakari
Copy link
Contributor

jakari commented Mar 16, 2016

Have you tried if it does help to set the priority to high when sending the message? Atleast that's required on IOS to wake up an app that isn't running
https://developers.google.com/cloud-messaging/concept-options#setting-the-priority-of-a-message

@macdonst
Copy link
Member

@bastian-meier if you force quit the app it won't get the push notification event. I have to write some code to wake up the push plugin if the app is killed.

@bastian-meier
Copy link
Author

@macdonst thanks for your Answer, that would be Nice. But its not my primary Issue.

The Problem is not that my app dont recieve the event, the Problem is that my Device wont show the Notification in the "notification area". On my other Devices it works also after i force quit the app or when i reboot the phone. But not on my Huawei Mate7. On this Device the app have to run to recieve the gcm notification

@macdonst
Copy link
Member

@bastian-meier okay, that is an odd one. Sadly I don't have a Huawei Mate7 to test on. All of the Android devices I have, Nexus 5, 5X, 6, 6P and OnePlusOne all get the notification in the shade even when the app is forced closed.

Obviously, the on('notification') handler is not run even when I tell it to be a background because the app is dead.

This seems to be a problem with the Huawei version of Android. What exactly is the Android variant on that device?

@bastian-meier
Copy link
Author

It is the Beta Version for Android 6.0

The Build Number is MT7-L09C432B513
Kernel 3.10.86-g5585a23

Is there anything i could do to help you?

@jianhao1203
Copy link

I faced same problem that when the app is not running background and foreground (Android), the notification cannot get, anyone has the solution for it? @macdonst , do u mind to share info how to wake up the push plugin if the app is killed?

@jianhao1203
Copy link

+1 @macdonst I using device as follow:

Model: Mi 4i
Android version: 5.0.2 LRX22G

Can I know that model of phone that can receive notification when apps is killed (not run in background and foreground)? Or is there any way to wake up the push notification service when the app is not running?

@macdonst
Copy link
Member

@jianhao1203 if you look at my earlier comment I've tested this on Nexus 5, 5X, 6, 6P and OnePlusOne. The problem seems to be with Huawei and Xiaomi phones. I'd be interested if they are forking Android and if GCM has problems running on those devices.

@jianhao1203
Copy link

@macdonst I did some testing using WhatsApp for my Xiaomi phone and Lenovo phone. I used Lenovo phone to send message to my Xiaomi phone. Although WhatsApp app is killed off(not running in background) in my Xiaomi phone, however the push notification still can be received in my Xiaomi phone. So I doubt that is it possible for me to modify the code to gather this requirement?

One more question is it the following code able to gather the push notification received when app is not running in background?

`function onDeviceReady() {
document.addEventListener("backbutton", onBackKeyDown, false);

        var push = PushNotification.init({ "android": {"senderID": "263977057230"},
        "ios": {"alert": "true", "badge": "true", "sound": "true"}, "windows": {} } );

        push.on('registration', function(data) {
        console.log(data.registrationId);
        //alert("lai");
        alert(data.registrationId);

        });

        push.on('notification', function(data) {
        console.log(data.message);
        alert(data.title+" Message: " +data.message);
        // data.title,
        // data.count,
        // data.sound,
        // data.image,
        // data.additionalData
        });

        push.on('error', function(e) {
        console.log(e.message);
        });
    }`

@jianhao1203
Copy link

@macdonst , I tried to push notification using http://phpfiddle.org/
with the following code:

'here is a message. message2', 'title' => 'This is a title. title', 'subtitle' => 'This is a subtitle. subtitle', 'tickerText' => 'Ticker text here...Ticker text here...Ticker text here', 'vibrate' => 1, 'sound' => 1 ); $fields = array ( 'registration_ids' => $registrationIds, 'data' => $msg ); $headers = array ( 'Authorization: key=' . API_ACCESS_KEY, 'Content-Type: application/json' ); $ch = curl_init(); curl_setopt( $ch,CURLOPT_URL, 'https://android.googleapis.com/gcm/send' ); curl_setopt( $ch,CURLOPT_POST, true ); curl_setopt( $ch,CURLOPT_HTTPHEADER, $headers ); curl_setopt( $ch,CURLOPT_RETURNTRANSFER, true ); curl_setopt( $ch,CURLOPT_SSL_VERIFYPEER, false ); curl_setopt( $ch,CURLOPT_POSTFIELDS, json_encode( $fields ) ); $result = curl_exec($ch ); curl_close( $ch ); echo $result; ?>

Is this might be the caused the notification cannot be received when the app is killed off? Any alternative way can send push notification besides this? thanks.

@jianhao1203
Copy link

@macdonst Good news, after I do some research online, I found this post http://stackoverflow.com/questions/30748107/xiaomi-does-not-receive-notification-when-application-is-not-running
This posts stated the notification is not received because the permissions of autostart is not allowed.
I followed the steps and lastly my Mi4i also can received the notification , just want to share to others who faced same scenario with me, I think @bastian-meier the Huawei device might have same kinds of permissions settings that caused the notification cannot be received if not running at background or foreground.

Anyway, thanks so much for previous comments. =)

@bastian-meier
Copy link
Author

It is working for me now.
I am setting priority to 2 in my json and i am recieving the push notifications when app is not running

@macdonst
Copy link
Member

I'm re-opening it so I can add this info to docs. Glad it was resolved for both of you.

@shaoner
Copy link

shaoner commented Apr 11, 2016

Hi,

I've just faced the exact same issue on a Huawei device (honor 7).

In order to get it working, you need to go to Settings > Protected apps > check "My App"

Applications like whatsapp seems to be "protected" by default, any idea how?

@deshanm
Copy link

deshanm commented Aug 8, 2016

I also had this problem.. I used mixpanel plugin (from stable branch) and phonegap-plugin-push.. I did not recied any from mixpanel.. because mixpanel json out and phonegap-plugin-push json output are not matched...

@filipsuk
Copy link

filipsuk commented Jan 27, 2017

I had the same problem with Huawei P9 lite, found the Protected apps under Battery manager (see http://android.stackexchange.com/questions/152649/what-is-protected-apps-in-huawei-phones). However you can't really communicate this to the end users 😕

@sarkaramitabh300
Copy link

@bastian-meier How to set priority to 2? Can you share some code. I am facing same problem.

@fedepasi
Copy link

@bastian-meier @AmitabhWork I have the same problem on P8 lite, still no solution?

@NasserAlmanji
Copy link

Android 5 Lolipop,
force-start:1
neither content-available: 1

will wake up a piece of code when the app is killed.

Basically, the .on("notification") will be called only when foreground or background (switched to another application for example).

App killing is when removed from RAM, by swiping it left or right when switching between previously opened apps.

Apps like whatsapp fetch more data and images even when they are killed. A notification is able to trigger a piece of code.

@flachica
Copy link

flachica commented Jun 17, 2017

Same issue here:

what version number of plugin are you using?

1.10.5

which platform and version you are testing on?

I'm using Android Studio 2.3.3 recently updated, device Huawei P8, Android 6.0, EMUI 4.0.1

  • steps to reproduce

Close the App (not in background, is completly closed) and send Push Notification

  • expected result

Get notified by a message in Status bar

  • actual result

Nothing in UI but get this message in adb logcat | grep GCM

06-17 13:51:52.028 4952 7647 I SendBroadcastPermission: action:com.google.android.c2dm.intent.RECEIVE, mPermissionType:0
06-17 13:51:52.042 4952 4952 W GCM-DMM : broadcast intent callback: result=CANCELLED forIntent { act=com.google.android.c2dm.intent.RECEIVE pkg=info.larinconadasisepuede.lrssp (has extras) }

@PetruMagdeleine
Copy link

Get it working with EMUI devices (Huawei P8 Lite) using this:
Settings > Advanced settings > Battery manager > Protected apps
When my app is allowed here, I can now received notifications when app is closed.

Problem, can we "protect" an app by default ...

Source [6]: https://www.androidcentral.com/first-6-tweaks-you-need-make-your-huawei-or-honor-phone

@flachica
Copy link

Yes, that was the problem, thank you very much

@joshimohit
Copy link

Any Alternate to FCM/GCM when phone is in doze mode?

@shubhamkhuva
Copy link

shubhamkhuva commented Apr 3, 2018

@PetruMagdeleine yeah it's work but any alternate way to set programmatically.

@lock
Copy link

lock bot commented Jun 3, 2018

This thread has been automatically locked.

@lock lock bot locked as resolved and limited conversation to collaborators Jun 3, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Development

No branches or pull requests