我知道有很多关于这个问题的帖子。我已经尝试了所有。这是我所做的步骤。
首先,我发现创建 channel 后就无法更改。唯一的方法是重新安装该应用程序。这就是我所做的,但没有奏效。
其次,有人说我可以删除 channel ,所以我也使用这段代码
val channelList = mNotificationManager.notificationChannels
var i = 0
while (channelList != null && i < channelList.size) {
Log.d("channelList","channel ID is ${channelList[i].id}")
//mNotificationManager.deleteNotificationChannel(channelList[i].id)
i++
}
然后在删除后重新创建 channel 。
第三,我尝试使用新的通知 channel ,但是每次使用新 channel 时,都会出现错误。
这是我尝试使用的所有解决方案的代码
val audioAttributes = AudioAttributes.Builder()
.setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
.setUsage(AudioAttributes.USAGE_ALARM)
.build()
val importance = NotificationManager.IMPORTANCE_DEFAULT
val channelList = mNotificationManager.notificationChannels
var i = 0
while (channelList != null && i < channelList.size) {
Log.d("channelList","channel ID is ${channelList[i].id}")
mNotificationManager.deleteNotificationChannel(channelList[i].id)
i++
}
Log.d("isnotification"," is it needed $isNotificationSoundNeeded importance is $importance")
val mChannel = NotificationChannel(CHANNEL_ID, appName, NotificationManager.IMPORTANCE_HIGH)
mChannel.setShowBadge(false)
mChannel.setSound(notifSound, audioAttributes)
val mChannelnew = NotificationChannel(CHANNEL_ID2, appName, NotificationManager.IMPORTANCE_DEFAULT)
mChannelnew.setShowBadge(false)
mChannelnew.setSound(notifSound, audioAttributes)
mNotificationManager.createNotificationChannel(mChannel)
我想念什么?有任何想法吗?谢谢
更新:这是notifsound的代码
val notifSound = Uri.parse("android.resource://" + packageName + "/" + R.raw.unconvinced)
最佳答案
首先,我不知道您的通知不适用于 Oreo , Pie 或以下版本(而不是 N )的设备。For your question StackOver Flow have lots of answer.
现在,根据您的问题,您仅丢失了一行代码,但是由于您尚未粘贴,因此此处无法检查您的整个通知代码。
我在这里粘贴一个通知代码,该代码恰好满足您所有的通知要求。 (完整自定义通知)
带有图像的通知
public void createNotificationWithImage(String title,String message ,Bitmap image) {
PendingIntent resultPendingIntent = PendingIntent.getActivity(mContext,
0 /* Request code */, resultIntent,
PendingIntent.FLAG_UPDATE_CURRENT);
// Custom Sound Uri
Uri soundUri = Uri.parse("android.resource://" + mContext.getApplicationContext()
.getPackageName() + "/" + R.raw.sniper_gun);
mBuilder = new NotificationCompat.Builder(mContext);
mBuilder.setSmallIcon(R.mipmap.notification_icon);
// Pay attention on below line here (NOTE)
mBuilder.setSound(soundUri);
if (image!=null) {
mBuilder.setContentTitle(title)
.setContentText(message)
.setAutoCancel(false)
.setLargeIcon(image)
.setStyle(new NotificationCompat.BigPictureStyle()
.bigPicture(image).setSummaryText(message).bigLargeIcon(null))
.setColor(Color.GREEN)
.setContentIntent(resultPendingIntent);
}
mNotificationManager = (NotificationManager) mContext.getSystemService(Context.NOTIFICATION_SERVICE);
现在,我将粘贴通知代码,该代码将在 OREO 设备之上或之上运行。
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
int importance = NotificationManager.IMPORTANCE_HIGH;
NotificationChannel notificationChannel = new NotificationChannel(NOTIFICATION_CHANNEL_ID, "NOTIFICATION_CHANNEL_NAME", importance);
notificationChannel.enableLights(true);
notificationChannel.setLightColor(Color.RED);
notificationChannel.enableVibration(true);
notificationChannel.setVibrationPattern(new long[]{100, 200, 300, 400, 500, 400, 300, 200, 400});
if(soundUri != null){
AudioAttributes audioAttributes = new AudioAttributes.Builder()
.setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
.setUsage(AudioAttributes.USAGE_ALARM)
.build();
notificationChannel.setSound(soundUri,audioAttributes);
}
assert mNotificationManager != null;
mBuilder.setChannelId(NOTIFICATION_CHANNEL_ID);
mNotificationManager.createNotificationChannel(notificationChannel);
}
assert mNotificationManager != null;
mNotificationManager.notify(0 /* Request Code */, mBuilder.build());
below middle braces use for close your method.
}
不含图片的通知
public void createNotification(String title,String message){
PendingIntent resultPendingIntent = PendingIntent.getActivity(mContext,
0 /* Request code */, resultIntent,
PendingIntent.FLAG_UPDATE_CURRENT);
Uri soundUri = Uri.parse("android.resource://" + mContext.getApplicationContext()
.getPackageName() + "/" + R.raw.sniper_gun);
mBuilder = new NotificationCompat.Builder(mContext);
mBuilder.setSmallIcon(R.mipmap.notification_icon);
mBuilder.setLargeIcon(BitmapFactory.decodeResource(mContext.getResources(),
R.mipmap.icon));
mBuilder.setSound(soundUri);
mBuilder.setContentTitle(title)
.setContentText(message)
.setAutoCancel(false)
.setColor(Color.GREEN)
.setStyle(new NotificationCompat.BigTextStyle())
.setContentIntent(resultPendingIntent);
mNotificationManager = (NotificationManager) mContext.getSystemService(Context.NOTIFICATION_SERVICE);
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
int importance = NotificationManager.IMPORTANCE_HIGH;
NotificationChannel notificationChannel = new NotificationChannel(NOTIFICATION_CHANNEL_ID, "NOTIFICATION_CHANNEL_NAME", importance);
notificationChannel.enableLights(true);
notificationChannel.setLightColor(Color.RED);
notificationChannel.enableVibration(true);
notificationChannel.setVibrationPattern(new long[]{100, 200, 300, 400, 500, 400, 300, 200, 400});
// notificationChannel.s
if(soundUri != null){
AudioAttributes audioAttributes = new AudioAttributes.Builder()
.setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
.setUsage(AudioAttributes.USAGE_ALARM)
.build();
notificationChannel.setSound(soundUri,audioAttributes);
}
assert mNotificationManager != null;
mBuilder.setChannelId(NOTIFICATION_CHANNEL_ID);
mNotificationManager.createNotificationChannel(notificationChannel);
}
assert mNotificationManager != null;
mNotificationManager.notify(0 /* Request Code */, mBuilder.build());
}
注意:在我的代码中,我提到过要注意的一点是,我描述要在通知中设置声音Uri。您可以这样描述。
mBuilder.setContentTitle(title)
.setContentText(message)
.setAutoCancel(false)
.setSound(soundUri)
.setColor(Color.GREEN)
.setStyle(new NotificationCompat.BigTextStyle())
.setContentIntent(resultPendingIntent);
但它不会为您播放声音,因为在Oreo设备之后未将声音设置为优先级。
因此,对于声音,请始终使用我所描述的代码。