Firebase通知未以高优先级发送

6
当我通过Firebase Web界面向我的Android设备发送通知时,通知不会从状态栏上弹出。如果我想查看通知,必须向下滑动。即使在Web界面中将优先级设置为“高”,仍会发生这种情况。为什么会这样?
如果通知在应用程序打开时到达,则这不是问题,因为我可以在我的FirebaseMessagingService类中自己设置优先级。
public class MyFirebaseMessagingService extends FirebaseMessagingService {

  @Override public void onMessageReceived(RemoteMessage remoteMessage) {
    super.onMessageReceived(remoteMessage);
    sendNotification("Message Body");

  }

  /**
   * Create and show a simple notification containing the received FCM message.
   *
   * @param messageBody FCM message body received.
   */
  private void sendNotification(String messageBody) {
    Intent intent = new Intent(this, SubscribedActivity.class);
    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent,
        PendingIntent.FLAG_ONE_SHOT);

    Uri defaultSoundUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
    NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
        .setSmallIcon(R.drawable.ic_push_notification)
        .setContentTitle("FCM Message")
        .setContentText(messageBody)
        .setAutoCancel(true)
        .setSound(defaultSoundUri)
        .setPriority(NotificationCompat.PRIORITY_MAX)
        .setColor(ContextCompat.getColor(this, R.color.color_accent))
        .setContentIntent(pendingIntent);

    NotificationManager notificationManager =
        (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

    notificationManager.notify(0 /* ID of notification */, notificationBuilder.build());
  }
}

代码看起来没问题。也许这种行为是设备特定的。你尝试过用不同的设备进行测试吗? - AL.
@AL。代码按预期工作。问题在于,只有当应用程序在前台接收到通知时,才会执行代码。当应用程序在后台接收到通知时,问题就出现了。我已经在Google Pixel XL和Nexus 6P上尝试过了。感谢您的帮助。 - Dick Lucas
1
据我理解,当您的应用程序在前台时,上述代码会按预期执行(生成从状态栏向下窥视的通知)。当在后台时,它只在状态栏中显示一个图标。问题在于后台时的行为。在我看来,这是预期的行为。通过Firebase控制台发送的通知被视为通知消息,如果在应用程序处于后台时发送通知消息,则设备本身将处理如何显示它。https://firebase.google.com/docs/cloud-messaging/android/receive - AL.
1个回答

9
当应用程序处于打开状态时,您可以处理自己的通知,因此可以控制它所做的操作。但是,当应用程序在后台运行时,通知由系统托盘处理,您可以从Web控制台传递的唯一优先级参数是高和正常。然而,如果用户不与您的应用程序交互,则将优先级设置为高将无法按预期工作。文档 高优先级消息通常应导致用户与您的应用程序进行交互。如果 FCM 检测到一种模式,在其中用户没有与您的应用程序进行交互,您的消息可能会被降低优先级。

嗨,伙计,我们能避免降低优先级吗? - famfamfam

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接