Android NotificationChannel 在运行时请求多个 channel 的权限?

标签 android push-notification notifications android-permissions

说明:
我通过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/

相关文章:

android - 将 Android OkHttp3 与 NewRelic 集成

java - 自定义适配器在 ArrayList 准备好之前启动

android - Websocket 服务器作为 Azure 上的服务?

ios - 从 Go appengine 站点发送 Apple 推送通知

ios - 通过声音和振动推送通知

google-chrome-extension - 迁移 Chrome 扩展 v3 通知

android - 如何在安装时启动android服务

android - 根据选择器中选择的应用程序,将不同的文本设置为 ACTION_SEND Intent

android - 在多个设备上同步通知

facebook - 发送通知