无法通过FirebaseMessaging接收前台通知,但在后台运行

我添加了一个FirebaseMessagingService类来接收前台通知。 我没有收到前台通知。 在后台最小化应用程序时,它可以正常工作。

我已经设置了具有Firebase功能的后台通知,并且可以正常工作,现在我正试图在前台获取此通知,但该通知没有出现。

我的FirebaseMessagingService类:

public class FirebaseMessaging extends FirebaseMessagingService {


    @Override
    public void onmessageReceived(@NonNull RemoteMessage remoteMessage) {
        super.onmessageReceived(remoteMessage);
        if (remoteMessage.getNotification() != null) {
            String notification_title = remoteMessage.getNotification().getTitle();
            String notification_message = remoteMessage.getNotification().getBody();

            Notification.Builder mBuilder = new Notification.Builder(this)
                    .setContentTitle(notification_title)
                    .setContentText(notification_message)
                    .setSmallIcon(R.drawable.default_avatar);

            int mNotificationId = (int) System.currentTimeMillis();

            Notificationmanager mNotifyMgr = (Notificationmanager) getSystemService(NOTIFICATION_SERVICE);
            mNotifyMgr.notify(mNotificationId,mBuilder.build());
        }
    }
}

没有错误消息:

实际结果:通知永远不会到达前台。 预期结果:我希望不仅在最小化时在应用程序中收到即时消息通知。

lingxueer1 回答:无法通过FirebaseMessaging接收前台通知,但在后台运行

我将FirebaseMEssagingService类更改为可以正常工作:

public class FirebaseMessaging extends FirebaseMessagingService {
private final String CHANNEL_ID = "personal_notifications";
@Override
public void onMessageReceived(@NonNull RemoteMessage remoteMessage) {
    super.onMessageReceived(remoteMessage);

    NotificationManager mNotifyMgr = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

    String notification_title = remoteMessage.getNotification().getTitle();
    String notification_message = remoteMessage.getNotification().getBody();

    NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        NotificationChannel notificationChannel = new NotificationChannel(CHANNEL_ID,"My Notifications",NotificationManager.IMPORTANCE_HIGH);

        NotificationCompat.Builder mBuilder =
                new NotificationCompat.Builder(this,CHANNEL_ID)
                        .setSmallIcon(R.drawable.default_avatar)
                        .setContentTitle(notification_title)
                        .setContentText(notification_message);

        notificationManager.createNotificationChannel(notificationChannel);

        int mNotificationId = (int) System.currentTimeMillis();
        mNotifyMgr.notify(mNotificationId,mBuilder.build());
    }
}

}

本文链接:https://www.f2er.com/3131548.html

大家都在问