FCM - Programmatically Send Push Notification To User Segments

14,530

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.

Share:
14,530
Hya
Author by

Hya

Updated on June 23, 2022

Comments

  • Hya
    Hya almost 2 years

    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"}]}
    
  • Hya
    Hya over 7 years
    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
  • AL.
    AL. over 7 years
    @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. :)
  • Hya
    Hya over 7 years
    ok i will do that! thanks for help!
  • Learning
    Learning over 7 years
    Is there any way to send notifications to user segment without using console?
  • AL.
    AL. over 7 years
    @Learning Depending on your use-case, you can either use Device Group Messaging or Topic Messaging. See here. Also, you must have the prerequisites to use them (the registration tokens to generate the notification id or register the device to topics).
  • Learning
    Learning over 7 years
    Will check it out. Thank you!
  • AL.
    AL. over 7 years
    @Learning Your welcome, you can just check this answer for the basic description from the docs. Cheers
  • Learning
    Learning over 7 years
    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?
  • AL.
    AL. over 7 years
    @Learning see this answer, I've included all the links that may be useful for you.
  • Learning
    Learning over 7 years
    I want an API to create a topic for my project. This stackoverflow.com/a/37376040/4539819 has that API explanation. But am not sure how to do it as it asks for registration token.
  • AL.
    AL. over 7 years
  • Andrei Chernyshev
    Andrei Chernyshev almost 5 years
    setup user properties, and then from user properties create audiences. then you should be able to do that.