Android gcm notification payload vs data payload? - android

According to
https://developers.google.com/cloud-messaging/server-ref
gcm message can have notification payload or/and data payload
What is the difference between these two?

Reading on the link you send it is explained under Payload section
Payload
Optional. If you are including a payload in the message, you use the
data parameter to include your custom key/value pairs. The client app
handles the data payload for display or other processing purposes.
The notification parameter with predefined options indicates that GCM
will display the message on the client app’s behalf if the client app
implements GCMListenerService on Android, or if the notification
message is sent to an iOS device. This applies for both HTTP and XMPP.
The app server can send a message including both notifications and
data payloads. In such cases, GCM handles displaying the notification
payload and the client app handles the data payload.
See the Server Reference for details on sending and receiving
messages.

You may find this explanation more helpful
Use scenario
Notification: GCM automatically displays the message to end user devices on behalf of the client app. Notifications have a pre-defined set of user-visible keys.
Data: Client app is responsible for processing data messages. Data messages have only custom key/value pairs.
How to send
Notification: Set notification payload. May have optional data payload. Always collapsible.
Data: Set data payload only. Can be either collapsible or non-collapsible.
https://developers.google.com/cloud-messaging/concept-options?hl=en

Related

Firebase display automatic notification even when all overrided

I use Firebase Cloud Messaging in my app, and I'm able to receive remote messages, with the data and everything I need from it. But even when I do nothing with the remote message, a notification appears automatically, with the content of the message.
public class MyFirebaseMessagingService extends FirebaseMessagingService {
#Override
public void onMessageReceived(RemoteMessage remoteMessage) {
// This line is executed, with the full message
}
}
With this code, the Android notification is still triggered, and I don't want it. I did not setup a default channel, and even on Android 8 (where I need to create a channel to send notification), there is one by default.
How can I remove this notification?
I want to display a notification only on my own terms, not on Firebase's ones
Don't use Notification message use data message instead.
in the Notification message, firebase will always show a notification when your app is in the background and there is no way to prevent it.
so the only way is to use data message and handle the notification by yourself.
from: https://firebase.google.com/docs/cloud-messaging/concept-options#notifications_and_data_messages
Notification message: FCM automatically displays the message to
end-user devices on behalf of the client app. Notification messages
have a predefined set of user-visible keys and an optional data
payload of custom key-value pairs.
Data message: Client app is responsible for processing data messages.
Data messages have only custom key-value pairs.
in all of my apps, I only use Notification message for very simple promotions because of lack of control and customization.

How to make a GCM / FCM notification-type message non-collapsible

Google Cloud Messaging (GCM) support two types of push messages: "notification" messages and "data" messages. According to the documentation, notification messages are collapsible by default, while data messages are non-collapsible by default.
In order to make a data message collapsible, you need to specifiy a collapseKey. My question is: How can you make a notification message non-collapsible?
Note: The question applies to Firebase Cloud Messaging (FCM) as well.
The message concepts and options documentation states:
messages are non-collapsible by default except for notification messages, which are always collapsible
But then later on the same page, it goes on to say:
except for notification messages, all messages are non-collapsible by default
Which is somewhat ambiguous. However, in the payload section, it states:
[notification messages] may have optional data payload. Always collapsible
Therefore, it doesn't seem possible to make notification messages non-collapsible.
I'd suggest that this is by design, because when creating notifications in Android, they are automatically replaced when another notification with the same ID is posted (similarly to how collapsing messages works). If I remember correctly, FCM/GCM uses the same ID for all notification messages.
Possible solution
If you do want a non-collapsible notification message, I'd suggest sending a data-only payload (with no notification or collapseKey), and then overriding the onMessageReceived() from the FirebaseMessagingService to create your own notification.
There is an example of this available on the Android quickstart sample:
#Override
public void onMessageReceived(RemoteMessage remoteMessage) {
// ...
// Check if message contains a data payload.
if (remoteMessage.getData().size() > 0) {
Log.d(TAG, "Message data payload: " + remoteMessage.getData());
// ...
}
// ...
// Also if you intend on generating your own notifications as a result of a received FCM
// message, here is where that should be initiated. See sendNotification method below.
}
The last comment there points you to the example sendNotification() method.
For your scenario, you'll need to pass a unique ID to the notificationManager.notify() call so that Android creates a new notification and does not replace any existing notifications - therefore, making the message non-collapsible.
First off, just going to put the FCM docs reference here:
A non-collapsible message denotes that each individual message is delivered to the device. A non-collapsible message delivers some useful content, as opposed to a collapsible message like a content-free "ping" to the mobile app to contact the server to fetch data.
As per your question -- "How can you make a notification message non-collapsible?" -- have you specifying a different collapse_key for each notification message you send?
A collapsible message works as if each of the message has the same collapse_key. But if you specify a different one for each message, then it won't be able to replace the previous one. This would make a notification message behave like a non-collapsible message.
However, since you are using a collapse_key, it is technically still treated as a collapsible message. Which means it is still a subject to the limit of four collapse keys at a time:
FCM allows a maximum of four different collapse keys per Android device to be used by the app server at any given time. In other words, the FCM server can simultaneously store four different collapsible messages per device, each with a different collapse key. If you exceed this number, FCM only keeps four collapse keys, with no guarantees about which ones are kept.
With all that said, any reason why you're not just using a data message payload? We use it on our app and I find it more flexible than a notification message.

Unable to send data message using firebase console

I'm using firebase console and can send only Notification messages using it.
Is there a way to send data messages using the same?
The Firebase Notifications Console can only be used to send notification messages. It cannot be used to send data messages.
See the table in message types in the Firebase documentation:
Notification message
Use scenario: FCM automatically displays the message to end-user devices on behalf of the client app. Notification messages have a predefined set of user-visible keys.
How to send:
Use your app server and FCM server API: Set the notification key. May have optional data payload. Always collapsible.
Use the Notifications console: Enter the Message Text, Title, etc., and send. Add optional data payload by providing Custom data in the Notifications console. Always collapsible.
Data message
Use scenario: Client app is responsible for processing data messages. Data messages have only custom key-value pairs.
How to send:
Use your app server and FCM server API: Set the data key only. Can be either collapsible or non-collapsible.
You can test both notification message and data message using Postman(rest client for testing http request).See screen shots:
In header pass:
key:Content-Type, value:application/json
key:Authorization:key=<Server key>
Please look here: Firebase push notifications update DB, my post from June.
In conclusion, you need send HTTP POST request to https://fcm.googleapis.com/fcm/send
https://fcm.googleapis.com/fcm/send
Content-Type:application/json
Authorization:key=AIzaSyZ-1u...0GBYzPu7Udno5aA
{ "data": {
"score": "5x1",
"time": "15:10"
},
"to" : "bk3RNwTe3H0:CI2k_HHwgIpoDKCIZvvDMExUdFQ3P1..."
}
You can now send notification message via the console. Note that it is different from data messages; notification messages only trigger the onMessageReceived callback when the app is in the foreground.
They are inside the advanced options tab on the compose message screen.
Just expand it and type your key/value map.
These will be included into the data field of the notification.

Getting Bad Request (400) on Azure Notification Hub's "Test Send"

I'm trying to send the following payload to a specific device (which is why I'm using the registration_ids) via Test Send on Azure's Notification Hub, but I'm getting a bad request error.
{
"registration_ids" : ["2670873167838193734-9582072870486547213-1"],
"data":{"message":"Hello World!"},
}
I'm not sure why since I checked the formatting and it looked fine to me based on other sources I saw.
Does anyone have an idea on what's happening?
EDIT:
Here's how I eventually managed to send a notification to a specific user:
on the client side (Java), I replaced the line
hub.register(regid)
with
hub.register(regid,"myTag")
Where "myTag" can be any other string tag you want. That way you can enter the word myTag in the 'Send to Tag' on Azure's Notification Hub and verify that you received the notification on your device.
Also, if you want to send the notification from the server side (C#), you'll need to add this header to your http request:
request.Headers.Add("ServiceBusNotification-Tags", "myTag");
Hope this helps anyone.
registration_id would not be part of the payload (see https://msdn.microsoft.com/en-us/library/azure/dn223273.aspx request body) although it is part of GCM's payload. To send to a specific device, you will need to tag that device with an id of sorts (e.g. registration_ids) and send to the tag "{registration_id}".
This is due to registration_ids field in the payload. Notification Hub does allow passing the registration_ids in the payload. Notification Hub internally substitute the registration id (before sending to GCM/FCM) based on selected devices.
https://msdn.microsoft.com/en-us/library/azure/dn530749.aspx
Thanks,
Sateesh

what does gcm.notification.e=1 stand for, into push notification payload on Android?

I'm writing an Android application which receives downstream messages from a server using Google Cloud Messaging. When receiving a message with onMessageReceived I print the bundle and read this:
RECEIVED PUSH NOTIFICATION: Bundle[{gcm.notification.e=1, gcm.notification.badge=1, gcm.notification.sound=default, ..etc...
I don't understand the key gcm.notification.e in the notification payload. On Google documentation I didn't find anything like that.
Moreover, when sending the message, I usually specify both a notification payload and a data payload. Now I tried to remove the notification payload but I still receive gcm.notification.e=1
Someone can help me understand this key and find a way to remove it?
Thanks
EDIT: I found out that sending a message without the notification payload and with content_available=false (I set this to true when working with iOS) then the gcm.notification.e=1 is not received. I receive only the data payload :). However, I still want to know the meaning of this e
Maybe this e field stands for enable. If you set it to zero in your server you won't receive the notification payload on Android. For more details se my answer here.

Categories

Resources