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. :)
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 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.
I tried a sample code to test the push notification system with Firebase and it's working well except one thing.
If I try to send a notification from Firebase console, using Device Token, the notification shows in device.
If I try to send a notification from Firebase console, using my topic topik, all notifications show in all devices.
If I try to send a notification from my web page or from postman, using Device Token, the notification shows in device.
If I try to send a notification from my web page or from postman, using my topic topik, NOTHING HAPPENS.
This is a example call:
link: https://fcm.googleapis.com/fcm/send
POST method
Header field:
Content-Type : application/json
Authorization : key=MY_SERVER_KEY (the new one)
Body:
{
"to": "/topics/topik",
"data": {
"title": "This is a Firebase Cloud Messaging Topic Message!",
"content-text": "This is a Firebase Cloud Messaging Topic Message!"
}
}
or
Body:
{
"to": "/topics/topik",
"data": {
"message": "This is a Firebase Cloud Messaging Topic Message!"
}
}
the result on send action is something like this
{
"message_id": 7150560334538835864 (SUCCESS!)
}
but no notification arrives in any device. I tried to debug the onReceive method, but nothing happens.
Any idea?
Are you trying to send data-messages or notification-messages?
see: https://firebase.google.com/docs/cloud-messaging/concept-options#notifications_and_data_messages
If you want to send notification-messages (the type of messages sent by the notification console)
the payload you wrote in the question is wrong. Try replacing data with notification:
Body:
{
"to": "/topics/topik",
"notification": {
"title": "Hello",
"body": "This is a Firebase Cloud Messaging Topic Message!"
}
}
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 };
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.