android - Android 忽略 FCM 通知优先级

标签 android firebase firebase-cloud-messaging

我正在通过以下请求测试 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 应用程序中推送通知优先级的代码 fragment 。即使我将优先级更改为“正常”和“高”,问题始终具有相同的优先级。

@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 - Android 忽略 FCM 通知优先级,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/72038238/

相关文章:

android - Android系统使用BackupAgent时神秘的ClassNotFoundException

android - 具有协调器布局的 ViewPager

java - 如何在 Firebase 中仅获取子 key

Flutter 背景 Firebase 通信

javascript - 为老用户更新 firebase service-workers

java - 关于制作TitleBar的一个问题

Android 定位模式,无 GPS,AOSP 中的选项

php - Firebase 推送通知不起作用获取 token 号但仍然不起作用

android - 如何在用户注册期间将数据存储在 firebase 的实时数据库中?

objective-c - 无法连接到 FCM。错误域=com.google.fcm 代码=2001