Firebase message with high priority not waking device from Doze android 6+

浪尽此生 提交于 2019-11-28 17:52:11

There is nothing you can do.

This is a known issue that is caused by a battery optimization implemented by some OEMs (like Meizu or Asus). When an app is swiped away in the app switcher, the application is treated as if it were Force stopped, which is not the default Android behavoir. The unfortunate side effect of this is that it can cause the FCM service for your app to stop running. Similar effect can be caused on high priority messages in doze mode.

Firebase team is working to improve this behavior from their end but the actual fix has to come from OEM side.

One way to check if your app is affected by any OEM's battery management feature, is as below:

1) Attach the OEM device to adb

2) Run your app on the device

3) Swipe the app away from recent screen on the device

4) Run command: adb shell dumpsys package MY-PACKAGE | grep stopped

If it shows stopped=true, it's safe to assume that the OEM has such a mechanism and that your app is affected by the same.

Patric Eriksson

After struggling with a similar issue I managed to get it to work.

I send following json data through postman:

{
  "data": {
    "body": "Test body from curl"
  },
  "registration_ids": ["Token"],
  "webpush": {
    "headers": {
      "Urgency": "high"
    }
  },
  "android": {
    "priority": "high"
  },
  "priority": 10
}

It seems like the last "priority":10 is what's fixing it for me.

I could not find any reference to this in the Firebase documentation, but in the deprecated GCM documentation it's used. https://developers.google.com/cloud-messaging/concept-options

While working on an application, I am also stuck at this point. Then I found a issue about it on Github, which solved my problem. That is,

On devices running Android 6.0+, Doze mode terminates all background connections when the phone is idle and not being charged, including the background connection to Pushy.

As soon as the device is moved or awoken by the user, background connections are restored and any pending notifications will be delivered within seconds, providing they have not expired yet.

To send notifications to devices in Doze mode, your app can declare the REQUEST_IGNORE_BATTERY_OPTIMIZATIONS permission in its AndroidManifest.xml and display a system dialog that asks users to whitelist your app from battery optimizations without leaving the app.

This will effectively keep the background connection to Pushy active and devices will be able to receive notifications even in Doze mode.

You can check this issue here https://github.com/ToothlessGear/node-gcm/issues/231

Hope it helps you!

It looks like it is not possible to make high priority while sending only data field without notification. Here is the quote from documentation:

High priority messages generally should result in user interaction with your app or its notifications. If FCM detects a pattern in which they don't, your messages may be de-prioritized.

Setting a time_to_live of 0 solved the problem for me.

I think it's because a very small time_to_live will tell FCM that this message is only worth delivering right this instant. So in an attempt to deliver it ASAP, it will ignore battery optimizations like the Android P's "app standby buckets". Be careful though, as setting a small time_to_live might mean not delivering the notification at all in some cases. I don't think you should be applying it to all kinds of push notifications.

For more details about time_to_live: https://firebase.google.com/docs/cloud-messaging/concept-options#setting-the-priority-of-a-message

Instead of using "android":{"priority":"high"}, use like this

    {
      "time_to_live": 300000,
      "delay_while_idle": false,
      "data": {
               "message": "PING_DEVICE",
               "time": "21/01/2018 16:20:28",
               "pushguid": "10062"
               },
      "priority": "high"  
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!