0

On my android app have many users but I want to send notification only specific user who is suitable. I don't want to send all users. if a user is already logged on app notification will pop up on the screen.

Need help, Suggestion will be highly appreciatable.

Kayes Fahim
  • 672
  • 5
  • 16
  • If you consider at some point in time to try using [Cloud Firestore](https://firebase.google.com/docs/firestore/), please note that I have explained in one of my **[tutorials](https://www.youtube.com/playlist?list=PLn2n4GESV0AmXOWOam729bC47v0d0Ohee)** step by step, how you can send **[notifications](https://www.youtube.com/watch?v=6RzB4HXzQyA&t=3s&list=PLn2n4GESV0AmXOWOam729bC47v0d0Ohee&index=17)** to specific users using `Node.js`. You can also take a look at my answer from this **[post](https://stackoverflow.com/questions/48298993/push-notifications-on-content-change/48299840)**. – Alex Mamo Feb 06 '21 at 10:32

2 Answers2

0

When the device firebase initialized, it will return a device token, which we normally send it the backend so it will be stored in the backend database together with the user id, then later can used for your purpose.

Shawn Cui
  • 1
  • 1
0

When a user first runs your app Firebase generates a Token for that user and his device. Each time user deletes the app and reinstalls it will re-generate this Token. This also includes new devices of course since that is just like the first installation ever.

What I did with this is that I used the Firebase Realtime database in which I stored this Token for each user I have. Then when I need to send the notification to that user I just get that token and send it within my application. This is one old example on how I did that:

OkHttpClient client = new OkHttpClient();                
Logger.getLogger(OkHttpClient.class.getName()).setLevel(Level.FINE);
JSONObject json = new JSONObject();                    
JSONObject notifJson = new JSONObject();
JSONObject dataJson = new JSONObject();
notifJson.put("text", body);
notifJson.put("title", title);
dataJson.put("customId", "02");
json.put("notification", notifJson);
json.put("data", dataJson);
json.put("to", firebase_token);
RequestBody body = RequestBody.create(MediaType.parse("application/json"), json.toString());
        .header("Authorization", "key="+server_key)
        .url("https://fcm.googleapis.com/fcm/send")
        .post(body)
        .build();
Response response = client.newCall(request).execute();
if (response.isSuccessful()) { //do something if notification is sent successfully }

You can do this using your code to send notifications to a specific user. This is, of course, if you want to send a notification from your code. If you want to use a server or something there is documentation you can follow here: https://firebase.google.com/docs/cloud-messaging/android/send-multiple

What I used in this example:

  • notifJson - notification part
  • dataJson - data part of notification
  • body - the body of my notification
  • title - the title of my notification
  • firebase_token - Token of user I am sending a notification to, this is retrieved earlier with Firebase real-time database
  • server_key - Server key from Firebase configuration

In the IF statement below if the response was successful I add some data to my real-time database but you can do anything else you want.

SlothCoding
  • 1,546
  • 7
  • 14