Send a notification from the server to the user segment that the application installed

In the Firebase console, I saw the ability to send a notification to a Segement user with the application "com.example" (where com.example is the name of the application).

As it shown on the picture:

enter image description here

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

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

+5
source share
6 answers

You really need to send a message to the topic. All participants who subscribe to the topic will receive your message.

Just check the link ..

https://developers.google.com/cloud-messaging/topic-messaging

+1
source

Unfortunately, it is not possible to send messages to user segments using the REST FCM API .

Alternatively, you will have to use other methods to send messages to several devices , for example, simply using the registration_ids parameter and the Messages topic (which I think is most preferable for your use case).

Here are examples of how to send this using Postman or cURL .

+1
source

I found a solution u can subscribe to your application on a specific topic, for example, your application package name in your FirebaseInstanceIdService class so that you can send data massage, for example

  { "to" : "/topics/your_package_name", "data" : { "key1" : "value 1", "key2": "value 2", ... } } 

here is the code to subscribe to your application in a topic in the 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

+1
source

Make an email call https://fcm.googleapis.com/fcm/send with the following parameters: -

Headers: -

Content-Type - Applications / 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, whenever you need to send a notification, you have to call this POST method on your part, and your application will automatically receive a call in OnMessageReceived. You can treat it like: -

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

Sign your users by OS

topic: "android" for android user

topic: "iOS" for an iOS user

(or any other name you want)

and then post on this subject ...

0
source

You need to keep in mind a few things. You must attach your headers first:

 Content-Type--application/json Authorization--key={your server key with key= in the value.Don't miss key= here} 

Following 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. There 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 process the notification both in the foreground and in the background. In the notification type, the background notification will be processed in the notification tray. You can see the link.

Then you have an array of json strings in which you have to send all the tokens of the users to whom you want to send notifications. You can add additional features that you can see in the firebase documentation. This is the main implementation for you.Hope it helps.

-1
source

Source: https://habr.com/ru/post/1261901/


All Articles