Android 应用程序处于前台时不显示通知
我已将此代码添加到Android 应用程序处于前台时不显示通知,android,push-notification,android-notifications,Android,Push Notification,Android Notifications,我已将此代码添加到FirebaseNotificationService类的onMessageReceived方法中,该类扩展了FirebaseMessagingService Notification notification = new NotificationCompat.Builder(this) .setContentTitle(remoteMessage.getNotification().getTitle()) .setContent
FirebaseNotificationService
类的onMessageReceived
方法中,该类扩展了FirebaseMessagingService
Notification notification = new NotificationCompat.Builder(this)
.setContentTitle(remoteMessage.getNotification().getTitle())
.setContentText(remoteMessage.getNotification().getBody())
.setSmallIcon(R.mipmap.ic_launcher)
.build();
NotificationManagerCompat manager = NotificationManagerCompat.from(getApplicationContext());
manager.notify(123, notification);
但是,当应用程序位于前台时,通知仍不起作用,而仅当应用程序位于后台时,通知才起作用
在AndroidManifest.xml
中使用此权限:
<service android:name="com.codb.shoppingapp.utils.FirebaseNotificationService">
<intent-filter>
<action android:name="com.google.firebase.MESSANGING_EVENT" />
</intent-filter>
</service>
尝试在通知上设置通知频道id。Android O需要通知通道。如果您不使用它,通知将不会显示
FCM有两种消息类型:当你的应用程序在后台时
- 通知信息:
- 数据电文:
- 通知消息:通知传递到设备的系统托盘,而不是onMessageReceived
- 数据消息:通知始终传递给onMessageReceived