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() {
}
}
Author by
Olcay Sönmez
Updated on July 09, 2022Comments
-
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 almost 8 yearsthanks 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 over 7 yearsis registration_id and registration token same thing?
-
Tim over 7 years@AnggaAriWijaya yes
-
kunwar97 over 6 yearsThis 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 over 6 yearsthat'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 over 6 yearsHow will we use this?
-
Durgesh Kumar over 6 yearsjust provide token_id of receiver and body which you want to send. Also do not forget to change api key
-
Tim about 6 yearswe can secure that part by encrypting the key - that's not true
-
John Albert over 5 yearsgood , 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 about 5 yearsSo 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 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 almost 5 yearscould you please share a sample of body? in java string that you have passed as a parameter
-
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 over 4 yearsDo not do this. Your private api key is for your use on hardware under your control. Do not distribute it publicly.