首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Firebase:如何在Android应用程序中设置默认通知通道?

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

Stack Overflow用户
提问于 2017-09-05 05:07:11
回答 3查看 69.1K关注 0票数 48

如何为应用程序在后台时出现的通知消息设置默认通知通道?默认情况下,这些消息使用“杂项”通道。

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2017-09-05 05:07:11

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

代码语言:javascript
复制
<meta-data
    android:name="com.google.firebase.messaging.default_notification_channel_id"
    android:value="@string/default_notification_channel_id"/>

如果通知消息没有指定的通道,或者应用程序尚未创建所提供的通道,则将使用此默认通道。

票数 63
EN

Stack Overflow用户

发布于 2021-03-11 10:00:47

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

此示例在Xamarin中使用c#,但在其他地方广泛适用。

代码语言:javascript
复制
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中的应用程序标记中添加对字符串的引用。

代码语言:javascript
复制
<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中设置字符串

代码语言:javascript
复制
<?xml version="1.0" encoding="UTF-8" ?>
<resources>
    <string name="default_notification_channel_id">com.mycompany.myapp.urgent</string>
</resources>
票数 2
EN

Stack Overflow用户

发布于 2020-05-19 08:43:23

您需要添加Cordova config.xml

代码语言:javascript
复制
<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>
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/46047343

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档