【问题标题】:Can't handdle FCM notification when app in foreground应用程序在前台时无法处理 FCM 通知
【发布时间】:2022-01-01 23:29:37
【问题描述】:

伙计们,即使我编写了处理前台的代码,我也只有在后台应用程序时才会收到通知

这是我的 FirebaseMessagingService 类代码:

 private static final String CHANNEL_ID ="101" ;
@Override
public void onMessageReceived(@NonNull RemoteMessage remoteMessage) {
    Intent intent = new Intent(this, All_Tickets.class);
    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 0);

    NotificationCompat.Builder builder = new NotificationCompat.Builder(this, "101")
            .setSmallIcon(R.drawable.ic_add)
            .setContentTitle(remoteMessage.getNotification().getTitle())
            .setContentText(remoteMessage.getNotification().getBody())
            .setPriority(NotificationCompat.PRIORITY_DEFAULT)
            // Set the intent that will fire when the user taps the notification
            .setContentIntent(pendingIntent)
            .setAutoCancel(true);

    NotificationManagerCompat notificationManager = NotificationManagerCompat.from(this);

// notificationId is a unique int for each notification that you must define
    notificationManager.notify(1, builder.build());
}

@Override
public void onNewToken(@NonNull String s) {

}

【问题讨论】:

    标签: android firebase firebase-cloud-messaging


    【解决方案1】:

    检查您的主要活动(在您的清单中设置为主要活动)的方法“onNewIntent”。可以在前台推送时调用它。如果它调用了,你必须从你的 Intent 的 extra 中获取数据。

    【讨论】:

      猜你喜欢
      • 2018-07-31
      • 2018-09-07
      • 2021-10-17
      • 2020-05-17
      • 1970-01-01
      • 2018-05-31
      • 1970-01-01
      • 2017-10-31
      • 2019-04-13
      相关资源
      最近更新 更多