英文:
How do I send notification to a specific user in Android using FCM token?
问题
I'm asking how I can send a notification to a specific user device by using the FCM token. The token is stored in the RealtimeDatabase in Firebase which is structured like this:
project-name: {
users: {
username: {
name: "..."
token: "..."
}
}
}
This is the code I use to store the token:
FirebaseInstanceId.getInstance().getInstanceId().addOnCompleteListener(new OnCompleteListener<InstanceIdResult>() {
@Override
public void onComplete(@NonNull Task<InstanceIdResult> task) {
if (task.isSuccessful()) {
String token = task.getResult().getToken();
saveToken(token);
}
}
});
private void saveToken(String token) {
reference.setValue(token);
}
where "reference" is the correct pointer to the db.. this works properly.
I want to use the token stored to send a push-notification to the user targeted.
I also have implemented the class MyFirebaseMessagingService
but I don't know how to use it to send notification to a specific user using his FCM token that I stored as I posted above.
public class MyFirebaseMessagingService extends FirebaseMessagingService {
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
Log.d(TAG, "From: " + remoteMessage.getFrom());
}
@Override
public void onNewToken(String token) {
Log.d(TAG, "Refreshed token: " + token);
sendRegistrationToServer(token);
}
private void sendRegistrationToServer(String token) {
//here I have code that store the token correctly
}
private void sendNotification(String messageBody) {
Intent intent = new Intent(this, MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent,
PendingIntent.FLAG_ONE_SHOT);
String channelId = getString(R.string.default_notification_channel_id);
Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notificationBuilder =
new NotificationCompat.Builder(this, channelId)
.setSmallIcon(R.drawable.ic_default)
.setContentTitle(getString(R.string.fcm_message))
.setContentText(messageBody)
.setAutoCancel(true)
.setSound(defaultSoundUri)
.setContentIntent(pendingIntent);
NotificationManager notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel channel = new NotificationChannel(channelId,
"Channel human readable title",
NotificationManager.IMPORTANCE_DEFAULT);
notificationManager.createNotificationChannel(channel);
}
notificationManager.notify(0, notificationBuilder.build());
}
}
So I want to target a specific user by his FCM token and sending him a notification but I can't find a way to do this. Please help me.
英文:
I'm asking how I can send a notification to a specific user device by using the FCM token. The token is stored in the RealtimeDatabase in Firebase which is structured like this:
project-name: {
users: {
username: {
name: "..."
token: "..."
}
}
}
This is the code I use to store the token
FirebaseInstanceId.getInstance().getInstanceId().addOnCompleteListener(new OnCompleteListener<InstanceIdResult>() {
@Override
public void onComplete(@NonNull Task<InstanceIdResult> task) {
if (task.isSuccessful()) {
String token = task.getResult().getToken();
saveToken(token);
}
}
});
private void saveToken(String token) {
reference.setValue(token);
}
where "reference" is the correct pointer to the db.. this works properly.
I want to use the token stored to send a push-notification to the user targeted.
I also have implemented the class MyFirebaseMessagingService but I don't know how to use it to send notification to a specific user using his FCM token that I stored as I posted above.
public class MyFirebaseMessagingService extends FirebaseMessagingService {
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
Log.d(TAG, "From: " + remoteMessage.getFrom());
}
@Override
public void onNewToken(String token) {
Log.d(TAG, "Refreshed token: " + token);
sendRegistrationToServer(token);
}
private void sendRegistrationToServer(String token) {
//here I have code that store the token correctly
}
private void sendNotification(String messageBody) {
Intent intent = new Intent(this, MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent,
PendingIntent.FLAG_ONE_SHOT);
String channelId = getString(R.string.default_notification_channel_id);
Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notificationBuilder =
new NotificationCompat.Builder(this, channelId)
.setSmallIcon(R.drawable.ic_default)
.setContentTitle(getString(R.string.fcm_message))
.setContentText(messageBody)
.setAutoCancel(true)
.setSound(defaultSoundUri)
.setContentIntent(pendingIntent);
NotificationManager notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel channel = new NotificationChannel(channelId,
"Channel human readable title",
NotificationManager.IMPORTANCE_DEFAULT);
notificationManager.createNotificationChannel(channel);
}
notificationManager.notify(0, notificationBuilder.build());
}
}
So I want to target a specific user by his FCM token and sending him a notification but I can't find a way to do this. Please help me.
答案1
得分: 2
要向特定用户发送通知,您需要调用此API:
https://fcm.googleapis.com/fcm/send
使用授权:"key=YOUR_FCM_KEY" 和内容类型:"application/json" 作为标头,请求体应如下:
{
"to": "FCM Token",
"priority": "high",
"notification": {
"title": "Your Title",
"text": "Your Text"
},
"data": {
"customId": "02",
"badge": 1,
"sound": "",
"alert": "Alert"
}
}
您应该从后端调用此API(推荐)。您也可以从Android设备调用它,但是要注意防止API密钥被劫持。
对于Android,您可以使用 okhttp 来调用API:
implementation("com.squareup.okhttp3:okhttp:4.9.0")
示例代码如下:
public static void senPushdNotification(final String body, final String title, final String fcmToken) {
new AsyncTask<Void, Void, Void>() {
@Override
protected Void doInBackground(Void... params) {
try {
OkHttpClient client = new OkHttpClient();
JSONObject json = new JSONObject();
JSONObject notificationJson = new JSONObject();
JSONObject dataJson = new JSONObject();
notificationJson.put("text", body);
notificationJson.put("title", title);
notificationJson.put("priority", "high");
dataJson.put("customId", "02");
dataJson.put("badge", 1);
dataJson.put("alert", "Alert");
json.put("notification", notificationJson);
json.put("data", dataJson);
json.put("to", fcmToken);
RequestBody requestBody = RequestBody.create(MediaType.parse("application/json; charset=utf-8"), json.toString());
Request request = new Request.Builder()
.header("Authorization", "key=YOUR_FCM_KEY")
.url("https://fcm.googleapis.com/fcm/send")
.post(requestBody)
.build();
Response response = client.newCall(request).execute();
String finalResponse = response.body().string();
Log.i("TAG", finalResponse);
} catch (Exception e) {
Log.i("TAG", e.getMessage());
}
return null;
}
}.execute();
}
英文:
To send the notification to a specific user you have to call this API:
https://fcm.googleapis.com/fcm/send
with Authorization:"key=YOUR_FCM_KEY" and Content-Type:"application/json" in header and the request body should be like:
{
"to": "FCM Token",
"priority": "high",
"notification": {
"title": "Your Title",
"text": "Your Text"
},
"data": {
"customId": "02",
"badge": 1,
"sound": "",
"alert": "Alert"
}
}
You should call this api from backend (Recommended). You can also call it from your android device,
but
> Be aware of hijacking your API Key
For android you can use okhttp for calling API
implementation("com.squareup.okhttp3:okhttp:4.9.0")
and the sample code will be like
public static void senPushdNotification(final String body, final String title, final String fcmToken) {
new AsyncTask<Void, Void, Void>() {
@Override
protected Void doInBackground(Void... params) {
try {
OkHttpClient client = new OkHttpClient();
JSONObject json = new JSONObject();
JSONObject notificationJson = new JSONObject();
JSONObject dataJson = new JSONObject();
notificationJson.put("text", body);
notificationJson.put("title", title);
notificationJson.put("priority", "high");
dataJson.put("customId", "02");
dataJson.put("badge", 1);
dataJson.put("alert", "Alert");
json.put("notification", notificationJson);
json.put("data", dataJson);
json.put("to", fcmToken);
RequestBody body = RequestBody.create(MediaType.parse("application/json; charset=utf-8"), json.toString());
Request request = new Request.Builder()
.header("Authorization", "key=YOUR_FCM_KEY")
.url("https://fcm.googleapis.com/fcm/send")
.post(body)
.build();
Response response = client.newCall(request).execute();
String finalResponse = response.body().string();
Log.i("TAG", finalResponse);
} catch (Exception e) {
Log.i("TAG", e.getMessage());
}
return null;
}
}.execute();
}
答案2
得分: 1
你需要创建一个后端服务器,用于发送特定操作的通知:https://firebase.google.com/docs/cloud-messaging/send-message
尽管你也可以使用Firebase实时数据库与云函数来实现:
英文:
You have to create a backend server to send notifications for specific actions: https://firebase.google.com/docs/cloud-messaging/send-message
Although you can use Firebase Realtime Database with Cloud Functions to achieve that also:
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论