Android 的 FCM 通知优先级被忽略
FCM notification priority is being ignored for Android
我正在使用以下请求测试 android 的 fcm 通知的优先级。并测试了“正常”和“高”的优先级。
curl --location --request POST 'https://fcm.googleapis.com/fcm/send' \
--header 'Authorization: key=##############################' \
--header 'Content-Type: application/json' \
--data-raw '{
"to":"##########################",
"notification" : {
"sound" : "default",
"body" : "high priority test",
"title" : "high priority test",
"content_available" : true,
"priority" : "high",
"time_to_live":4
}
}'
下面是我用来在 android 应用程序中检查推送通知优先级的代码片段。即使我将优先级更改为“正常”和“高”,问题始终具有相同的优先级。
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
super.onMessageReceived(remoteMessage);
Log.d("msg", "onMessageReceived: " + remoteMessage.getData().get("message"));
Intent intent = new Intent(this, MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_ONE_SHOT);
String channelId = "Default";
NotificationCompat.Builder builder = new NotificationCompat.Builder(this, channelId)
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle(remoteMessage.getNotification().getTitle())
.setContentText(remoteMessage.getNotification().getBody()
+" original priority-" + remoteMessage.getOriginalPriority()
+" delivered priority-" + remoteMessage.getPriority()
)
.setAutoCancel(true).setContentIntent(pendingIntent);;
NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel channel = new NotificationChannel(channelId, "Default channel", NotificationManager.IMPORTANCE_DEFAULT);
manager.createNotificationChannel(channel);
}
manager.notify(0, builder.build());
}
我找到了答案,问题出在我放置“优先级”参数的地方。它应该在通知对象之外。
curl --location --request POST 'https://fcm.googleapis.com/fcm/send' \
--header 'Authorization: key=##############' \
--header 'Content-Type: application/json' \
--data-raw '{
"to":"##############################",
"notification" : {
"sound" : "default",
"body" : "high priority test11",
"title" : "high priority test11",
"content_available" : true,
"time_to_live":4
},
"priority" : "high"
}'
我正在使用以下请求测试 android 的 fcm 通知的优先级。并测试了“正常”和“高”的优先级。
curl --location --request POST 'https://fcm.googleapis.com/fcm/send' \
--header 'Authorization: key=##############################' \
--header 'Content-Type: application/json' \
--data-raw '{
"to":"##########################",
"notification" : {
"sound" : "default",
"body" : "high priority test",
"title" : "high priority test",
"content_available" : true,
"priority" : "high",
"time_to_live":4
}
}'
下面是我用来在 android 应用程序中检查推送通知优先级的代码片段。即使我将优先级更改为“正常”和“高”,问题始终具有相同的优先级。
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
super.onMessageReceived(remoteMessage);
Log.d("msg", "onMessageReceived: " + remoteMessage.getData().get("message"));
Intent intent = new Intent(this, MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_ONE_SHOT);
String channelId = "Default";
NotificationCompat.Builder builder = new NotificationCompat.Builder(this, channelId)
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle(remoteMessage.getNotification().getTitle())
.setContentText(remoteMessage.getNotification().getBody()
+" original priority-" + remoteMessage.getOriginalPriority()
+" delivered priority-" + remoteMessage.getPriority()
)
.setAutoCancel(true).setContentIntent(pendingIntent);;
NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel channel = new NotificationChannel(channelId, "Default channel", NotificationManager.IMPORTANCE_DEFAULT);
manager.createNotificationChannel(channel);
}
manager.notify(0, builder.build());
}
我找到了答案,问题出在我放置“优先级”参数的地方。它应该在通知对象之外。
curl --location --request POST 'https://fcm.googleapis.com/fcm/send' \
--header 'Authorization: key=##############' \
--header 'Content-Type: application/json' \
--data-raw '{
"to":"##############################",
"notification" : {
"sound" : "default",
"body" : "high priority test11",
"title" : "high priority test11",
"content_available" : true,
"time_to_live":4
},
"priority" : "high"
}'