react-native-notifications: Android notifications won't wake up device
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:
- 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.
- 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.
- 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 react-native-notifications@1.1.7 and react-native@0.39.2.
If you have any idea about why this happens or if the issue is from somewhere else, please let me know. Thanks.
About this issue
- Original URL
- State: closed
- Created 7 years ago
- Comments: 15
Ok, so the solution to this specific problem is to send both
dataandnotificationobjects within the payload. If you want to send along some custom data, just append it to thedataobject, like you normally would. I suspect this is because when in background,Gcmautomatically handles and displays the notification, while when the app is in foreground,react-native-notificationshandles the notification.This is how the payload should look:
This solves the problem mentioned at the beginning.
If you were to send only the
notificationobject, you would have the following issues:If you were to send only the
dataobject containing the notification fields: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
dataandnotificationwith the payload, which in this case is the solution.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”);
}
@d4vidi From what I understand, the problem from #38 was the fact that both
dataandnotificationwere sent along with the request, while the solution was sending only thedataobject with all the notification options inside it.In my case, I only send the
notificationpayload, withoutdata. So I don’t think I have the same problem. If I apply the suggested changes and usedatainstead ofnotification, the device obviously doesn’t display any kind of notification at all. GCM’s docs even state that the client is responsible for processingdatamessages, while thenotificationmessages 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.
@arnebr @andreipham Please have a look at issue #38 (more specifically, refer to this link: https://developers.google.com/cloud-messaging/concept-options#target)
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.