【问题标题】:Firebase notifications are not sending as high priorityFirebase 通知未作为高优先级发送
【发布时间】:2017-01-08 00:52:35
【问题描述】:

当我通过 Firebase 网络界面向我的 Android 设备发送通知时,通知不会从状态栏向下查看。如果我想看到通知,我必须向下滑动。即使我在 Web 界面中将优先级设置为 High,也会发生这种情况。这是为什么呢?

如果在应用打开时收到通知,这不是问题,因为我可以自己在 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。代码按预期工作。问题是,只有在应用程序处于前台时收到通知时才会执行该代码。当应用程序在后台时收到通知时会出现此问题。我试过 Google Pixel XL 和 Nexus 6P。感谢您的帮助。
  • 据我了解,当您的应用程序处于前台时,上面的代码会按预期执行(生成从状态栏向下查看的通知)。在后台时,它仅在状态栏中显示一个图标。问题是在后台时的行为。在我看来,这是预期的行为。通过 Firebase 控制台发送的通知被视为通知消息,其中如果在应用程序处于后台时发送通知消息,设备本身将处理如何显示它。 firebase.google.com/docs/cloud-messaging/android/receive

标签: android firebase firebase-notifications


【解决方案1】:

当应用打开时,您正在处理自己的通知,因此您可以控制它的作用。但是,当应用程序处于后台时,通知由系统托盘处理,您可以从 Web 控制台传递的唯一优先级参数是高且正常的。但是,如果用户不与您的应用进行交互,则将优先级传递得尽可能高将无法正常工作。 Documentation

高优先级消息通常会导致用户与您的应用进行交互。如果 FCM 检测到他们没有检测到的模式,您的消息可能会被取消优先级。

【讨论】:

  • 嗨,哥们,我们可以避免降低优先级吗?
猜你喜欢
  • 2019-05-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-08-05
  • 2020-09-15
  • 1970-01-01
  • 1970-01-01
  • 2020-12-21
相关资源
最近更新 更多