13

I am able to send the notification to single device, topics and user segment from Firebase Console.

I want to send the push notification to a user segment. I searched a lot but I'm only getting script to send notification to Single User or Topic not to user segments.

I tried the below code

var client = new RestClient("https://fcm.googleapis.com/fcm/send");
var request = new RestRequest(Method.POST);
request.AddHeader("content-type", "application/json");
request.AddHeader("authorization", "key=mykey");
request.AddParameter("application/json", "\n\n{\"to\" : \"user-segment-name\",\n\"notification\" : {\n    \"body\" : \"test message\",\n    \"title\" : \"Portugal vs. Denmark\"\n    },\n    \"priority\":\"high\"\n}", ParameterType.RequestBody);
IRestResponse response = client.Execute(request);
Response.Write(response.Content); 

And I am getting the response below

{"multicast_id":5837227475989925972,"success":0,"failure":1,"canonical_ids":0,"results":[{"error":"InvalidRegistration"}]}
Cœur
  • 32,421
  • 21
  • 173
  • 232
Hya
  • 189
  • 1
  • 2
  • 10

1 Answers1

14

Unfortunately, it seems that sending to user segments programatically still isn't possible. Reference here:

But you won't be able to

  1. send notifications to a Firebase User Identifier (UID) and

  2. send notifications to user segments (targeting properties & events like you can on the user console).

It's only possible via the Firebase Console.

Community
  • 1
  • 1
AL.
  • 33,241
  • 9
  • 119
  • 257
  • then the other option is topics, i used the above code to send message topics just setting "to" parameter to /topics/mytopicname. it gives me message _id but i am not receiving any push – Hya Sep 05 '16 at 09:12
  • @Hya That seems to be a different concern now. I suggest creating a different post, however, do look around the community, maybe there is already a duplicate or something similar to what you are experiencing. :) – AL. Sep 05 '16 at 09:15
  • ok i will do that! thanks for help! – Hya Sep 05 '16 at 09:19
  • 1
    Is there any way to send notifications to user segment without using console? – Learning Sep 28 '16 at 12:13
  • @Learning Depending on your use-case, you can either use Device Group Messaging or Topic Messaging. See [here](https://firebase.google.com/docs/cloud-messaging/android/send-multiple). Also, you must have the prerequisites to use them (the registration tokens to generate the notification id or register the device to topics). – AL. Sep 28 '16 at 12:22
  • Will check it out. Thank you! – Learning Sep 28 '16 at 12:25
  • @Learning Your welcome, you can just check this [answer](http://stackoverflow.com/a/39547590/4625829) for the basic description from the docs. Cheers – AL. Sep 28 '16 at 12:58
  • Actually I think creating topic is better to get it done. I need to add `FirebaseMessaging.getInstance().subscribeToTopic("TopicName");` this in my android project to get it work. But I can't update my android project again to work it out. Can I create topic for my app in firebase console itself? – Learning Sep 28 '16 at 13:05
  • @Learning see this [answer](http://stackoverflow.com/a/39382345/4625829), I've included all the links that may be useful for you. – AL. Sep 28 '16 at 13:14
  • I want an API to create a topic for my project. This http://stackoverflow.com/a/37376040/4539819 has that API explanation. But am not sure how to do it as it asks for registration token. – Learning Sep 28 '16 at 13:21
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/124423/discussion-between-al-and-learning). – AL. Sep 28 '16 at 13:26
  • setup user properties, and then from user properties create audiences. then you should be able to do that. – Andrei Chernyshev May 21 '19 at 20:12