Firebase allows us to send messages by accepting our post requests:
{ "data": {
"score": "5x1",
"time": "15:10"
},
"to" : "bk3RNwTe3H0:CI2k_HHwgIpoDKCIZvvDMExUdFQ3P1..."
}
However, all examples which are shown in their docs are for specific users. That requires their registration keys.
In Firebase Dashboard we can send messages to devices by choosing specific platform.
What kind of parameter is required in order to send message for specific application package?
for example: kz.mycompany.myapp
With Firebase Cloud Messaging, you have a few ways to address to whom messages are sent:
to a specific device
to a device group
to a topic
The Firebase Notifications tab in the new Firebase Console uses either #1 or #2 to send to groups of users.
But it looks like what you are looking for can be most easily accomplished by sending to a topic:
FirebaseMessaging messaging = FirebaseMessaging.getInstance()
messaging.subscribeToTopic("package_kz_mycompany_myapp");
Then your server-code can push messages to this topic to have it reach all devices that have subscribed to the topic:
var topic = '/topics/package_kz_mycompany_myapp';
var data = { "data": {
"score": "5x1",
"time": "15:10"
},
to : topic };
Related
I have a scenario in my app such that, a certain event occurs and I have a list of user-id/tokens and I need to send the notification to all of those n devices.
To trigger the fcm with n tokens , n time will not be feasible
so I should create a topic dynamically and subscribe those n users's device id/ token to that topic.
I know I can do it from the client app , but is it possible to do that from backend.
I am using Phoenix as my backend.
I found the way, writing this answer in case it help others in future
Yes Its possible to create a topic dynamically if we have the list of
valid registration tokens
This is the endpoint url if you want to generate a topic , given you have a list of users-
https://iid.googleapis.com/iid/v1:batchAdd
The Authorization header contains-
Content-Type- application/json
Authorization- key=<your-server-key>
The body parameters look like-
{
"to": "/topics/<topic name>",
"registration_tokens": [
"token1",
"token2"
]
}
And now the topic is created,
You can easily sen message to that topic with- https://fcm.googleapis.com/fcm/send
Authorization token is same as previous one
And body as-
{
"priority": "HIGH",
"notification": {
"title": "New Text Message",
"image": "https://firebase.google.com/images/social.png",
"body": "Hello how are you?"
},
"to": "/topics/<topic name>"
}
To trigger the fcm with n tokens , n time will not be feasible
Using topics does not inherently change how FCM message delivery works. When you use a topic, the Google servers keep a mapping of that topic to the subscribed tokens. So when you call the API to send a message to a topic, the Google servers fan-out from that topic to the tokens, and then deliver the message with the same infrastructure as when you call the API with the tokens yourself.
Since you already have the tokens, so it might be simpler to just send to them directly, rather than creating a one-off topic.
I have an app with Android and iOS users, subscribed to Firebase Clould Messaging topics. Some users have access to send a message to these topics. I make an http request to send the message. The body looks like this:
{
"to": "/topics/debug"
"notification" : {
"body" : "Hi there",
"title" : "Wow!",
"sound": "default"
},
"priority": "high"
"data": {
"title": "Hi there",
"body": "Wow",
}
}
For android, in order to send data message, I need to use the "data" key, so that onMessageReceived is called.
However, ios needs the "notification" key, so that even if the app is killed the message will display. But, whenever I add the "notification" key, the android devices don't call onMessageReceived, and it isn't treated as a data message.
How can I solve this issue, to make one request which is work for both devices. If I do two requests (one with "notification" for ios, and one with "data" for android, the android devices get two notifications (one from onMessageReceived, and one from the system. Can I stop the android devices from showing the message if its sent with just the "notification" key?
I hope I explained the issue well, since this is how I understand the problem I have.
Thanks
I'm not sure this counts as an answer, but I realised an easy workaround, that I prescribed different topics to android and ios users, and set different code appropriate for each one, instead of having one topic for all users.
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.
Im implented this GCM example into my Android application. When I trigger a message it will be received by all devices, which have installed my app.
Is it possible to address a single device by purpose (also with other libraries/technologies other than GCM)?
Is it possible to address a single device by purpose?
Yes, of course, it is possible.
There are two ways.
1. Using GCM Registration token :
You can target a single device using it's GCM registration token as mentioned
here.
{
"data": {
"score": "5x1",
"time": "15:10"
},
"to" : "bk3RNwTe3H0:CI2k_HHwgIpoDKCIZvvDMExUdFQ3P1..."
}
2. Using GCM Topics :
GCM topic messaging allows your app server to send a message to
multiple devices that have opted into a particular topic as mentioned
here.
Client Side :
private void subscribeTopics(String token) throws IOException {
GcmPubSub pubSub = GcmPubSub.getInstance(this);
pubSub.subscribe(token, "/topics/foo-bar", null);
}
Server Side :
{
"data": {
"message": "This is a GCM Topic Message!",
},
"to": "/topics/foo-bar"
}
For both the ways,
Add your target device's registration token string or topic name under to section of a message request.
Hope it helps. :)
As documentation says, I can send message with topic as
{
"to": "/topics/foo-bar",
"data": {
"message": "This is a GCM Topic Message!",
}
}
But to send message to specified device, i need to set to as device's push token as "to":"cvE0coiuUEg:APA91...". But in this case, how should I set topic?
They are different types of messaging. You want to use Device Group Messaging. In that case, you do not need to set a topic.