firebase / quickstart-cpp

Firebase Quickstart Samples for C++
https://firebase.google.com/games
Apache License 2.0
206 stars 122 forks source link

Android FCM not waking up #40

Closed dangvohoanganhvn closed 4 years ago

dangvohoanganhvn commented 4 years ago

Hi team, I'm using Firebase C++ SDK quick start messaging to get familiar with Android FCM. After I set up completely, Although I can receive notifications from server, quick start messaging app did not wake up. How can I solve this problem. Thanks.

Platform: Android (built by MAC OS) Device: Samsung J7 Firebase C++ SDK version: 6.8

google-oss-bot commented 4 years ago

This issue does not seem to follow the issue template. Make sure you provide all the required information.

cynthiajoan commented 4 years ago

@dangvohoanganhvn could you please try using sample app to see if it has the same behavior?

If you are actually using the sample app, could you describe a bit more of what's the expected behavior and what you actually see happening? Thanks!

dangvohoanganhvn commented 4 years ago

@cynthiajoan Actually, I'm using sample app. The Logs in loop "while (listener.PollMessage(&message))" just printed on Logcat when I start the app. Is there some C++ API that will be called when my device receive a notification ? I tested Java code and it worked well (see in the attached picture) , the function onMessageReceived will be called when my device receive a notification.Thanks!

image

chkuang-g commented 4 years ago

Hi @dangvohoanganhvn

Although I can receive notifications from server, quick start messaging app did not wake up.

I believe what you meant was: when you put the app in the background, not killed the app, you can still received the push notification. However, the app did not "wake up" and the listener you added through C++ API was not triggered. Is that what you mean?

TL; DR; If so, I think it is an intended behavior.

In your scenario, you should still receive cached ::firebase::messaging::Listener::OnMessage() event from C++ SDK once you relaunch the app or put the app back to foreground, but with a catch. C++ OnMessage() will not be called if the message contains notification data, since the notification data is passed to your app, which is a Android activity, through Intent, when you click on the notification in Android system tray. A bit more information from here about how messages are handled in different cases. However, you can override Android Activity to forward the message from the Intent to our customized ListenerService in order to trigger OnMessage() function.

If you need to process the message immediately when the device receive a message, the only way is thru what you are doing right now, override a ListenerService in Java. Here is more detail about it. And again, ListenerService will not receive messages with notification data, as described above. Also, ListenerService is an Android service, which is different from your app, which is an Android activity. You cannot wake your app from a service, AFAIK. And the default behavior of this ListenerService would actually cache received message and makes them available when your app is back to foreground again.

This is just a result of the design of Android SDK, which make the event only available in a service, and the design of C++ SDK, where we want the event to be available in an app, e.g. an Android Activity.

In conclusion, if you don't need to process the message immediately, but still want to trigger the C++ event when the user clicks on the notification from the system tray, the most general solution is by, again, modifying Activity.

Hope this helps!

Shawn

dangvohoanganhvn commented 4 years ago

Hi @dangvohoanganhvn

Although I can receive notifications from server, quick start messaging app did not wake up.

I believe what you means is: when you put the app in the background, not killed the app, you can still received the push notification. However, the app did not "wake up" and the listener you added through C++ API was not triggered. Is that what you mean?

TL; DR; If so, I think it is an intended behavior.

In your scenario, you should still receive cached ::firebase::messaging::Listener::OnMessage() event from C++ SDK once you relaunch the app or put the app back to foreground, but with a catch. C++ OnMessage() will not be called if the message contains notification data, since the notification data is passed to your app, which is a Android activity, through Intent, when you click on the notification in Android system tray. A bit more information from here about how messages are handled in different cases. However, you can override Android Activity to forward the message from the Intent to our customized ListenerService in order to trigger OnMessage() function.

If you need to process the message immediately when the device receive a message, the only way is thru what you are doing right now, override a ListenerService in Java. Here is more detail about it. And again, ListenerService will not receive messages with notification data, as described above. Also, ListenerService is an Android service, which is different from your app, which is an Android activity. You cannot wake your app from a service, AFAIK. And the default behavior of this ListenerService would actually cache received message and makes them available when your app is back to foreground again.

This is just a result of the design of Android SDK, which make the event only available a service, and the design of C++ SDK, where we want the event to be available in an app, e.g. an Android Activity.

In conclusion, if you don't need to process the message immediately, but still want to trigger the C++ event when the user clicks on the notification from the system tray, the most general solution is by, again, modifying Activity.

Hope this helps!

Shawn

Hi Shawn, Thanks for your help!

However, my expectation is that, when my application was killed ,it can receive the notification and call onMessage() C++ function without opening my application and user actions. What should I do? Please give me some helps.

chkuang-g commented 4 years ago

It is basically impossible by design.

When your app is killed or stay in the background for way too long, the only way to wake your app is through Intent. Take a look of this doc. That means you would need to implement your own FirebaseMessageService and start the activity with the Intent. From your app side, you would need to specify which Intent your app is listening to in AndroidManifest.xml. And you will need to process the Intent from your app when it is awake.

All these work still won't trigger OnMessage() function in C++.

We don't do this in C++ SDK because we do not want to launch the app everytime when the device receives a cloud message. Think about the chaos. 😂

May I learn why you need to process the message immediately even when the app is killed?

chkuang-g commented 4 years ago

correction

I think Android stop allowing you to start an activity from a service. That is in general a very bad idea to do so.

If you really need to do something about the message immediately, you can extend ListenerService or FirebaseMessageService directly, like what you did here. But you also run into the limited about different types of message..

google-oss-bot commented 4 years ago

Hey @dangvohoanganhvn. We need more information to resolve this issue but there hasn't been an update in 7 days. I'm marking the issue as stale and if there are no new updates in the next 3 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!

google-oss-bot commented 4 years ago

Since there haven't been any recent updates here, I am going to close this issue.

@dangvohoanganhvn if you're still experiencing this problem and want to continue the discussion just leave a comment here and we are happy to re-open this.