我已经开发了用于Firebase进行推送的Android应用程序,我希望使用设备的默认声音发送推送通知。我已经在android 6上测试了该应用程序,并且可以正常工作,它显示了图标,标题,正文并具有声音。但是,当我将推送发送到android 9设备时,它收到了推送,但没有声音,振动,led .....仅标题和正文。能告诉我如何在Android 9的频道上设置亮度,振动和声音的所有标志吗?我检查了电话设置,可以看到默认情况下如何禁用应用程序,并且仅启用声音通道,并且我想像其他应用程序一样默认启用它 谢谢
代码部分。
var channelId = channel.NotificationChannelId;
var channelName = channel.NotificationChannelName;
var channelImportance = channel.NotificationChannelImportance;
var notificationManager = (NotificationManager)context.GetSystemService(Context.NotificationService);
var notChannel = new NotificationChannel(channelId, channelName, channelImportance);
if (SoundUri != null)
{
try
{
var soundAttributes = new AudioAttributes.Builder()
.SetContentType(AudioContentType.Sonification)
.SetUsage(AudioUsageKind.Notification).Build();
notChannel.SetSound(SoundUri, soundAttributes);
}
catch (Exception ex)
{
System.Diagnostics.Debug.WriteLine(ex);
}
}
notificationManager.CreateNotificationChannel(notChannel);
答案 0 :(得分:1)
添加这些权限
<uses-permission android:name="android.permission.USE_FULL_SCREEN_INTENT" />
<uses-permission android:name="android.permission.ACCESS_NOTIFICATION_POLICY"/>
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
2。添加构建方法
fun createBuilder(
context: Context,
manager: NotificationManagerCompat): NotificationCompat.Builder {
val channelId = context.packageName
val notBuilder = NotificationCompat.Builder(context, channelId)
notBuilder.setSmallIcon(R.drawable.ic_stat_name)
notBuilder.setAutoCancel(true)
notBuilder.setVisibility(NotificationCompat.VISIBILITY_PUBLIC)
notBuilder.setDefaults(Notification.DEFAULT_ALL)
notBuilder.priority = NotificationCompat.PRIORITY_HIGH
val soundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION)
notBuilder.setSound(soundUri)
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
val importance= NotificationManager.IMPORTANCE_HIGH
val channel = NotificationChannel(
channelId, "Notifications",
importance
)
channel.importance =importance
channel.shouldShowLights()
channel.lightColor = Color.BLUE
channel.canBypassDnd()
val audioAttributes = AudioAttributes.Builder()
.setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
.setUsage(AudioAttributes.USAGE_NOTIFICATION)
.build()
channel.setSound(soundUri, audioAttributes)
channel.description = "Enable Notification"
notBuilder.setChannelId(channelId)
manager.createNotificationChannel(channel)
}
return notBuilder
}
3。显示通知
val builder = createBuilder(context, manager)
builder.setContentTitle(title)
builder.setContentText(message)
builder.setCategory(NotificationCompat.CATEGORY_MESSAGE)
val parentIntent = Intent(context, MainActivity::class.java)
parentIntent.flags = (Intent.FLAG_ACTIVITY_CLEAR_TASK
or Intent.FLAG_ACTIVITY_NEW_TASK)
val pIntent = PendingIntent.getActivity(
context, 0,
intent,
PendingIntent.FLAG_ONE_SHOT or
PendingIntent.FLAG_UPDATE_CURRENT
)
builder.setContentIntent(pIntent)
manager.notify(757, builder.build())
如果仍然无法运行,请卸载并重新安装该应用程序