gpt4 book ai didi

Android NotificationChannel 在运行时请求多个 channel 的许可?

转载 作者:行者123 更新时间:2023-12-04 10:58:35 29 4
gpt4 key购买 nike

解释:
我通过 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/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com