gpt4 book ai didi

java - 未通过 FirebaseMessaging 接收前台通知,但在后台工作

转载 作者:行者123 更新时间:2023-12-01 21:44:08 25 4
gpt4 key购买 nike

我添加了一个 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());
}
}
}

没有错误消息:

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

最佳答案

我将 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());
}
}

}

关于java - 未通过 FirebaseMessaging 接收前台通知,但在后台工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58782814/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com