Santhosh
Santhosh

Reputation: 11824

Send notification from the server to user segment who installed the app

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:

enter image description here

But how to do it from the server side using the FCM REST API:

https://fcm.googleapis.com/fcm/send

Upvotes: 17

Views: 15433

Answers (5)

ehsan khormali
ehsan khormali

Reputation: 113

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

Upvotes: 6

ruben
ruben

Reputation: 119

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...

Upvotes: 0

AL.
AL.

Reputation: 37798

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.

Upvotes: 9

Nainal
Nainal

Reputation: 1748

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());
    }

Upvotes: -1

Hobbit
Hobbit

Reputation: 621

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

Upvotes: 0

Related Questions