繁体   English   中英

应用程序在前台时无法处理 FCM 通知

[英]Can't handdle FCM notification when app in foreground

伙计们,即使我编写了处理前台的代码,我也只有在后台应用程序时才收到通知我不知道是什么问题希望你能帮助我解决这个问题我真的卡住了

这是我的 FirebaseMessagingService class 代码:

 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) {

}

检查您的主要活动(在您的清单中设置为主要活动)的方法“onNewIntent”。 可以通过您在前台的推送来调用它。 如果它调用,您必须从您的意图的附加信息中获取数据。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM