I'm using Firebase Cloud Messaging for Android. When my app is in the foreground, FCM will invoke onMessageReceived on my app's FirebaseMessagingService subclass.
When my app is in the background, the Android OS will create a default notification entry in the system tray. That notification entry looks pretty good to me; for the notifications I need to send, I don't particularly need to interrupt the user with the notification. The default notification in the system tray is just fine.
My question is, how do I make that "default" notification happen in onMessageReceived when my app is in the foreground? Is there a way to say, "I don't need to intercept this notification; please just do what you'd normally do if I were in the background"?
(Do I have to simulate it by hand with NotificationCompat.Builder? If so, which settings do I need to pass to get default behavior?)
When the app is in the background, your notifications are processed by the Google Services, which takes care of displaying your notifications as required, including the default click action (opening the app) and the notification icon.
When the app is in the foreground, the received messages are processed by the app.
Yes, you will have to mimic the NotificationCompat.Builder to look like default. There is no other way to do this without intercepting with onMessageReceived() callback.
Reference: https://firebase.google.com/docs/cloud-messaging/android/receive
Related
In Firebase push notifications, the payload can be of type "notification" or "payload", but they arrive (or not) depending on whether the app is in background or not and other details. Please clarify them.
(This answer focuses on Android devices)
Firebase Cloud Messaging (FCM) push notifications can be of three types : notification, data and notification+data.
Notification messages are meant to be received by the Android operating system itself, with no intervention by the app. When received by Android, they will be shown as a notification in the tray. Some details:
The tray notification will not be shown if received when your app is in the foreground.
You can implement a FirebaseMessagingService (see the data payload for more info on this), which will receive the message if your app is in the foreground. In your FirebaseMessagingService, you can show a tray notification yourself (or do whatever you want) when you receive the message.
When sending the message, you can specify what happens when the user clicks on the notification; this can be controlled by either specifying an activity in the click_action Android-specific option (see this) or by specifying an URL in the link property and having your app configure an intent filter associated with the URL you specified.
Data messages are meant to be received by an Android service of your app. This service can, in principle (see below [*]), receive the messages when your app is in the foreground, in the background, or not running at all. Some details:
To implement the service, you have to extend FirebaseMessagingService and configure it in your app's manifest.
When you receive the message in your FirebaseMessagingService, you can decide to emit a local notification to be shown in the tray. You can do this either when your app is in the background or in the foreground, in principle (see below [*]). Of course, you may also decide to do other stuff instead (or apart) of showing the tray notification.
[*] Some phone manufacturers, especially Chinese ones like Xiaomi and Oppo, implement some mechanisms to save battery that include killing services. This means that, by default, your FirebaseMessagingService will not be running on those phones unless your app is on the foreground and, therefore, it will NOT receive your data payloads when your app is not on the foreground. There is no way around this, except if the user whitelists your app specifically. The famous apps like Whatapp or Gmail are by default included in the whitelist, but yours won't; therefore, if you rely on data payloads and you want your app to work on that kind of phones, you'd better guide your user to configure their phone to allow it; here you can see how to do it for Xiaomi (Miui) devices. This can also happen in vanilla Android devices since Android 9 (API level 28) with background restrictions, but the behaviour is opposite: your service won't be killed unless the user requests it; you can check this with ActivityManager.isBackgroundRestricted
Notification + data messages include both types of payloads. They behave exactly like notification payload-only messages:
When your app is in background, Android shows the notification in the tray. The data payload is accessible to the app if it receives the intent invocation when the user clicks (described above) in intent.extras.
When your app is in foreground, your FirebaseMessagingService receives the notification with the contents of the data payload.
I have implemented a service which extends FirebaseMessagingService and i am using it when the app is on the foreground to the received notification with my custom in-app notification view. And when the app is in background, as all other FCM releated topics and documentation here suggests, it is handled by the system tray, i only need to get the data from the bundle on the launched activity and make my redirections etc. on there.
What i also need to do is, when a notification is received and the app is launched by tapping on the launcher icon, not by notification. I need to get the notification data in that case and again make my redirections accordingly. But i couldn't able to get the attached data in that case. Any idea how to get the notification data on regular app launch?
Okay, this is how i solved my issue for above case, it is more like a workaround than a solution, but maybe someone might find it useful for his/her own purposes:
If the app was in the background when the notification is received and it is launched from the app icon but not notification, then i catch it inside the handleIntent method of the service that extends FirebaseMessagingService to store the received data on my local.
If the app was in the background when the notification is received and it is launched from the notification but not from the app icon, then i get the notification data from the getIntent().getExtras() of SplasActivity (which is my LAUNCHER activity)
If the app was in the foreground when the notification is received, the i show it as an in-app notification without storing anything to my local
And i delete the stored data, after i show the incoming message as in-app notification on my next app launch either by notification or from app launcher icon.
There is definetely downsides of this implementation, but like i said it is more like workaround than a solution. I guess a proper solution should send the data as a data message as it is described in this post. But unfortunately that was not an option for me.
I have an android app connected to FCM(firebase cloud messaging) for sending notifications . However , when the app is in background , it doesn't apply
custom notifications. I think this is because it does not enter OnMessageReceived method . Whats the solution for this ? As i read somewhere it can be done using HTTP Api but i am not sure of how to go about it ? Please help.Thank you.
Whether the method onMessageReceived is called is dependent on the notification payload.
See 'Handling Messages' section in FCM docs here.
Handling messages
onMessageReceived is provided for most message types, with the following exceptions:
Notification messages delivered when your app is in the background. In this case, the notification is delivered to the device’s system tray. A user tap on a notification opens the app launcher by default.
Messages with both notification and data payload, both background and foreground. In this case, the notification is delivered to the device’s system tray, and the data payload is delivered in the extras of the intent of your launcher Activity.
In summary, if the app is in foreground, then onMessageReceived will always be called. If the app is in background, then onMessageReceived will only be called if the notification payload is data only.
So to answer your question, if you must have onMessageReceived every time regardless if the app is in foreground or background state, then set your notification payload to contain data only.
I implemented firebase into my app to use it's notification service a while back and it worked, and today decided to use it's key/value feature, but realized that no matter what I write in my class inherited from FirebaseMessagingService, nothing happens. So I decided to remove the my class for dubugging purposes, and I still got notifications. So I removed the firebase messaging service from the manifest, deleted the app cache and ran the project, but I still get notifications! The only way I can prevent notifications from coming is to remove the firebase dependency in gradle. What's happening here?
Just read the documentation.
...
Ok, let's go: there are three kind of "notification" using Firebase: notification messages, data messages and messages with both notification and data. Each is received and handled differently by the system. Here you can see more information about message types (how there are built).
There are handled like that:
Notification message
App in foreground: onMessageReceived's implementation
App in background: system tray (system dispatch automatically notification) -> with or without service implementation (maybe your case, without code I can't know)
Data message
App in foreground: onMessageReceived's implementation
App in background: onMessageReceived's implementation
Message with both notification and data
App in foreground: onMessageReceived's implementation
App in background: system tray (notification) and in extras of the intent (data)
But without any code or notification information, I can't continue to help you. Until you provide some code example, I only think that you send a notification message, and so system automatically display a notification and there is no call to the service's onMessageReceived, it's normal.
If you want to "control" and decide if notification should be shown or not, just send data messages, and in your onMessageReceived implementation, create a notification with content and intent and notify it to the system (if you want it to be shown).
I am able to group notifications for my app when it is in foreground. Whenever the app goes in background it creates multiple notification. How can I fix this?
As google says in this site, when app is in background notifications are administrated by the system by default :
Notifications delivered when your app is in the background. In this
case, the notification is delivered to the device’s system tray. A
user tap on a notification opens the app launcher by default.
But if you want to work with the notifications by "yourself" you should send the 'data message' instead of the 'notification' as is said here and you can handle the notification.