17

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

AL.
  • 36,815
  • 10
  • 142
  • 281
Santhosh
  • 9,965
  • 20
  • 103
  • 243

5 Answers5

9

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.

Graham
  • 7,431
  • 18
  • 59
  • 84
AL.
  • 36,815
  • 10
  • 142
  • 281
  • I cant ask my members to subscribe to topic. Whoever installs the app will start getting the notification – – Santhosh Dec 27 '16 at 05:13
  • I get it @SanthoshYedidi - You should just go ahead and make use of `registration_ids`. If you have more than a thousand users, you'll have to make batch requests, each having a max of 1000 registration tokens each. – AL. Dec 27 '16 at 05:15
  • Very old answer, but for future reference: Topic subscription can be done without user interaction. Just make a POST via http to `https://iid.googleapis.com/iid/v1/[REGISTRATIONTOKEN]/rel/topics/[TOPIC]` (authorized with your server key). – Brian Sep 27 '18 at 22:20
  • @Brian The post was about using the native User Segments in Firebase. But sure using topics is a workaround if you know the actual segments and the users that must subscribe to it. – AL. Sep 28 '18 at 05:10
  • 1
    @AL.: I was responding to santhosh's that he cannot ask his members to subscribe to a topic, which I took to mean that Santhosh thought topic subscription had to be done client-side. – Brian Oct 01 '18 at 13:06
6

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

0

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

Hobbit
  • 601
  • 1
  • 9
  • 22
0

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

ruben
  • 109
  • 2
  • 10
-1

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());
    }
Nainal
  • 1,728
  • 14
  • 27
  • 1
    `"registration_ids": ["device_token1,device_token2,.........."]`: I want to send to any user who installed the app. Refer the updated question. From firebase console using user segment option i can send without need of device token. Because my notifications are generic – Santhosh Dec 26 '16 at 11:28
  • when user installs your app , a refresh token is generated. send this token to your server , this is device token – Nainal Dec 26 '16 at 11:42
  • and you can save the tokens in your server – Nainal Dec 26 '16 at 11:42
  • From Firebase console, you can only send notification to user when app is in foreground. To send notification in background or when app is closed, u need to have a post call – Nainal Dec 26 '16 at 11:44
  • yes, thats the android part.But i am looking how to send to user-segment from the server side. I have implemented the foreground part already. the notifications are getting recieved. But till now i using firebase console. NOw i want to shift to a server from where i want to send notification – Santhosh Dec 26 '16 at 11:47
  • if your backend is in php then you can refer to https://www.simplifiedcoding.net/android-push-notification-tutorial-using-firebase/ – Nainal Dec 26 '16 at 12:07
  • @santhosh one think i can think is that silently register the user for a topic say "generic" when they install the app.so all your user's will be subscribed to it.and then send your generic notifications to this topic – ashif-ismail Jul 13 '17 at 05:17