Android NotificationChannel 在运行时请求多个通道的权限?
Android NotificationChannel ask permission at runtime for multiple channels?
解释:
我通过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_ID 和 CHANNEL_NAME。
解释:
我通过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_ID 和 CHANNEL_NAME。