Firebase:如何在Android应用程序中设置默认通知渠道?

54
如何设置应用在后台时收到的通知消息的默认通知渠道?这些消息默认使用“杂项”通道。
4个回答

67

如您可以在官方文档中看到的,您需要在应用程序组件内的AndroidManifest.xml中添加以下元数据元素:

<meta-data
    android:name="com.google.firebase.messaging.default_notification_channel_id"
    android:value="@string/default_notification_channel_id"/>

当通知消息没有指定通道或提供的通道尚未被应用程序创建时,将使用此默认通道。


26
这个 default_notification_channel_id 在哪里? - Mahesh Gawhane
18
请在您的strings.xml文件中加入<string name=“default_notification_channel_id”>Channel ID</string>,您可以查看此帖子获取更多信息:https://medium.com/exploring-android/exploring-android-o-notification-channels-94cd274f604c。 - chornge
4
如果我们在应用程序清单中甚至没有创建默认的通知渠道,会发生什么? - user1261913
16
在字符串资源中定义的“Channel ID”必须与某个地方的内容相对应,对吗? - The Tahaan
3
如果创建 strings.xml 文件,请确保使用 xml versionresources 标签正确格式化它,否则将会出现构建错误。参见:https://developer.android.com/guide/topics/resources/string-resource - Sludge
显示剩余5条评论

5

您需要使用NotificationManager CreateNotificationChannel注册一个频道。

此示例在Xamarin中使用C#,但在其他地方也可以广泛适用。

private void ConfigureNotificationChannel()
{
    if (Build.VERSION.SdkInt < BuildVersionCodes.O)
    {
        // Notification channels are new in API 26 (and not a part of the
        // support library). There is no need to create a notification
        // channel on older versions of Android.
        return;
    }

    var channelId = Resources.GetString(Resource.String.default_notification_channel_id);
    var channelName = "Urgent";
    var importance = NotificationImportance.High;

    //This is the default channel and also appears in the manifest
    var chan = new NotificationChannel(channelId, channelName, importance);

    chan.EnableVibration(true);
    chan.LockscreenVisibility = NotificationVisibility.Public;

    var notificationManager = (NotificationManager)GetSystemService(NotificationService);
    notificationManager.CreateNotificationChannel(chan);

}

这个通道应该是唯一的,例如com.mycompany.myapp.urgent

然后在AndroidManifest.xml文件中的application标签中添加对字符串的引用。

<application android:label="MyApp" android:icon="@mipmap/icon">
    <meta-data android:name="com.google.firebase.messaging.default_notification_channel_id" android:value="@string/default_notification_channel_id" />
</application>

最后,在strings.xml中设置字符串。
<?xml version="1.0" encoding="UTF-8" ?>
<resources>
    <string name="default_notification_channel_id">com.mycompany.myapp.urgent</string>
</resources>

这在发布模式下对我不起作用,只有在调试模式下才有效。你能帮忙吗? - Henrique Guimarães

1
你需要添加Cordova config.xml。
<widget 
... 
xmlns:android="http://schemas.android.com/apk/res/android" 
...>



 <platform name="android">
    <config-file target="AndroidManifest.xml" parent="application" mode="merge">
      <meta-data android:name="com.google.firebase.messaging.default_notification_channel_id" android:value="@string/default_notification_channel_id"/>
    </config-file>
    <config-file target="res/values/strings.xml" parent="/*">
      <string name="default_notification_channel_id">urgent_alert</string>
    </config-file>
</platform>

0
作为我的案例,我已经添加了以下内容:
- 在AndroidManifest.xml上添加了元数据 - 在MainActivity.kt上添加了Android通知通道 - 在values/string.xml上添加了<string name="default_notification_channel_id" translatable="false">fcm_default_channel</string> 然后我发现当时运行的设备处于夜间/暗模式,所以我需要将values/string.xml复制到values-night/string.xml(如果不存在则创建一个),这样本地通知就会显示出来。
因此,在浅色模式和暗色模式之间,我们需要独立调整这些值。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接