Firebase:如何在 Android 应用程序中设置默认通知渠道?
Firebase: How to set default notification channel in Android app?
如何为应用程序在后台时出现的通知消息设置默认通知渠道?默认情况下,这些消息使用 "Miscellaneous" 频道。
如您所见 here in the official docs,您需要在应用程序组件内的 AndroidManifest.xml
中添加以下元数据元素:
<meta-data
android:name="com.google.firebase.messaging.default_notification_channel_id"
android:value="@string/default_notification_channel_id"/>
当通知消息没有指定的频道,或者提供的频道尚未被应用程序创建时,将使用此默认频道。
你需要添加 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>
您需要使用 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>
如何为应用程序在后台时出现的通知消息设置默认通知渠道?默认情况下,这些消息使用 "Miscellaneous" 频道。
如您所见 here in the official docs,您需要在应用程序组件内的 AndroidManifest.xml
中添加以下元数据元素:
<meta-data
android:name="com.google.firebase.messaging.default_notification_channel_id"
android:value="@string/default_notification_channel_id"/>
当通知消息没有指定的频道,或者提供的频道尚未被应用程序创建时,将使用此默认频道。
你需要添加 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>
您需要使用 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>