如何在通知中有时播放声音
How to play sometimes sound in notifications
我有这个代码来显示一个通知,它可以显示通知,但是当我再次 运行 这个功能时,我听不到任何声音。
public NotificationManager nm;
...
void ShowNotification(string title = "", string message = "", string header = "", bool notify = false)
{
if (Build.VERSION.SdkInt >= BuildVersionCodes.O)
{
CreateSilentNotificationChannel();
}
var text = GetText(Resource.String.Local_service_label);
// Set the icon, scrolling text and timestamp
var sinceEpoch = DateTime.UtcNow - new DateTime(1970, 1, 1);
var msSinceEpoch = (long)sinceEpoch.TotalMilliseconds;
PendingIntent contentIntent = PendingIntent.GetActivity(this, 0, new Intent(this, typeof(MainActivity)), 0);
PendingIntent refreshIntent = PendingIntent.GetActivity(this, 0, new Intent(this, typeof(LocalService)), 0);
Android.Net.Uri uri = RingtoneManager.GetDefaultUri(RingtoneType.Notification);
NotificationCompat.Builder builder = new NotificationCompat.Builder(this, currentNotId);
builder.SetContentTitle(GetString(Resource.String.OddEven_Notifications_AlertTitle))
.SetContentIntent(contentIntent)
.SetSmallIcon(Resource.Mipmap.ic_launcher)
.SetTicker(text)
.SetWhen(msSinceEpoch)
.SetAutoCancel(false)
.SetOngoing(true)
.SetUsesChronometer(true)
.SetShowWhen(true)
.SetContentTitle(title)
.SetVisibility((int)NotificationVisibility.Public)
.SetSubText(header)
.SetLargeIcon(BitmapFactory.DecodeResource(this.Resources, Resource.Mipmap.ic_launcher))
.SetContentText(message);
if (notify)
{
builder.SetVibrate(new long[] { 0, 400, 200, 200 });
builder.SetSound(RingtoneManager.GetDefaultUri(RingtoneType.Notification));
}
StickyNotification = builder.Build();
if (Build.VERSION.SdkInt < BuildVersionCodes.O && notify)
{
StickyNotification.Vibrate = new long[] { 0, 400, 200, 200 };
}
nm.Notify(NOTIFICATION, StickyNotification);
}
private void CreateSilentNotificationChannel()
{
var channelDescription = "SilentChannelForOddEvenNotification";
var chan1 = new NotificationChannel(notIDSilent, GetString(Resource.String.app_name), NotificationImportance.High)
{
Description = channelDescription,
Name = GetString(Resource.String.app_name),
};
// set the channel properties
chan1.EnableLights(true);
chan1.LightColor = Color.Red;
chan1.SetSound(null, null);
chan1.EnableVibration(false);
chan1.SetBypassDnd(true);
chan1.LockscreenVisibility = NotificationVisibility.Public;
// finally create the notification channel
//var manager = (NotificationManager)GetSystemService(NotificationService);
nm.CreateNotificationChannel(chan1);
}
我运行每 5 分钟调用一次 ShowNotification() 函数,但是如果我有 属性 通知 true 或 false,我听不到任何声音,为什么?
获得我们定期更新且有时我们还希望有声音的粘性通知的正确方法是什么?
在通知的通知负载中有一个声音键。
从官方文档看它的用法是:
表示设备收到通知时播放的声音。支持默认或应用程序中捆绑的声音资源的文件名。声音文件必须位于 /res/raw/anySound
.
{
"to" : "bk3RNwTe3H0:CI2k_HHwgIpoDKCIZvvDMExUdFQ3P1...",
"notification" : {
"body" : "great match!",
"title" : "Portugal vs. Denmark",
"icon" : "myicon",
"sound" : "default"
}
}
在通知负载中,您可以使用 "sound" : "default"
或 "sound" : "anySound"
你的情况可能是这些行有问题
chan1.SetSound(null, null);
if (notify){
builder.SetVibrate(new long[] { 0, 400, 200, 200 });
builder.SetSound(RingtoneManager.GetDefaultUri(RingtoneType.Notification));
}
从Android8.0(Oreo)开始,最好在Channel里面设置声音或者优先级。并使用下面的两种方式代替 chan1.SetSound(null, null);
.
使用特定的铃声进行通知。
void CreateNotificationChannel()
{
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 channel = new NotificationChannel(CHANNEL_ID, "FCM Notifications", NotificationImportance.Default)
{
Description = "Firebase Cloud Messages appear in this channel"
};
channel.SetSound(RingtoneManager.GetDefaultUri(RingtoneType.Ringtone),alarmAttributes);
var notificationManager = (NotificationManager)GetSystemService(NotificationService);
notificationManager.CreateNotificationChannel(channel);
}
详情请参考下方link。 specific ringtone firebase notification xamarin.android
使用自定义声音进行通知。
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);
}
有关详细信息,请查看下面的 link。 Custom Notification Sound FireBase
我有这个代码来显示一个通知,它可以显示通知,但是当我再次 运行 这个功能时,我听不到任何声音。
public NotificationManager nm;
...
void ShowNotification(string title = "", string message = "", string header = "", bool notify = false)
{
if (Build.VERSION.SdkInt >= BuildVersionCodes.O)
{
CreateSilentNotificationChannel();
}
var text = GetText(Resource.String.Local_service_label);
// Set the icon, scrolling text and timestamp
var sinceEpoch = DateTime.UtcNow - new DateTime(1970, 1, 1);
var msSinceEpoch = (long)sinceEpoch.TotalMilliseconds;
PendingIntent contentIntent = PendingIntent.GetActivity(this, 0, new Intent(this, typeof(MainActivity)), 0);
PendingIntent refreshIntent = PendingIntent.GetActivity(this, 0, new Intent(this, typeof(LocalService)), 0);
Android.Net.Uri uri = RingtoneManager.GetDefaultUri(RingtoneType.Notification);
NotificationCompat.Builder builder = new NotificationCompat.Builder(this, currentNotId);
builder.SetContentTitle(GetString(Resource.String.OddEven_Notifications_AlertTitle))
.SetContentIntent(contentIntent)
.SetSmallIcon(Resource.Mipmap.ic_launcher)
.SetTicker(text)
.SetWhen(msSinceEpoch)
.SetAutoCancel(false)
.SetOngoing(true)
.SetUsesChronometer(true)
.SetShowWhen(true)
.SetContentTitle(title)
.SetVisibility((int)NotificationVisibility.Public)
.SetSubText(header)
.SetLargeIcon(BitmapFactory.DecodeResource(this.Resources, Resource.Mipmap.ic_launcher))
.SetContentText(message);
if (notify)
{
builder.SetVibrate(new long[] { 0, 400, 200, 200 });
builder.SetSound(RingtoneManager.GetDefaultUri(RingtoneType.Notification));
}
StickyNotification = builder.Build();
if (Build.VERSION.SdkInt < BuildVersionCodes.O && notify)
{
StickyNotification.Vibrate = new long[] { 0, 400, 200, 200 };
}
nm.Notify(NOTIFICATION, StickyNotification);
}
private void CreateSilentNotificationChannel()
{
var channelDescription = "SilentChannelForOddEvenNotification";
var chan1 = new NotificationChannel(notIDSilent, GetString(Resource.String.app_name), NotificationImportance.High)
{
Description = channelDescription,
Name = GetString(Resource.String.app_name),
};
// set the channel properties
chan1.EnableLights(true);
chan1.LightColor = Color.Red;
chan1.SetSound(null, null);
chan1.EnableVibration(false);
chan1.SetBypassDnd(true);
chan1.LockscreenVisibility = NotificationVisibility.Public;
// finally create the notification channel
//var manager = (NotificationManager)GetSystemService(NotificationService);
nm.CreateNotificationChannel(chan1);
}
我运行每 5 分钟调用一次 ShowNotification() 函数,但是如果我有 属性 通知 true 或 false,我听不到任何声音,为什么?
获得我们定期更新且有时我们还希望有声音的粘性通知的正确方法是什么?
在通知的通知负载中有一个声音键。
从官方文档看它的用法是:
表示设备收到通知时播放的声音。支持默认或应用程序中捆绑的声音资源的文件名。声音文件必须位于 /res/raw/anySound
.
{
"to" : "bk3RNwTe3H0:CI2k_HHwgIpoDKCIZvvDMExUdFQ3P1...",
"notification" : {
"body" : "great match!",
"title" : "Portugal vs. Denmark",
"icon" : "myicon",
"sound" : "default"
}
}
在通知负载中,您可以使用 "sound" : "default"
或 "sound" : "anySound"
你的情况可能是这些行有问题
chan1.SetSound(null, null);
if (notify){
builder.SetVibrate(new long[] { 0, 400, 200, 200 });
builder.SetSound(RingtoneManager.GetDefaultUri(RingtoneType.Notification));
}
从Android8.0(Oreo)开始,最好在Channel里面设置声音或者优先级。并使用下面的两种方式代替 chan1.SetSound(null, null);
.
使用特定的铃声进行通知。
void CreateNotificationChannel() { 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 channel = new NotificationChannel(CHANNEL_ID, "FCM Notifications", NotificationImportance.Default) { Description = "Firebase Cloud Messages appear in this channel" }; channel.SetSound(RingtoneManager.GetDefaultUri(RingtoneType.Ringtone),alarmAttributes); var notificationManager = (NotificationManager)GetSystemService(NotificationService); notificationManager.CreateNotificationChannel(channel); }
详情请参考下方link。 specific ringtone firebase notification xamarin.android
使用自定义声音进行通知。
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); }
有关详细信息,请查看下面的 link。 Custom Notification Sound FireBase