In Firebase console, I saw the option to send a notification to User Segement with app "com.example" (where com.example is the app name).
As the image shows:
But how to do it from the server side using the FCM REST API:
In Firebase console, I saw the option to send a notification to User Segement with app "com.example" (where com.example is the app name).
As the image shows:
But how to do it from the server side using the FCM REST API:
Unfortunately, it is not possible to send messages to User Segments using the FCM REST API.
As an alternative, you'll have to make use of the other ways to send messages to multiple devices, like simply using the registration_ids
parameter and Topics Messaging (which I think is the most preferable for your use-case).
Here are samples on how to send this using Postman or cURL.
i found a solution u can subscribe your app to a specific topic for example your app package name in your FirebaseInstanceIdService class so u can send data massage like
{ "to" : "/topics/your_package_name", "data" : { "key1" : "value 1", "key2": "value 2", ... } }
here is the code to subscribe your app to topic in FirebaseInstanceIdService class
public class MyFirebaseInstanceIDService extends FirebaseInstanceIdService { private final String TAG="your_tag"; @Override public void onTokenRefresh() { // Get updated InstanceID token. String refreshedToken = FirebaseInstanceId.getInstance().getToken(); FirebaseMessaging.getInstance().subscribeToTopic("your_app_package_name"); } }
its worked for me
You actually need to send a message to the topic .. All the members subscribed to a topic will get your message ..
Just check out the link ..
https://developers.google.com/cloud-messaging/topic-messaging
Make a post call to https://fcm.googleapis.com/fcm/send with following parameters:-
Headers:-
Content-Type--application/json
Authorization--key={your server key}
Body:-
{ "data": { "my_custom_key" : "my_custom_value", "message" : "notification message" }, "registration_ids": ["device_token1,device_token2,.........."] }
EDIT:-
Basically what you need to do is ,whenever u need to send notification you have to call this POST method from your server side and your app will automatically get a call in OnMessageReceived . You can handle that as:-
@Override public void onMessageReceived(RemoteMessage remoteMessage) { super.onMessageReceived(remoteMessage); // TODO: Handle FCM messages here. // If the application is in the foreground handle both data and notification messages here. // Also if you intend on generating your own notifications as a result of a received FCM // message, here is where that should be initiated. Log.d(TAG, "From: " + remoteMessage.getFrom()); Map<String, String> data=remoteMessage.getData(); Log.d(TAG, "From: " + data.toString()); String value=data.get("my_custom_key"); Log.d(TAG, "From: " + value); String msg=data.get("message"); Log.d(TAG, "From: " + msg); sendNotification(msg,value,remoteMessage.getSentTime()); }
Subscribe your users depending on the OS
topic: "android" for android user
topic: "iOS" for iOS user
(or whatever name you want)
and then send to that topic...
You will have to keep in mind a few things. First you have to attach your headers:
Content-Type--application/json Authorization--key={your server key with key= in the value.Don't miss key= here}
The next thing is the body:
{ "data": { "my_custom_key" : "chat", "message" : "chat msg", "priority":"high" }, "registration_ids": ["ids of the users"] }
This is the body of your request.Their are two types: data and notification.You can use either of them or both.You can understand the difference from this link. Basically, if you send data,you will have to handle the notification on both foreground and background phase.In the notification type the background notifications will be handled by notification tray.Rest you can look into the link.
Then you have a json string array in which you will have to send all the tokens of users to whom you want to send notifications.You can add more features which you can see in the firebase documentation.This is the basic implementation for you.Hope this helps