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

因此,当应用程序处于后台或前台时,您应该发送一条格式为“数据”的消息来处理onMessageReceived中的通知