未通过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());
}
}
}

无错误消息:

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

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

}

相关内容

  • 没有找到相关文章

最新更新