说明:
我通过fcm发送消息。
当用户收到消息时,会显示通知并播放特定的(应用程序原始文件夹中的多个音频文件中的)音频文件。
要播放不同的音频消息,我只需更改下面代码中 jUri 中提供的文件名。
这在 Android O 下有效,但在 => O 上有效
我想要什么:
1. 如何请求运行时权限为不同的消息播放这些不同的音频文件?
2. 另外,当我手动授予权限时,即使 jUri 文件名不同,我也会听到相同的音频消息?
3.NotificationChannel中的CHANNEL_ID和CHANNEL_NAME有什么区别?
4.“if()”下面的代码是否应该检查,是否位于“else”部分内?
以下是我的代码:
Uri jUri = Uri.parse(ContentResolver.SCHEME_ANDROID_RESOURCE+ "://" +getApplicationContext().getPackageName()+"/"+ R.raw.p);
String classnm = " FirebaseMessagingService ";
int NOTIFICATION_ID = 1;
Log.d("loog", classnm + "747 " + ContentResolver.SCHEME_ANDROID_RESOURCE+ "://" +getApplicationContext().getPackageName()+"/"+ R.raw.p );
Log.d("loog", classnm + "748 " + jUri );
NotificationManager notificationManager = (NotificationManager) this.getSystemService(this.NOTIFICATION_SERVICE);
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
Log.d("loog", classnm + "690 ");
int importance = NotificationManager.IMPORTANCE_HIGH;
NotificationChannel mChannel = new NotificationChannel(CHANNEL_ID, CHANNEL_NAME, importance);
mChannel.setDescription(CHANNEL_DESCRIPTION);
mChannel.enableLights(true);
mChannel.setLightColor(Color.RED);
mChannel.enableVibration(true);
// mChannel.setVibrationPattern(new long[]{100, 200, 300, 400, 500, 400, 300, 200, 400});
mChannel.setShowBadge(false);
AudioAttributes audioAttributes = new AudioAttributes.Builder()
.setUsage(AudioAttributes.USAGE_NOTIFICATION)
.build();
mChannel.setSound(jUri, audioAttributes);
if (notificationManager != null)
notificationManager.createNotificationChannel(mChannel);
}
Log.d("loog", classnm + "703 ");
NotificationCompat.Builder builder = new NotificationCompat.Builder(this, CHANNEL_ID)
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle(title)
.setSound(jUri)
.setContentText(message);
Log.d("loog", classnm + "710 ");
Intent resultIntent = new Intent(this, MainActivity.class);
TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
stackBuilder.addParentStack(MainActivity.class);
stackBuilder.addNextIntent(resultIntent);
PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
builder.setContentIntent(resultPendingIntent);
notificationManager.notify(NOTIFICATION_ID, builder.build());
Log.d("loog", classnm + "721 ");
最佳答案
对于您注意到的第二点,相同的声音文件在不同的 channel 名称上运行,只需动态传递 CHANNEL_ID 和 CHANNEL_NAME 即可。
关于Android NotificationChannel 在运行时请求多个 channel 的权限?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59004387/