我有一个应用程序,在其中我更新了通知声音(添加了自定义声音)。现在的问题是,当我收到一条通知时,我听到双声,第一声是我早些,第二声是我添加了新声。经过搜索和谷歌搜索,我知道我们需要卸载并重新安装该应用程序才能使“新”声音正常工作。显而易见,我不能要求2k +用户重新安装该应用程序。那么有办法解决吗?我猜notificationmanager存储了一些临时数据来处理/播放通知(我想我不知道)。
这是我的代码
public void sendNotification(String string, Context context) {
// NotificationManager notificationManager2 = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
NotificationManager notificationManager = (NotificationManager) context.getSystemService(context.NOTIFICATION_SERVICE);
notificationManager.cancel(notificationId);
if (soundUri == null) {
soundUri = Uri.parse("android.resource://" + context.getPackageName() + "/" + R.raw.mycustomsound);
}
// Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
String channelId = "channel-01";
String channelName = "Channel Name";
int importance = NotificationManager.IMPORTANCE_HIGH;
Intent intent = new Intent(context, MainFrag.class);
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, 0);
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
NotificationChannel mChannel = new NotificationChannel(
channelId, channelName, importance);
AudioAttributes attributes = new AudioAttributes.Builder()
.setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
.setUsage(AudioAttributes.USAGE_NOTIFICATION)
.build();
mChannel.setSound(soundUri, attributes);
notificationManager.createNotificationChannel(mChannel);
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context, channelId)
.setSmallIcon(R.drawable.ic_launcher)
.setContentTitle("My Title")
.setContentText("Hello there!")
.setContentIntent(pendingIntent)
.setAutoCancel(true)
.setDefaults(0)
.setGroupAlertBehavior(GROUP_ALERT_SUMMARY);
TaskStackBuilder stackBuilder = TaskStackBuilder.create(context);
stackBuilder.addNextIntent(intent);
mBuilder.setContentIntent(pendingIntent);
notificationManager.notify(notificationId, mBuilder.build());
} else {
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context, channelId)
.setSmallIcon(R.drawable.ic_launcher)
.setContentTitle("Title")
.setContentText("Hello there!")
.setContentIntent(pendingIntent)
.setAutoCancel(true)
.setDefaults(0)
.setGroupAlertBehavior(GROUP_ALERT_SUMMARY);
// .setSound(defaultSoundUri);
.setSound(Uri.parse(ContentResolver.SCHEME_ANDROID_RESOURCE
+ "://" + context.getPackageName() + "/raw/mycustomsound"));
TaskStackBuilder stackBuilder = TaskStackBuilder.create(context);
stackBuilder.addNextIntent(intent);
mBuilder.setContentIntent(pendingIntent);
notificationManager.notify(notificationId, mBuilder.build());
}
}
而且,有时我还会收到鬼通知(队列中播放了4-5次声音!所以我的代码中一定会有毛刺。