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.
Related
I am using Firebase Cloud Messaging HTTP Protocol to send push notifications to my application using postman for testing.
I am using the following code to send the push.
{
"notification":{
"title":"Title",
"body":"this is a notification to a specific topic",
"sound":"default",
"click_action":"FCM_PLUGIN_ACTIVITY",
},
"data":{
"action":"ping"
},
"to":"/topics/Topic_1",
"priority":"high"
}
and I am using this code to handle the notification on my app:
FCMPlugin.onNotification(function(data){
console.log(data);
if(data.wasTapped){
//Notification was received on device tray and tapped by the user.
alert('notification tapped'+ JSON.stringify(data) );
}else{
//Notification was received in foreground. Maybe the user needs to be notified.
alert('application is open'+ JSON.stringify(data) );
}
});
it is all working except i cannot get the title and the body to use them in my application, all I am getting is the following:
Object {wasTapped: false, action: "ping"}
I cannot find a way to get the notification title and body.
I know I can copy them to the data section but that's not logical its a dirty workaround
so any idea how to get the notification data?
thank you.
Basing from the behavior shown, I'm presuming that the client platform is Android. If so, then this is working as expected.
When sending a combination of both notification and data in your message payload, the Android System tray will be the one to handle the values in notification. The dirty workaround you mentioned is the only workaround so far -- a workaround that I actually suggested on one of my answers as well.
I receive notifications when the app is closed and when it's open, but when it's closed and I click on the push notification that has a link it only opens the app and doesn't open the browser, and if the app is open and I click on the push notification the browser opens.
How can I open the browser when I click on a push notification that has a URL and when the app is closed.
An FCM with notification payload does not go through the onMessageReceived when your app is not running. It's posted by Google Play Services on your behalf directly to notification shade. Once clicked your main activity is launched and intent extras contain the data payload.
You have two options:
Continue using notification payload and handle the data in your main activity onCreate.
That's the activity started by launcher. Use this for information push notifications. If your app is not running it won't be started, which saves battery.
Don't use notification payload.
Use this for business logic push messages. This will always start your app process and let you handle the message in onMessageReceived.
Read more here:
https://firebase.google.com/docs/cloud-messaging/android/receive
https://firebase.google.com/docs/cloud-messaging/concept-options#notifications_and_data_messages
Use the below payload if you want to open the WEB URL while clicking on the FCM notification when the app is Killed or in the Background.
{
"to": "device-token",
"priority":"high",
"data":{
"body": "there",
"title": "Hi",
"LinkUrl": "https://www.google.com"
}
}
Note: I have not used "notification" in the above payload, if you use the below payload while using the notification keyword in your payload it will open the app not the URL,
{
"to": "device-token",
"priority":"high",
"notification":{
},
"data":{
"body": "there",
"title": "Hi",
"LinkUrl": "https://www.google.com"
}
}
Cheers!
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
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.
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.