我有这个应用程序,我想要设置自定义通知声音,
我有这个方法检查Android版本并相应地创建通知..
在Android 6及更低版本上工作正常并播放自定义声音。
但在Android 7及更高版本上它没有播放我想要的自定义通知声音
1-堆栈溢出的答案都没有解决这个问题
2-我的代码是以android开发人员docs指定的最佳方式编写的
3-我的文件是.wav及其44100HZ
这是代码,我缺少什么?
private void createLocalNotificationx(Intent intent) {
NotificationManager notificationManager;
PendingIntent pendingIntent = PendingIntent.getActivity(this, (int) System.currentTimeMillis(), intent, 0);
Uri alarmSound;
if (type.equals(VALUE_ROUTINE)) {
alarmSound = Uri.parse(ContentResolver.SCHEME_ANDROID_RESOURCE
+ "://" + getPackageName() + "/" + R.raw.alarm);
} else {
alarmSound = Uri.parse(ContentResolver.SCHEME_ANDROID_RESOURCE
+ "://" + getPackageName() + "/" + R.raw.emergency);
}
if (android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.O) {
notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
Notification mNotification = new Notification.Builder(this)
.setOngoing(false)
.setContentTitle(title)
.setContentText(body)
.setSmallIcon(R.drawable.ic_launcher)
.setContentIntent(pendingIntent)
.setAutoCancel(true)
.setSound(alarmSound).setPriority(Notification.PRIORITY_DEFAULT)
.build();
notificationManager.notify(0, mNotification);
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
Log.d(TAG, "alarmSound =" + alarmSound.getPath());
/*First create the notification channel:*/
String CHANNEL_ID = getPackageName() + "/ID";
String name = getString(R.string.app_name);
int importance = NotificationManager.IMPORTANCE_DEFAULT;
NotificationChannel mChannel = new NotificationChannel(CHANNEL_ID, name, importance);
mChannel.enableVibration(true);
mChannel.setVibrationPattern(new long[]{100, 200, 500});
mChannel.enableLights(true);
// Register the channel with the system; you can't change the importance
// or other notification behaviors after this
notificationManager.createNotificationChannel(mChannel);
Notification mNotification = new Notification.Builder(this, CHANNEL_ID)
.setDefaults(Notification.DEFAULT_ALL | Notification.DEFAULT_LIGHTS)
.setOngoing(false)
.setContentTitle(title)
.setContentText(body)
.setSound(alarmSound)
.setSmallIcon(R.drawable.ic_launcher)
.setContentIntent(pendingIntent)
.build();
notificationManager.notify(0, mNotification);
}
}
注意:它可能是更干净的代码,但应该没问题!
答案 0 :(得分:0)
从“奥利奥”开始,您无法为通知设置自定义音调。但是您可以为特定频道设置自定义音调。该频道的所有通知都将具有此自定义通知音。 要为频道设置音调,请按照以下步骤操作:
martin@MARWIN:~$ python skript.py
INFO:requests.packages.urllib3.connectionpool:Starting new HTTP connection (1): 10.10.10.10
DEBUG:requests.packages.urllib3.connectionpool:"POST /canon/ij/command2/port1 HTTP/1.1" 200 None
INFO:requests.packages.urllib3.connectionpool:Resetting dropped connection: 10.10.10.10
DEBUG:requests.packages.urllib3.connectionpool:"GET /canon/ij/command2/port1 HTTP/1.1" 409 None
答案 1 :(得分:-1)
查看您的.setDefaults()
方法:
Notification mNotification = new Notification.Builder(this, CHANNEL_ID)
.setDefaults(Notification.DEFAULT_ALL | Notification.DEFAULT_LIGHTS)
可能Notification.DEFAULT_ALL
导致您的问题。