简体   繁体   English

FCM升级后,通知未显示在Android API级别26+上

[英]Notification not showing on Android API level 26+ after FCM upgrade

The code below doesn't seem to be showing a notification on my emulator. 下面的代码似乎没有在我的模拟器上显示通知。 I upgraded from the deprecated Firebase code to the new stuff, with channels. 我将不赞成使用的Firebase代码升级为带有频道的新内容。 Is there something I should look into? 我有什么需要研究的吗?

<application android:icon="@drawable/icon" android:allowBackup="false" android:label="@string/application_label" android:theme="@style/Theme">
        <meta-data android:name="com.google.firebase.messaging.default_notification_icon" android:resource="@drawable/icon" />
        <meta-data android:name="com.google.firebase.messaging.default_notification_color" android:resource="@color/accentColor" />
        <meta-data android:name="com.google.firebase.messaging.default_notification_channel_id" android:value="@string/default_notification_channel_id" />
        ......
        <service
            android:name="com.exposure.services.ExposureFirebaseMessagingService">
            <intent-filter>
                <action android:name="com.google.firebase.MESSAGING_EVENT"/>
            </intent-filter>
        </service>
    </application>

在此处输入图片说明

From Android Oreo, you must use channels to create notifications. 在Android Oreo中,您必须使用渠道来创建通知。

Starting in Android 8.0 (API level 26), all notifications must be assigned to a channel. 从Android 8.0(API级别26)开始,必须将所有通知分配给一个频道。 For each channel, you can set the visual and auditory behavior that is applied to all notifications in that channel. 对于每个渠道,您可以设置应用于该渠道中所有通知的视觉和听觉行为。 Then, users can change these settings and decide which notification channels from your app should be intrusive or visible at all. 然后,用户可以更改这些设置,并确定您应用中的哪些通知渠道应该是侵入性的或根本不可见的。


Caution: If you target Android 8.0 (API level 26) and post a notification without specifying a notification channel, the notification does not appear and the system logs an error. 注意:如果您以Android 8.0(API级别26)为目标并发布通知而未指定通知渠道,则不会出现该通知,并且系统会记录错误。

Read more docs at: https://developer.android.com/training/notify-user/channels 在以下网址阅读更多文档: https//developer.android.com/training/notify-user/channels


Apparently, you have used a notification channel for your notifications, but haven't created a notification channel associated with that id. 显然,您已使用通知通道进行通知,但尚未创建与该ID关联的通知通道。 So you will need to create a notification channel before creating notifications. 因此,您需要在创建通知之前创建一个通知通道。

Example to create a notification channel (maybe in your FCM Service class) is given below: 下面给出了创建通知通道的示例(可能在FCM服务类中):

@Override
public void onCreate() {
    super.onCreate();
    if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
        // Make sure you use the same channel id ("default" in this case)
        // while creating notifications.
        NotificationChannel channel = new NotificationChannel("default", "Default Channel",
                NotificationManager.IMPORTANCE_HIGH);
        channel.setDescription("Default Notification Channel");
        NotificationManager notificationManager = (NotificationManager)
                getSystemService(NOTIFICATION_SERVICE);
        if (notificationManager != null) {
            notificationManager.createNotificationChannel(channel);
        }
    }
}

在根据Google开发者方https://developer.android.com/training/notify-user/channels创建通知之前,请使用通知频道

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

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