I know that there is the same question with this title but unfortunately it is not answered right and it is accepted!!! here
I want to know how I can find out a FCM message received when app is in background to do some action on message received before clicking by user. but when app is in background onMessageReceived is not triggered!
I googled so much and could not find a good way.
To handle FCM push notification from onMessageReceived() when the app in background the server should always send Data only messages.
Notification messages can only handle when the app is in the foreground. When the app is in the background an automatically generated notification is displayed. When the user taps on the notification they are returned to the app. Messages containing both notification and data payloads are treated as notification messages.
With FCM, you can send two types of messages to clients:
Notification messages, sometimes thought of as "display messages."
Data messages, which are handled by the client app.
A notification message is the more lightweight option, with a 2KB limit and a predefined set of user-visible keys. Data messages let developers send up to 4KB of custom key-value pairs. Notification messages can contain an optional data payload, which is delivered when users tap on the notification.
Notification messages
{
"to" : "bk3RNwTe3H0:CI2k_HHwgIpoDKCIZvvDMExUdFQ3P1...",
"notification" : {
"body" : "great match!",
"title" : "Portugal vs. Denmark",
"icon" : "myicon"
}
}
Notification messages are delivered to the notification tray when the app is in the background. For apps in the foreground, messages are handled by these callbacks:
onMessageReceived() on Android. The notification key in the data bundle contains the notification.
Data messages
{
"to" : "bk3RNwTe3H0:CI2k_HHwgIpoDKCIZvvDMExUdFQ3P1...",
"data" : {
"Nick" : "Mario",
"body" : "great match!",
"Room" : "PortugalVSDenmark"
},
}
On Android, a client app receives a data message in onMessageReceived() and can handle the key-value pairs accordingly.
Note these further platform-specific details:
On Android, the data payload can be retrieved in the Intent used to launch your activity.
Messages with both notification and data payloads
{
"to" : "APA91bHun4MxP5egoKMwt2KZFBaFUH-1RYqx...",
"notification" : {
"body" : "great match!",
"title" : "Portugal vs. Denmark",
"icon" : "myicon"
},
"data" : {
"Nick" : "Mario",
"Room" : "PortugalVSDenmark"
}
}
App behavior when receiving messages that include both notification and data payloads depends on whether the app is in the background or the foreground—essentially, whether or not it is active at the time of receipt.
When in the background, apps receive the notification payload in the notification tray, and only handle the data payload when the user taps on the notification.
When in the foreground, your app receives a message object with both payloads available.
Reference
About FCM messages
Handling received messages
As I've mentioned in the comments section, when sending a message with a notification message payload the Android System (Notification Tray) will handle the push notification when your app is in background.
You should use a data-only message payload, so that it will always be handled by onMessageReceived().
See the Handling Messages docs in Android for more details.
Handle notification messages in a backgrounded app
When your app is in the background, Android directs notification messages to the system tray. A user tap on the notification opens the app launcher by default.
This includes messages that contain both notification and data payload (and all messages sent from the Notifications console). In these cases, 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.
Related
I am very new to android app development and am wondering if there is a way to silently push a data message to a user's phone.
Basically, I have managed to set up Firebase notification on my app, and the app is able to receive notifications when the app is not running in the foreground. However, I want to be able to update some data in the user's phone without the user seeing a notification. Is this doable?
Thanks!!!
yes it is doable.You can send data by calling your api in async task without informing the user and update the data in application without informing the user.
But this will happen only when the application is in foreground.
2nd method is to use service and update your data even after the app is not in foreground.
I will personally prefer to use service.
To do that you need to send the message payload with data payload instead of notification to the firbase from your server where the notification payloads get generated.
{
"to" : "bk3RNwTe3H0:CI2k_HHwgIpoDKCIZvvDMExUdFQ3P1...",
"data" : {
"Nick" : "Mario",
"body" : "great match!",
"Room" : "PortugalVSDenmark"
},
}
So that you will receive the message in the Firebase service class inside onMessageReceived(). Once the data received then using key values you can get the data.
Refer the link to know more
https://firebase.google.com/docs/cloud-messaging/concept-options#data_messages
We think that Android’s push notifications by default are silent notifications and must be the developer who programmatically raise the notification on the screen. Or the push notifications aren’t silent by default?
Today we work using this JSON structure which our app receives from the GCM server.
{
"data":
{
“Type” : “2”,
“_dId” : “3718829”,
“_mId” : “9924012”,
“_msg” : “HOLA JVE”,
“collapse_key” : “9924012”
},
"to" : "xxxxxxxxxxxxxxxxxxxxxxxxxxx"
}
Is this structure valid for silent notifications or there is a different one ?
Truly we are pretty lost on this topic. Following this question, anyone have a json structure of an Android push notification with message and title tags ?
Other question : When a silent notification is shown to the user ? When the user open the app or just when the user unlock the phone ?
Or the push notifications aren’t silent by default?
There are two ways that a notification is received in Android, either it's Notification Tray, or you handle it yourself in onMessageReceived() depending on which payload you use (Notification or Data).
Basing from your inquiry (silent push notification), I'm guessing you'd prefer the latter, since you'll be able to handle it yourself. As per the GCM Payload docs:
Use notifications when you want GCM to handle displaying a notification on your client app’s behalf. Use data messages when you want your app to handle the display or process the messages on your Android client app,..
So what you are currently using right now, (a data payload) should be fine. However, if you have both notification and data in your payload, you will have to consider your app's status. Referring to this FCM docs, for data payload:
App state Notification Data Both
Foreground onMessageReceived onMessageReceived onMessageReceived
Background System tray onMessageReceived Notification: system tray
Data: in extras of the intent.
..anyone have a json structure of an Android push notification with message and title tags ?
As also mentioned in the docs I linked above:
Data messages have only custom key/value pairs.
So I think it's safe for you to just use keys so long as it's not a reserved word, as mentioned in this docs:
The key should not be a reserved word ("from" or any word starting with "google" or "gcm"). Do not use any of the words defined in this table (such as collapse_key).
When a silent notification is shown to the user ? When the user open the app or just when the user unlock the phone ?
I think what I mentioned above pretty much covers this part (see the table).
On Android, differently from iOS, the app is responsible for creating and showing the push notification. So yes, you can think of them as silent by default, although you don't exactly have this concept on Android.
What I normally do is, if the notification should not be shown, add a silent field. For example:
{
"to" : "bk3RNwTe3H0:CI2k_HHwgIpoDKCIZvvDMExUdFQ3P1...",
"notification" : {
"body" : "Will not show this message",
"title" : "Portugal vs. Denmark",
"icon" : "myicon"
"silent": "true"
}
}
And then you can use the silent field to show or not show the notification according to an if statement.
I don't understand very well how to send a notification to a user that receives an action. For example, when User A adds a comment or a like to User B, I want User B to receive a notification.
If User A adds a comment or a like, I can send a notification to each follower that is subscribed in the topic of User A in this mode. Topic subscription by followers:
FCMPlugin.subscribeToTopic('topicExample');
And I send a notification by the REST API by User A:
{
"notification":{
"title":"Notification title", //Any value
"body":"Notification body", //Any value
"sound":"default", //If you want notification sound
"click_action":"FCM_PLUGIN_ACTIVITY", //Must be present for Android
"icon":"fcm_push_icon" //White icon Android resource
},
"data":{
"param1":"value1", //Any data to be retrieved in the notification callback
"param2":"value2"
},
"to":"/topics/topicExample", //Topic or single device
"priority":"high", //If not set, notification won't be delivered on completely closed iOS app
"restricted_package_name":"" //Optional. Set for application filtering
}
But I don't understand how to send a notification to User B, because he is not subscribed to User A and is it possible to send the notification only to users subscribed to a certain topic?
When in the background, apps receive the notification payload in the notification tray, and only handle the data payload when the user taps on the notification.
Is there any way to handle the data payload without user tapping on the notification?
Basing from the FCM docs on Handling Messages:
App state Notification Data Both
Foreground onMessageReceived onMessageReceived onMessageReceived
Background System tray onMessageReceived Notification: system tray
Data: in extras of the intent.
It's not specifically handled ONLY by tapping on the Notification. You may handle it in the onMessageReceived(). Pretty sure the tap action also depends on how you implement it.
If you intend for it to do something else, do provide more details.
If you want to receive data payload when app is not running then you need to use a custom app-server with http protocol(in your case) which will send only data payload no notification and will send data to fcm endpoint like this
{ "data": { "some_key" : "some_value",
"some_more_key" : "some_more_value" },
"registration_ids": ["device-token","device-token2"] }
or just to send data from server to one client
{
"to" : "bk3RNwTe3H0:CI2k_HHwgIpoDKCIZvvDMExUdFQ3P1...",
"data" : {
"Nick" : "Test",
"body" : "Your message here",
"Room" : "Some Test Room"
},
}
Then using POST you can send downstream message to your device and it will work the way you want. If you want to have a basic idea of such app server please consider this : How to push notification to client when Firebase has a new entry?
I have posted basic Java Servlet code here and also a node.js code by firebase official blog.
Hope it helps.
Assume we have a football app, user have interests with team inter and milan.
but in the app, he only want to receive the push notification from inter.
Can we still sent out the push notification to user for both team inter and milan.
but do the filter work on client side to only display the message for inter?
On Android, if you are using Firebase to deliver notifications it depends what payload does your notification has.
Basically, there are two types of notification payloads: notification and data. Here is an example illustrating notification with both types of payload.
{
"to" : "APA91bHun4MxP5egoKMwt2KZFBaFUH-1RYqx...",
"notification" : {
"body" : "great match!",
"title" : "Portugal vs. Denmark",
"icon" : "myicon"
},
"data" : {
"Nick" : "Mario",
"Room" : "PortugalVSDenmark"
}
}
If your notification contains both types of payload or only "notification" type, there is no way to handle if notification gets displayed when app is in background.
If your application contains only "data" type of payload than you can handle if notification gets displayed no matter if application is in background or in foreground.