【发布时间】:2020-06-14 07:35:55
【问题描述】:
如何向 xamarin.android 项目发送通知以使电话响起就像打电话一样我试图在 Resource 中创建原始文件夹并设置我的铃声,但不幸的是它不起作用
有什么解决办法吗?
【问题讨论】:
如何向 xamarin.android 项目发送通知以使电话响起就像打电话一样我试图在 Resource 中创建原始文件夹并设置我的铃声,但不幸的是它不起作用
有什么解决办法吗?
【问题讨论】:
在 Resource 中创建一个 raw 文件夹并将 mp3 文件放入其中,并使用AndroidResource Build Action。
设置通知通道的自定义声音会覆盖默认声音。
您可以通过频道的SetSound 使用自定义通知。
创建频道的整体:
void CreateNotificationChannel1()
{
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 alarmAttributes = new AudioAttributes.Builder()
.SetContentType(AudioContentType.Sonification)
.SetUsage(AudioUsageKind.Notification).Build();
var path = Android.Net.Uri.Parse("android.resource://com.companyname.NotificationChannelsDemo/" + Resource.Raw.Hello);
var name = Resources.GetString(Resource.String.channel_name);
var description = GetString(Resource.String.channel_description);
var channel = new NotificationChannel(CHANNEL_ID1, name, NotificationImportance.Max)
{
Description = description
};
channel.SetSound(path, alarmAttributes);
var notificationManager = (NotificationManager)GetSystemService(NotificationService);
notificationManager.CreateNotificationChannel(channel);
}
有关如何使用通知渠道的更多信息,您可以查看下面的链接。 https://docs.microsoft.com/en-us/xamarin/android/app-fundamentals/notifications/local-notifications-walkthrough
【讨论】: