How to send notification to specific users with FCM?

64,262

Did I need to create a web server

Yes. You need a place where you can map name/email to registration IDs. These registration IDs must be included in the request to FCM, for example

{
    'registration_ids': ['qrgqry34562456', '245346236ef'],
    'notification': {
        'body': '',
        'title': ''
    },
    'data': {

    }
}

will send the push to 'qrgqry34562456' and '245346236ef'.

The registration ID you use in the call is the one that's called 'token' in this callback in the app.

public class MyService extends FirebaseInstanceIdService {
    @Override
    public void onTokenRefresh() {
    }
}
Share:
64,262
Olcay Sönmez
Author by

Olcay Sönmez

Updated on July 09, 2022

Comments

  • Olcay Sönmez
    Olcay Sönmez almost 2 years

    I prepared the receiver for FCM and can send a notification to all devices.

    gcm-http.googleapis.com/gcm/send with this link can send to target users who is registered and post to the target devices like below json :

     {
         "notification": {
                    "title": "sample Title",
                    "text": "sample text"   },   
            "to" : "[registration id]"
             }
    

    However, I need to send notifications to target users which I choose, via email or name...etc . For example:

    {
         "notification": {
                    "title": "sample Title",
                    "text": "sample text"   },   
            "to" : "[email or name or sex ...]"
             }
    

    How can I do that? Do I need to create a web server or something else?

  • Olcay Sönmez
    Olcay Sönmez almost 8 years
    thanks for your response. It's helpful for me . I read almost all document from FCM.There wirte same thing like your answer. I want to be sure. because I think there is a server for notification.
  • Angga Ari Wijaya
    Angga Ari Wijaya over 7 years
    is registration_id and registration token same thing?
  • Tim
    Tim over 7 years
    @AnggaAriWijaya yes
  • kunwar97
    kunwar97 over 6 years
    This is not recommended way because anyone can get your API key by using tools like Smali2Java and can send the notification to anyone.
  • Durgesh Kumar
    Durgesh Kumar over 6 years
    that's the part of hacking, and Yeah I also don't recommend this but I was giving the answer that we can do it from here too, we can secure that part by encrypting the key if needed. and yes thanks for letting me know this.
  • Tarlan Ahad
    Tarlan Ahad over 6 years
    How will we use this?
  • Durgesh Kumar
    Durgesh Kumar over 6 years
    just provide token_id of receiver and body which you want to send. Also do not forget to change api key
  • Tim
    Tim about 6 years
    we can secure that part by encrypting the key - that's not true
  • John Albert
    John Albert over 5 years
    good , but id you can get from FirebaseInstanceId.getInstance().getToken() or class AppFirebaseMessageingService: FirebaseMessagingService() { override fun onNewToken(token: String?) { super.onNewToken(token) } override fun onMessageReceived(msg: RemoteMessage?) { super.onMessageReceived(msg)}}
  • TheWebGuy
    TheWebGuy about 5 years
    So if I get this right after we get the registration ID, we need to do a postback at our own endpoint to save the registration ID along with the unique identifier of the user in our system. And there is no way for firebase to save that data for us?
  • Tim
    Tim about 5 years
    @TheWebGuy yes. How would you tell firebase who to send a message to if you can't tell them who it is?
  • Jay Dwivedi
    Jay Dwivedi almost 5 years
    could you please share a sample of body? in java string that you have passed as a parameter
  • Durgesh Kumar
    Durgesh Kumar almost 5 years
    FireBase fb = new FireBase(); JSONObject jsonObject = new JSONObject(); try { jsonObject.put("status", params[0]); jsonObject.put("title", "Request " + params[0]); jsonObject.put("from", preferences.getString("name", "")); jsonObject.put("token_id", preferences.getString("token_id", "")); } catch (Exception e) { e.printStackTrace(); } fb.send(requester_token, jsonObject.toString());
  • Caleb_Allen
    Caleb_Allen over 4 years
    Do not do this. Your private api key is for your use on hardware under your control. Do not distribute it publicly.