firebase / flutterfire

🔥 A collection of Firebase plugins for Flutter apps.
https://firebase.google.com/docs/flutter/setup
BSD 3-Clause "New" or "Revised" License
8.71k stars 3.97k forks source link

🐛[firebase_messaging] When app is killed and mobile phone lock screen, BackgroundMessage sometimes can't receive #10419

Closed niumj closed 1 year ago

niumj commented 1 year ago

Bug report

When app is killed and mobile phone lock screen, BackgroundMessage sometimes can't receive. device:Pixel3 When charging,send 20 notifications from the server , all notifications can be received. When uncharged state,send 20 notifications from the server ,but 5 to 15 random notifications can be received。

Anybody encountered the same problem?

firebase_messaging: ^14.2.2

darshankawar commented 1 year ago

@niumj

BackgroundMessage sometimes can't receive.

Is this only happening during device is not being charged ? And does not occur when the device is charging ?

Please use the plugin example and see if using the code in it, you still get same behavior or not.

google-oss-bot commented 1 year ago

Hey @niumj. We need more information to resolve this issue but there hasn't been an update in 7 weekdays. I'm marking the issue as stale and if there are no new updates in the next 7 days I will close it automatically.

If you have more information that will help us get to the bottom of this, just add a comment!

niumj commented 1 year ago

@darshankawar

@niumj

BackgroundMessage sometimes can't receive.

Is this only happening during device is not being charged ? And does not occur when the device is charging ?

Yes, only happening during device is not being charged. Based on test,it isn't happ during the device is charging. Any Android devive's permission is necessary? Thank you for your attention!

darshankawar commented 1 year ago

Any Android devive's permission is necessary?

Yes. It looks like an edge case as from your comments, it seems to be intermittent. Are you still able to consistently replicate the behavior everytime ?

google-oss-bot commented 1 year ago

Hey @niumj. We need more information to resolve this issue but there hasn't been an update in 7 weekdays. I'm marking the issue as stale and if there are no new updates in the next 7 days I will close it automatically.

If you have more information that will help us get to the bottom of this, just add a comment!

niumj commented 1 year ago

Yes. It looks like an edge case as from your comments, it seems to be intermittent. Are you still able to consistently replicate the behavior everytime ?

Yes, everytime when device is not being charged.

danagbemava-nc commented 1 year ago

Hi @niumj, can you provide a complete minimal code sample that we can use to investigate or does this reproduce with the plugin example app as well?

Also, are you by any chance restricting background activity for your app?

What version of android is your device running?

niumj commented 1 year ago

What version of android is your device running?

Pixel3 (Android 12)

Is any influence by "Optimize for Doze and App Standby"

danagbemava-nc commented 1 year ago

Hi @niumj, the mode you have set does restrict background activity. So there is the possibility that it's the reason why you're not getting any messages.

What priority are you using for the messages that you send?

niumj commented 1 year ago

Hi @niumj, the mode you have set does restrict background activity. So there is the possibility that it's the reason why you're not getting any messages.

@danagbemava-nc , Thank you for your attention. But what does the mode mean? Is that need to change code ? or because the user's settings restrict backgroud activity. Infact mobile phone didn't set any restrict background activity.

What priority are you using for the messages that you send?

Priority.HIGH

danagbemava-nc commented 1 year ago

Hi @niumj, you can find more information about what "optimize for doze and app standby" does in https://developer.android.com/training/monitoring-device-state/doze-standby

Priority.HIGH

Ideally, setting the priority to HIGH should make the messages display. How long have you been testing this? And are the notifications you have been sending resulting in the user taking some actions? According to https://firebase.google.com/docs/cloud-messaging/android/message-priority#using_high_priority_messages_for_android , the messages can be deprioritized if they do not result in user actions

niumj commented 1 year ago

Hi @danagbemava-nc , Thank you for your information. I add [[Manifest.permission.REQUEST_IGNORE_BATTERY_OPTIMIZATIONS] into AndroidManifest.xml and use plugin [android_power_manager] request the permission [REQUEST_IGNORE_BATTERY_OPTIMIZATIONS], but has the same problem

https://developer.android.com/training/monitoring-device-state/doze-standby

danagbemava-nc commented 1 year ago

Hi @niumj, I don't think you're supposed to use that permission unless your app meets a set of requirements.

Does your app meet the exemptions stated in https://developer.android.com/training/monitoring-device-state/doze-standby#exemption-cases?

Also, do the notifications that you receive prompt user action? Because if it does not, that means your messages have likely been deprioritized and will not wake the device.

niumj commented 1 year ago

@darshankawar Thank you for your information!

Does your app meet the exemptions stated in https://developer.android.com/training/monitoring-device-state/doze-standby#exemption-cases?

It looks like the app can't meets requirements.

danagbemava-nc commented 1 year ago

Hi @niumj, are you able to receive messages normally when you disable "Optimize for Doze and App Standby"?

niumj commented 1 year ago

Hi @danagbemava-nc , Yes, I can receive messages normally(Except lock screen). Background and foregroud both OK when screen on.

Infact, mobile phone disabled the "Optimize for Doze and App Standby". https://developer.android.com/training/monitoring-device-state/doze-standby#exemption-cases? [If a user leaves a device unplugged and stationary for a period of time, with the screen off, the device enters Doze mode]

danagbemava-nc commented 1 year ago

Hi @niumj, I have a feeling your messages are being deprioritized by fcm (which would be out of scope for this repo & plugin). Can you try to retrieve the priority of the notifications you're getting?

If your notification gets deprioritized, the original priority you give to the message does not take effect.

The RemoteNotification property has an AndroidNotification property which should contain the priority of the notification that you get

niumj commented 1 year ago

Hi @danagbemava-nc , AndroidNotification set the priority by androidNotifiBuilder. Here is my server's java code.

AndroidNotification.Builder androidNotifiBuilder=AndroidNotification.builder(); ... androidNotifiBuilder.setPriority(Priority.HIGH); ... AndroidNotification androidNotification=androidNotifiBuilder.build();
androidConfigBuilder.setNotification(androidNotification);

danagbemava-nc commented 1 year ago

Hi @niumj, see https://github.com/firebase/flutterfire/issues/9667#issuecomment-1429921118

Please also note that the priority you set on the backend is not always the same priority that the message will be delivered with.

When your phone is on charge, the device is not restricted in it's capabilities which is why you can receive all the messages. This is not the same when the device is optimizing for doze. It's trying to conserve battery power so it will not allow every notification to come through.

Closing this based on the comment in https://github.com/firebase/flutterfire/issues/9667#issuecomment-1429921118 and the reason above.

Thank you