Android NotificationChannel在运行时询问多个频道的权限?
说明:Android NotificationChannel在运行时询问多个频道的权限?,android,push-notification,notifications,android-permissions,Android,Push Notification,Notifications,Android Permissions,说明: 我通过fcm发送消息。 当用户收到消息时,将显示通知,并播放特定的(应用程序原始文件夹中的多个音频文件中的)音频文件。 要播放不同的音频消息,我只需在下面的代码中更改jUri中提供的文件名。 这在Android O下工作,但在=>O 我想要什么: 1.如何请求运行时权限为不同的消息播放这些不同的音频文件? 2.此外,当我手动授予权限时,即使jUri文件名不同,我也会听到相同的音频消息? 3.NotificationChannel中的频道ID和频道名称有什么区别? 4.是否应检查“if()
我通过fcm发送消息。
当用户收到消息时,将显示通知,并播放特定的(应用程序原始文件夹中的多个音频文件中的)音频文件。
要播放不同的音频消息,我只需在下面的代码中更改jUri中提供的文件名。
这在Android O下工作,但在=>O
我想要什么:
1.如何请求运行时权限为不同的消息播放这些不同的音频文件?
2.此外,当我手动授予权限时,即使jUri文件名不同,我也会听到相同的音频消息?
3.NotificationChannel中的频道ID和频道名称有什么区别?
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 ");
对于您注意到的第二点,相同的声音文件在不同的频道名称上运行,只需动态传递频道ID和频道名称