React-native-notifications: Android notifications won't wake up device

Created on 2 Mar 2017  路  15Comments  路  Source: wix/react-native-notifications

Hi,

First of all, I always successfully receive the push notifications that I'm sending from server side. However, they do not wake up the device if it's locked. There are also a couple more problems I've noticed that may be related to this.

I'm going to list all the instances of unusual behaviour of the notification system:

  1. If the device is locked (display off), the notifications are received, but they do not wake up the device, nor do they trigger any sound or vibration at all.
  2. If the device is unlocked and my app is completely closed or in background, my device receives the notifications but again, they do not trigger sound/vibration.
  3. If the device is unlocked and my app is opened/in foreground, the notifications are received, they do trigger the default sound and vibration, but their content is empty, as if I didn't specify a title or body.

The payload that I send to https://gcm-http.googleapis.com/gcm/send looks like this:

{  
    "notification": {  
        "title": "Notification title test",
        "body": "Notification description test"
     },
     "priority": "high",
     "collapse_key": "test",
     "sound": "default",
     "to":"fkvgOg..[...]..r7xbb"
}

I use [email protected] and [email protected].

If you have any idea about why this happens or if the issue is from somewhere else, please let me know. Thanks.

Androicore bug

Most helpful comment

Ok, so the solution to this specific problem is to send both data and notification objects within the payload. If you want to send along some custom data, just append it to the data object, like you normally would. I suspect this is because when in background, Gcm automatically handles and displays the notification, while when the app is in foreground, react-native-notifications handles the notification.

This is how the payload should look:

{
    "notification": {
            "priority" : "high",
            "sound" : "default",
            "tag": "example",
            "icon" : "icon",
            "title" : "Notification Title",
            "body" : "Notification Body"
    },
    "data": {
            "priority" : "high",
            "sound" : "default",
            "tag": "example",
            "icon" : "icon",
            "title" : "Notification Title",
            "body" : "Notification Body",
            "custom_field" : {}
    },
    "registration_ids" : [
            "dikzJkIhCPs...TPA9"
    ]
}

This solves the problem mentioned at the beginning.

If you were to send only the notification object, you would have the following issues:

  • The notification would be empty when received in foreground
  • The notification would be ok when received in background, but it wouldn't trigger sound/vibration

If you were to send only the data object containing the notification fields:

  • The notification would be empty when received in background
  • The notification would be ok when received in foreground

Therefore, the only solution that I had found was to send the notification fields within both of these objects in the payload.

However, applying this would introduce the problem of #38, whose root cause was exactly the fact that he sent both data and notification with the payload, which in this case is the solution.

All 15 comments

Yup Same problem here

Is it possible that your devices are in Doze mode when this takes place? Try to compare your behavior with other (low-priority, e.g. not WhatsApp) notifications.

No, I'm running my app on Android 5.0.2, and since Doze mode was introduced in 6.0, that cannot be the case. Nevertheless, I do receive notifications from WhatsApp or other apps even with my device locked. edit: Even if they are low priority, I still receive them.
I'll try to tweak with the payload or the receiver.

However, have you got any idea as to why do the notifications appear as completely empty (except for the icon) when they are received while the app is in foreground?

Thanks

I have also noticed that if I close the app, the notifications won't arrive at all. Is there something I have to do or is it related to your background listener?

When I have the App opened the log successfully put out:
'Notification received on device' - with the correct Data but the Notification is empty except for the ic_launcher Icon when clicked setNotificationOpenedListener is firing.
I get the following error in the log:

D/GcmReceiver: Missing wake lock permission, service start may be delayed

03-10 10:59:53.195 10660-10766/com.fvjm.jphh W/Bundle: Key google.sent_time expected String but value was a java.lang.Long.  The default value <null> was returned.
03-10 10:59:53.195 10660-10766/com.fvjm.jphh W/Bundle: Attempt to cast generated internal exception:
                                                       java.lang.ClassCastException: java.lang.Long cannot be cast to java.lang.String
                                                           at android.os.Bundle.getString(Bundle.java:1121)
                                                           at com.google.android.gms.gcm.zza.zzF(Unknown Source)
                                                           at com.google.android.gms.gcm.GcmListenerService.zzo(Unknown Source)
                                                           at com.google.android.gms.gcm.GcmListenerService.zzn(Unknown Source)
                                                           at com.google.android.gms.gcm.GcmListenerService.zzm(Unknown Source)
                                                           at com.google.android.gms.gcm.GcmListenerService.zza(Unknown Source)
                                                           at com.google.android.gms.gcm.GcmListenerService$1.run(Unknown Source)
                                                           at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
                                                           at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
                                                           at java.lang.Thread.run(Thread.java:841)

When the app process is dead the device receives the notification with content but no action is logged when clicked even when the setNotificationOpenedListener is set.

@arnebr @andreipham Please have a look at issue #38 (more specifically, refer to this link: https://developers.google.com/cloud-messaging/concept-options#target)

@d4vidi From what I understand, the problem from #38 was the fact that both data and notification were sent along with the request, while the solution was sending only the data object with all the notification options inside it.

In my case, I only send the notification payload, without data. So I don't think I have the same problem. If I apply the suggested changes and use data instead of notification, the device obviously doesn't display any kind of notification at all. GCM's docs even state that the client is responsible for processing data messages, while the notification messages are automatically displayed as system notifications.

Can you please give me an example of a payload that you would normally send to gcm, which successfully triggers notifications? Perhaps something is wrong with mine.

Ok, so the solution to this specific problem is to send both data and notification objects within the payload. If you want to send along some custom data, just append it to the data object, like you normally would. I suspect this is because when in background, Gcm automatically handles and displays the notification, while when the app is in foreground, react-native-notifications handles the notification.

This is how the payload should look:

{
    "notification": {
            "priority" : "high",
            "sound" : "default",
            "tag": "example",
            "icon" : "icon",
            "title" : "Notification Title",
            "body" : "Notification Body"
    },
    "data": {
            "priority" : "high",
            "sound" : "default",
            "tag": "example",
            "icon" : "icon",
            "title" : "Notification Title",
            "body" : "Notification Body",
            "custom_field" : {}
    },
    "registration_ids" : [
            "dikzJkIhCPs...TPA9"
    ]
}

This solves the problem mentioned at the beginning.

If you were to send only the notification object, you would have the following issues:

  • The notification would be empty when received in foreground
  • The notification would be ok when received in background, but it wouldn't trigger sound/vibration

If you were to send only the data object containing the notification fields:

  • The notification would be empty when received in background
  • The notification would be ok when received in foreground

Therefore, the only solution that I had found was to send the notification fields within both of these objects in the payload.

However, applying this would introduce the problem of #38, whose root cause was exactly the fact that he sent both data and notification with the payload, which in this case is the solution.

done same as @andreipham told but still not getting notification when app is closed.

How to trigger callback if app was dropped from process on android ?

I tried different scenarios, only with data, with notification, with both, but nothing trigger callback if app killed from process. Anybody found the solution ?

@kholiavko-roman If app is killed from process it means nothing will happen.It should be in memory or in background then it will work.

But usually apps has possibility to run some code on push notificaiton, for example I need increase badge counter (small icon on app icon with count of new notificaion) on my app, on android.

For exaple react-native-fcm has this functionality, but I trying this plugin because I am using react-native-navigation by wix, and when app push arrives, when app killed, app started to foreground. Here is problem: https://github.com/wix/react-native-navigation/issues/1799

Same issue. When app in background, I can receive notification and bring my app to foreground, but if I killed the app, nothing happened when I sending notification to the device. Any solution so far?

Nothing.

Send only data message and use below code in FCM onMessageReceived() method.

PowerManager pm = (PowerManager)context.getSystemService(Context.POWER_SERVICE);
boolean isScreenOn = pm.isScreenOn();
Log.e("screen on.................................", ""+isScreenOn);
if(isScreenOn==false)
{
WakeLock wl = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK |PowerManager.ACQUIRE_CAUSES_WAKEUP |PowerManager.ON_AFTER_RELEASE,"MyLock");
wl.acquire(10000);
WakeLock wl_cpu = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK,"MyCpuLock");

wl_cpu.acquire(10000);

}

Was this page helpful?
0 / 5 - 0 ratings