我想从我的应用程序中的原始 mp3 或 wav 文件设置自定义通知声音。下面是我的代码
private void sendMyNotification(String message) {
Intent intent;
if (sharedPreferences.getBoolean(SPConstants.IS_LOGGED_IN, false)) {
intent = new Intent(this, ActivityNotification.cl);
} else {
intent = new Intent(this, ActivitySplash.cl);
}
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_ONE_SHOT);
Uri soundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
soundUri = Uri.p("android.resource://" + getApplicationContext().getPackageName() + "/" + R.raw.panic);
AudioManager manager = (AudioManager)getSystemService(Context.AUDIO_SERVICE);
manager.setStreamVolume(AudioManager.STREAM_MUSIC, 100, 0);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this, CHANNEL_ID)
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle(getString(R.string.app_name))
.setContentText(message)
.setAutoCancel(true)
.setSound(soundUri)
.setContentIntent(pendingIntent);
NotificationManager notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel mChannel = new NotificationChannel(CHANNEL_ID, channelName, NotificationManager.IMPORTANCE_HIGH);
notificationManager.createNotificationChannel(mChannel);
}
notificationManager.notify(0, notificationBuilder.build());
}
恐慌音频文件驻留在 res-& gt;raw。我已经尝试使用声音的 mp3 和 wav 格式,但似乎没有任何工作来设置通知声音。我目前正在 Pixel 2 OS 8.1 上进行测试。
任何建议可能是什么问题?
测试吹代码,并按预期与我合作。
添加内容意图和仍然工作没有任何问题与我。
private void sendMyNotification(String message) {
Intent intent = new Intent(this, SplashActivity.cl);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_ONE_SHOT);
Uri soundUri = Uri.p("android.resource://" + getApplicationContext().getPackageName() + "/" + R.raw.correct_answer);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this, "CH_ID")
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle(getString(R.string.app_name))
.setContentText(message)
.setAutoCancel(true)
.setSound(soundUri)
.setContentIntent(pendingIntent);
NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
if(soundUri != null){
// Changing Default mode of notification
notificationBuilder.setDefaults(Notification.DEFAULT_VIBRATE);
// Creating an Audio Attribute
AudioAttributes audioAttributes = new AudioAttributes.Builder()
.setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
.setUsage(AudioAttributes.USAGE_ALARM)
.build();
// Creating Channel
NotificationChannel notificationChannel = new NotificationChannel("CH_ID","Testing_Audio",NotificationManager.IMPORTANCE_HIGH);
notificationChannel.setSound(soundUri,audioAttributes);
mNotificationManager.createNotificationChannel(notificationChannel);
}
}
mNotificationManager.notify(0, notificationBuilder.build());
}
Update
您可能需要卸载应用程序以更改声音设置,请查看这些link以了解更多详细信息。
简单的答案:
Uri soundUri = Uri.p(
"android.resource://" +
getApplicationContext().getPackageName() +
"/" +
R.raw.push_sound_file);
AudioAttributes audioAttributes = new AudioAttributes.Builder()
.setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
.setUsage(AudioAttributes.USAGE_ALARM)
.build();
// Creating Channel
NotificationChannel channel = new NotificationChannel("YOUR_CHANNEL_ID",
"YOUR_CHANNEL_NAME",
NotificationManager.IMPORTANCE_HIGH);
channel.setSound(soundUri, audioAttributes);
((NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE))
.createNotificationChannel(notificationChannel);
晚了,但可能是有帮助的一些人,只需添加下面的行在你的 NotificationCompat.Builder()实例:
.setSound("your sound uri",AudioManager.STREAM_NOTIFICATION)
注意:由于NotificationCompat.Builder()
向后兼容,因此不需要通知通道中的AudioAttributes
等
本站系公益性非盈利分享网址,本文来自用户投稿,不代表边看边学立场,如若转载,请注明出处
评论列表(25条)