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

假装没事ソ 提交于 2019-12-22 04:09:50

问题


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:

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


回答1:


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.




回答2:


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




回答3:


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




回答4:


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




回答5:


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


来源:https://stackoverflow.com/questions/41329990/send-notification-from-the-server-to-user-segment-who-installed-the-app

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!