简体   繁体   English

当应用程序在前台时不显示通知

[英]Notification not showing when app is in foreground

I have added this code in the onMessageReceived method of the FirebaseNotificationService class which extends FirebaseMessagingService我在扩展FirebaseMessagingServiceFirebaseNotificationService类的onMessageReceived方法中添加了此代码

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);

But still, the notification isn't working when the app is in the foreground but works only when the app is in the background.但是,当应用程序在前台时通知不起作用,但仅当应用程序在后台时才起作用。

With this permission in AndroidManifest.xml :AndroidManifest.xml使用此权限:

<service android:name="com.codb.shoppingapp.utils.FirebaseNotificationService">
        <intent-filter>
            <action android:name="com.google.firebase.MESSANGING_EVENT" />
        </intent-filter>
    </service>

Try setting a notification channel id on the notification.尝试在通知上设置通知频道 ID。 Notification channels became required with Android O. If you leave it out, the notification will not show. Android O 需要通知渠道。如果您忽略它,通知将不会显示。

https://developer.android.com/training/notify-user/channels https://developer.android.com/training/notify-user/channels

FCM has two messages types: when your app is in the background. FCM 有两种消息类型:当您的应用程序在后台时。

  • Notification message:通知讯息: 在此处输入图片说明

  • Data message:数据信息: 在此处输入图片说明

    1. Notification message: the notification is delivered to the device's system tray, not onMessageReceived通知消息:通知发送到设备的系统托盘,而不是 onMessageReceived

    2. Data message: always, the notification is delivered to onMessageReceived数据消息:始终,通知传递到 onMessageReceived 作为图像

so, You should send a format is Data Message to handle notification in onMessageReceived when the app is background or foreground因此,当应用程序处于后台或前台时,您应该发送格式为数据消息以处理 onMessageReceived 中的通知

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM