【发布时间】:2020-04-12 08:14:13
【问题描述】:
我希望用户通过选择通知的声音来自定义他们的应用,但我不知道如何制作声音列表以及如何将它们设置为通知的声音。我已经搜索并尝试了各种方法,但都没有成功,请帮助我!
这是我显示通知的代码:
NotificationCompat.Builder builder = new NotificationCompat.Builder(getApplicationContext(), channelID)
.setSmallIcon(R.drawable.logo) /// Set notification's icon
.setSound(uri) /// Set sound
.setAutoCancel(true) /// Allow sound to auto cancel
.setVibrate(new long[]{1000, 1000, 1000, 1000, 1000}) /// Set sound vibration
.setOnlyAlertOnce(true) /// Only alert Once
.setContentIntent(pendingIntent) /// Set intent it will show when click notification
.setContent(getCustomDesign(title, message)); /// Set the design of notification
/// Manage notification
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
/// Check if android version is Oreo or upper to show notification via NotificationChannel
/// For each channel, you can set the and auditory behavior that is applied to all notifications in that channel
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
/// Set channel's ID, name, and importance
NotificationChannel notificationChannel = new NotificationChannel(channelID, "DEMO", notificationManager.IMPORTANCE_HIGH);
/// Set sound for channel
notificationChannel.setSound(uri, null);
/// Create channel
notificationManager.createNotificationChannel(notificationChannel);
}
/// Show the notification
notificationManager.notify(0, builder.build());
我尝试制作铃声选择器的意图:
btnChange.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(RingtoneManager.ACTION_RINGTONE_PICKER);
intent.putExtra(RingtoneManager.EXTRA_RINGTONE_TYPE, RingtoneManager.TYPE_NOTIFICATION);
intent.putExtra(RingtoneManager.EXTRA_RINGTONE_TITLE, "Select notification tone");
intent.putExtra(RingtoneManager.EXTRA_RINGTONE_EXISTING_URI, (Uri) null);
startActivityForResult(intent, 5);
}
});
和 onActivityResult:
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent intent) {
super.onActivityResult(requestCode, resultCode, intent);
if (resultCode == NotificationActivity.RESULT_OK && requestCode == 5) {
Uri uri = intent.getParcelableExtra(RingtoneManager.EXTRA_RINGTONE_PICKED_URI);
if (uri != null) {
//NotificationActivity.this.chosenRingtone = uri.toString();
RingtoneManager.setActualDefaultRingtoneUri(this, RingtoneManager.TYPE_NOTIFICATION, uri);
} else {
//this.chosenRingtone = null;
}
}
}
但是Logcat告诉我的项目没有被授予这个权限:android.permission.WRITE_SETTINGS。
【问题讨论】:
-
好的,我已经编辑好了
标签: android firebase notifications