Android alert notification messages are "High" priority by default.
Android data only notification messages are "Low" priority by default.
What priority is an alert notification, which also carries data (using content-available).
We require high priority notifications, however, it seems that when our App is in the background and a notification arrives, the Title/Body are consumed by the OS. This means that when the user selects the notification from the notification tray, this information is missing in the provided Intent.
We are therefore obliged to pass these (Title/Body) fields as data, with every high priority alert notification.
Does doing this affect the priority of the message, and, if it does affect it, what is the workaround?
Also, is there a way of discovering what priority was actually used for a received message?
On the sender side, you send a mixed message with the priority you decide. There is no impact.
The difference is just, that when a message contains data you get the callback in your FCMService, so your app wakes up.
If there is no data part, your app will not wake up before the user clicks the notification.
In general, this is the only question that matters: Do you want to wake up when the message arrives or just when the user clicks the notification?
If your message contains data, the OS will not post the notification. This part is up to you then.
We work with data-Message only as we want to have more control over the notification and we need to wake up when it arrives.
Related
Question
I have come across some voices stating that FCM data message delivery is less consistent compared to that of notification messages. Does anyone have direct experience or can point me to resources exploring the issue? Or is a notification message just a collapsible, high-priority data message that the Firebase SDK handles automatically?
https://stackoverflow.com/a/49998176
FCM data message not received in Android when the application is in background
About FCM's notification message and data message type, which has better receiving rate
The question does not consider the case of force quitting the app. In this scenario, both types of messages will not be delivered (to my knowledge).
Background
I am writing a new Android SDK for a push service provider (similar to OneSignal). The SDK should handle the display of push notifications by default, optionally the client app can handle incoming pushes itself.
The actual delivery is of course done by Firebase Cloud Messaging (on devices running Play Services). So there are 2 types of messages to choose from on FCM: data vs notification messages.
As data messages are consistently handled by the registered FirebaseMessagingService (provided there is no notification key in the payload), this should be the way to go for the SDK. [See documentation] So far, I have not been able to produce a situation in which a data message was not delivered (foreground or background).
By setting the priority in Message, We can reflect the delivery.
You have two options for assigning delivery priority to downstream messages on Android: normal and high priority. Delivery of normal and high priority messages works like this:
Normal priority. This is the default priority for data messages. Normal priority messages are delivered immediately when the device is not sleeping. When the device is in Doze mode, delivery may be delayed to conserve battery until the device exits doze. For less time-sensitive messages, such as notifications of new email, keeping your UI in sync, or syncing app data in the background, choose normal delivery priority.
When receiving a normal priority message on Android that requests a background data sync for your app, you can schedule a task with WorkManager to handle it when the network is available.
High priority. FCM attempts to deliver high priority messages immediately, allowing FCM to wake a sleeping device when necessary and to run some limited processing (including very limited network access). High priority messages generally should result in user interaction with your app or its notifications.
See Set and manage message priority topic in FireBase Documentation.
For More details you can prefer this link
For Displaying the Notification You Can Prefer this link
Best Of Luck In Advance.
I am currently trying to make a personal location application between 2 devices on Android.
The concept is simple: I install my application on my phone as well as on that of my wife and each can geolocate the other.
(This application is strictly personal)
To achieve this, I thought of using sending notifications by FCM.
Telephone A sends a request to telephone B which listens via a service for the reception of a message.
When phone B receives the request, it returns the GPS coordinates via FCM so that phone A displays them on a MAP.
(I also have the possibility to store the coordinates in a database instead of sending back an FCM message)
But FCM's documentation says:
"When your app is in the background, notification messages are displayed in the system tray, and onMessageReceived is not called. For notification messages with a data payload, the notification message is displayed in the system tray, and the data that was included with the notification message can be retrieved from the intent launched when the user taps on the notification."
Of course, this reduces the scope since it forces the user of the phone receiving the notification to click on it to activate the actions of the service.
Can FCM still meet my needs through another channel?
Are there other options to send a "request" to another phone?
(I know that this kind of application exists on the PlayStore, but I want to try to make mine :-))
The key word in that section from the documentation you quote is notification messages. Firebase Cloud Messaging supports two types of messages:
Notification messages, which are display by the system when the app is inactive - and delivered to your application code when the app is active.
Data messages, which are always delivered to your application code.
It sounds like you'll want to use data messages for this use-case
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.
We are choosing to use FCM's notification message or data message (only for custom designs). We already know that custom designs data message perform better than the non custom design data messages. But we are wondering if notification message can have better receive rate than data message because it is using the system to show the notification. Anyone has done any investigation?
If not, then what's the advantages the notification message type have
It's an interesting question and topic of research. I faced the same
the situation about a while ago.
According to firebase's documentation :
Notification messages are high priority by default, and
collapsible--the following message will replace the current message if
it's not delivered yet.
In the custom data type payload, you can actually pass the notification priority to high. But.....
According to this document from firebase
High priority. FCM attempts to deliver high priority messages
immediately, allowing the FCM service to wake a sleeping device when
necessary and to run some limited processing (including very limited
network access). 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.
Android P introduced app standby buckets which limit the number of FCM
high priority messages you can send to your app that don't result in
the user using your app or viewing a notification. If in response to
a high priority message, a notification is displayed in a way that is
visible to the user, then your app standby bucket quota will not be
consumed by that message.
And now let's talk about my personal experience. I faced the same
situation and the observations we noted with our QA were quite the
same as their documentation.
Although after setting priority HIGH in data type payload we noticed random behaviour with some custom OS phones like Oneplus, Oppo etc. While in the case of notification type payload it was consistent and we were getting notification perfectly.
So I advise that if you don't have a particular requirement to handle
the data silently in the background just go with a simple
notification payload.
When using FCM Notification Channels - it is no longer possible to change the Notification Sound when a notification arrives (using SetSound()). This is causing me a problem when my app receives a notification while the audio of a previous notification is playing. Our notification sound bites are 2-3 seconds long each, and when the second notification arrives, it cuts the first notification's audio off.
Instead of delaying the second notification from displaying, I would like for the second notification to display, but not play any audio. Is this possible?
I don't think you have that level of control over how Android displays/plays incoming notification messages.
The only approach I can think of is taking full control of the display of the messages in your own application code, by using data messages instead of notification messages.
Reminder: Firebase Cloud Messaging has two message types: notification messages, and data messages. Notifications messages are automatically handled/displayed by the OS when your app is not active, while data messages are always delivered to your application code.
From within your application code, you can then use the Android notification API to build the exact display of the message that you want, and display it exactly when you want it (within the notification settings of the user of course).