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?
Related
From python, I want to send a notification to all my android users who subscribed to a specific topic.
I enabled the fcm for my android app, and it works fine when I send a notification from the console to android.
Here's my code:
topic = 'new'
# See documentation on defining a message payload.
message = messaging.Message(
data={
'title': 'hahaha',
'body': 'test',
},
topic=topic,
)
# Send a message to the devices subscribed to the provided topic.
response = messaging.send(message)
# Response is a message ID string.
print('Successfully sent message:', response)
The problem is if I don't specify the topic in python (my back-end), the message sends, but when I specify the topic, it doesn't send, what could be wrong here? I'm not getting any error messages. The notification just doesn't appear on my device and when I send a message to devices subscribed to this topic from fcm console, it successfully sends.
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.
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.
Parse is shutting down and they've made their server opensource. However they do not have the extended functionality with push notifications as what used to be the case with parse.com
Can anyone help me set up push notifications on the open source version of Parse on android?
I've been through their wiki and I'm a tad bit confused about it.
If I'm not wrong, should I just add the GCM credentials to where the Parse Server is being initialized and then deploy it manually (possibly to heroku) myself and then use cURL to send notifications as per the wiki?
Or did I misunderstand the whole process and need to do something else?
Thanks in advance!
You can host your own Parser Server in a self-hosting solution or use a Parse Hosting provider like https://www.back4app.com
See all options below:
https://github.com/ParsePlatform/parse-server#parse-server-sample-application
Then you can send the push notifications using dashboard console, API or cloud code. Note that push notifications cannot be sent anymore from client. Because of security issues, Parse Server has discontinued sending push notifications direct from client. The best practice now is to create a cloud function to send the push notifications and then call it from client code. See more details below:
https://github.com/ParsePlatform/parse-server/wiki/Push#4-send-push-notifications
And here there is an example of a cloud function that can be used to send push notifications:
Parse.Cloud.define('push', function (request, response) {
// THIS METHOD NO LONGER WORKS
// Parse.Cloud.useMasterKey();
Parse.Push.send({
channels: request.params.channels,
data: request.params.data
}, {
// ADD THE `useMasterKey` TO THE OPTIONS OBJECT
useMasterKey: true,
success: function () {
response.success('Success!');
},
error: function (error) {
response.error('Error! ' + error.message);
}
});
});
You can also send the push notification throw Parse Dashboard. Parse has just announced this feature is now available:
http://blog.parse.com/announcements/push-and-config-come-to-the-parse-dashboard/
just fill the GCM field and the second field when you initialize Parse server (in the index.js or the ecosystem.json)... this will allow the server to send the Push for Android, for sending push U can use cloud code, curl or whatever. You need to use MasterKey though
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