Java 不使用FirebaseMessaging接收前台通知,但在后台工作
我添加了一个FirebaseMessagingService类来接收前台通知。 我没有收到前台通知。 在后台,当应用程序最小化时,它工作正常 我已经用firebase函数设置了后台通知,它工作得很好,现在我试图在前台获取此通知,但通知没有出现 我的FirebaseMessagingService类:Java 不使用FirebaseMessaging接收前台通知,但在后台工作,java,android,firebase-cloud-messaging,Java,Android,Firebase Cloud Messaging,我添加了一个FirebaseMessagingService类来接收前台通知。 我没有收到前台通知。 在后台,当应用程序最小化时,它工作正常 我已经用firebase函数设置了后台通知,它工作得很好,现在我试图在前台获取此通知,但通知没有出现 我的FirebaseMessagingService类: public class FirebaseMessaging extends FirebaseMessagingService { @Override public void o
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());
}
}
}