Send notification from the server to user segment

2019-04-21 16:26发布

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

5条回答
Ridiculous、
2楼-- · 2019-04-21 16:39

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());
    }
查看更多
唯我独甜
3楼-- · 2019-04-21 16:41

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

查看更多
Rolldiameter
4楼-- · 2019-04-21 16:43

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

查看更多
地球回转人心会变
5楼-- · 2019-04-21 16:47

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.

查看更多
迷人小祖宗
6楼-- · 2019-04-21 16:51

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

查看更多
登录 后发表回答