Unable to send data message using firebase console - android

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.

Related

Sending Firebase push notification to topic directly from device using FirebaseMessaging.getInstance()

Can i send push notification to topic using FirebaseMessaging.getInstance().send ?
There are a lot of answers describing how to do this using https request. It's clear, but I don't want to use large block of code, building JSON request body, adding custom headers with my key, etc.
So, what I have tried:
At first - FirebaseMessaging.getInstance().subscribeToTopic("test")
then
RemoteMessage rm = new RemoteMessage.Builder("test")
.addData("message", "Hello")
.build();
FirebaseMessaging.getInstance().send(rm);
where test is my topic.
Result: no message received. But when I send this JSON via postman:
{"to": "/topics/test",
"data": {
"message": "Hello",
}
}
everything is OK and i receive notification on my phone.
So, do FirebaseMessaging.getInstance().send support sending topic messages and how do i need to configure RemoteMessage?

Firebase Cloud Message Sending Notification through phone

I figured how to send and receive Firebase Push notifications using Firebase Cloud Messaging
I want to know how to send a notification manually using code in android studio so I can send it from any phone.
You send messages from your phone using FCM. You need to make a POST to https://fcm.googleapis.com/fcm/send api with payload that you want to send, and you server key found in Firebase Console project.
As an exameple of payload sending to a single user with to param:
{ "data": {
"score": "5x1",
"time": "15:10"
},
"to" : "bk3RNwTe3H0:CI2k_HHwgIpoDKCIZvvDMExUdFQ3P1..."
}
Also, to param can be used for topics "to": "topics/yourTopic"
In data you can send whatever you want, message is received in onMessageReceived() service from Firebase.
More details you can found in Firebase documentation.

Android FCM Push notification is NOT delivered

I sent the following notification json request to FCM gateway:
https://fcm.googleapis.com/fcm/send
And the following is the json request:
{ "data": {"body": "Nice to meet you!"},"to" : "dWgg2uGMlrs:APA91bFWhoMvV2WIZrYlENUqHzP0J2fXTuBGo-FiFd-YwGUT6vqyTjeGiOi28rOnU6MQggDwziQ7Xwg4mw6Fbnjo4-OqfOKsfw1M4E6w2rRxc0yQyGbKhQEGpIGC2eIc2CACYrEudsxz","priority" : "high"}
And the following is FCM gateway response
{"multicast_id":7757558437981419128,"success":1,"failure":0,"canonical_ids":0,"results":[{"message_id":"0:1489893859089718%f58ec05df9fd7ecd"}]}
However, my my phone is not able to receive the notification. If I use the exact same device token dWgg2uGMlrs:APA91bFWhoMvV2WIZrYlENUqHzP0J2fXTuBGo-FiFd-YwGUT6vqyTjeGiOi28rOnU6MQggDwziQ7Xwg4mw6Fbnjo4-OqfOKsfw1M4E6w2rRxc0yQyGbKhQEGpIGC2eIc2CACYrEudsxz and send notification in FCM web console https://console.firebase.google.com
I am able to receive the notification.
Why?
Never mind. The request and response are totally valid. There was a bug in my android app. Issue closed.

Android gcm notification payload vs data payload?

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

How to post notifications to GCM

let me explain my requirement, I want to send a notification to my users on their(Android) when any changes happen on table.
Using SAP NW Gateway I could able to send notification to any listener(listener class)when my table record updated/deleted.
Now I want to send this notification to users android devices,I have seen GCM but not able to understand how can I post my notification received to GCM and send that message from GCM to Android devices.
Please let me know if I can post my notification details to GCM how to send(read the sent message over GCM) to devices.
Notifications(using SAP Gateway) I am receiving is in XML format.
Thanks
Rajesh
To send a message, the application server issues a POST request to https://android.googleapis.com/gcm/send
Content-Type:application/json
Authorization:key=AIzaSyB-1uEai2WiUapxCs2Q0GZYzPu7Udno5aA
{
"registration_ids" : ["APA91bHun4MxP5egoKMwt2KZFBaFUH-1RYqx..."],
"data" : {
...
},
}
Contents of data lands on connected GCM clients. Of course, you must first configure GCM API in your google developer's console.
https://developer.android.com/google/gcm/server.html
https://developer.android.com/google/gcm/http.html

Categories

Resources