web-dev-qa-db-ja.com

android通知チャンネルサウンドが機能しない)

私は問題について多くの投稿があることを知っています。私はそれらすべてを試しました。これが私がしたステップです。

まず、チャンネルが作成されたら、変更することはできません。唯一の方法はアプリを再インストールすることでした。だからそれは私がしたことです、そしてそれはうまくいきませんでした。

第二に、私はこのコードを使っても私がチャネルを削除できると言う人もいます

val channelList = mNotificationManager.notificationChannels
        var i = 0
        while (channelList != null && i < channelList.size) {
            Log.d("channelList","channel ID is ${channelList[i].id}")
            //mNotificationManager.deleteNotificationChannel(channelList[i].id)
            i++
        }
 _

その後、削除後のチャンネルを再作成します。

第3に、新しい通知チャネルを使用してみましたが、新しいチャンネルを使用するたびにエラーが発生し続けました。

これが私が試したすべての解決策と一緒に使っているコードです

 val audioAttributes = AudioAttributes.Builder()
                .setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
                .setUsage(AudioAttributes.USAGE_ALARM)
                .build()


        val importance = NotificationManager.IMPORTANCE_DEFAULT

        val channelList = mNotificationManager.notificationChannels
        var i = 0
        while (channelList != null && i < channelList.size) {
            Log.d("channelList","channel ID is ${channelList[i].id}")
            mNotificationManager.deleteNotificationChannel(channelList[i].id)
            i++
        }

        Log.d("isnotification"," is it needed $isNotificationSoundNeeded importance is $importance")
        val mChannel = NotificationChannel(CHANNEL_ID, appName,  NotificationManager.IMPORTANCE_HIGH)
        mChannel.setShowBadge(false)
        mChannel.setSound(notifSound, audioAttributes)



        val mChannelnew = NotificationChannel(CHANNEL_ID2, appName,  NotificationManager.IMPORTANCE_DEFAULT)
        mChannelnew.setShowBadge(false)
        mChannelnew.setSound(notifSound, audioAttributes)




        mNotificationManager.createNotificationChannel(mChannel)
 _

何が行方不明ですか?何か案は?ありがとう

更新:ここでNotifSoundのコードです

val notifSound = Uri.parse("Android.resource://" + packageName + "/" + R.raw.unconvinced)
 _
9
thenewbie

オーディオ属性を使用する必要があり、アクセス権URIも許可を与えられる必要があります。

だから最初に着信音URIを定義します。

Uri ringtoneUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
boolean vibrate = true;
long[] vibratePattern = new long[]{0L, 1000L};



public constructor(){
 notificationBuilder = new NotificationCompat.Builder(mContext, app.getAppContext().getString(R.string.default_notification_channel_id));
        mNotifyManager = (NotificationManager) mContext.getSystemService(Context.NOTIFICATION_SERVICE);
        mContext.grantUriPermission("com.Android.systemui", ringtoneUri, Intent.FLAG_GRANT_READ_URI_PERMISSION);
}
 public void showNotificationNormal(String notificationTitle, String notificationBody, Intent intent) {
    String id = mContext.getString(R.string.default_notification_channel_id);
    PendingIntent lowIntent = PendingIntent.getActivity(mContext, 100, intent, PendingIntent.FLAG_CANCEL_CURRENT);
    NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(mContext, id);
    NotificationManager mNotifyManager = (NotificationManager) mContext.getSystemService(Context.NOTIFICATION_SERVICE);

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        CharSequence name = mContext.getString(R.string.default_notification_channel_name);
        String description = mContext.getString(R.string.default_notification_channel_description); //user visible
        int importance = NotificationManager.IMPORTANCE_HIGH;

        AudioAttributes att = new AudioAttributes.Builder()
                .setUsage(AudioAttributes.USAGE_NOTIFICATION)
                .setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
                .build();

        NotificationChannel mChannel = new NotificationChannel(id, name, importance);
        mChannel.setDescription(description);
        mChannel.enableLights(true);
        mChannel.enableVibration(vibrate);
        mChannel.setVibrationPattern(vibratePattern);
        mChannel.setLightColor(Color.RED);
        mChannel.setSound(ringtoneUri, att);
        mChannel.setBypassDnd(true);
        mChannel.setLockscreenVisibility(NotificationCompat.VISIBILITY_PUBLIC);
        mChannel.setShowBadge(true);

        if (mNotifyManager != null) {
            mNotifyManager.createNotificationChannel(mChannel);
        }

        notificationBuilder
                .setSmallIcon(R.mipmap.ic_launcher)
                .setPriority(NotificationCompat.PRIORITY_MAX)
                .setCategory(NotificationCompat.CATEGORY_MESSAGE)
                .setVibrate(vibratePattern)
                .setSound(ringtoneUri)
                .setColor(ContextCompat.getColor(mContext, R.color.colorPrimary))
                .setContentTitle(notificationTitle)
                .setStyle(new NotificationCompat.BigTextStyle().bigText(notificationBody))
                .setAutoCancel(true)
                .setVisibility(NotificationCompat.VISIBILITY_PUBLIC)
                .setContentIntent(lowIntent);

    } else {
        notificationBuilder.setContentTitle(notificationTitle)
                .setSmallIcon(R.mipmap.ic_launcher)
                .setPriority(NotificationCompat.PRIORITY_MAX)
                .setCategory(NotificationCompat.CATEGORY_MESSAGE)
                .setVibrate(vibratePattern)
                .setSound(ringtoneUri)
                .setStyle(new NotificationCompat.BigTextStyle().bigText(notificationBody))
                .setColor(ContextCompat.getColor(mContext, R.color.colorPrimary))
                .setAutoCancel(true)
                .setVisibility(NotificationCompat.VISIBILITY_PUBLIC)
                .setContentIntent(lowIntent);

    }

    notificationBuilder.setContentText(notificationBody);

    if (mNotifyManager != null) {
        mNotifyManager.notify(AppConstants.NOTIFY_ID, notificationBuilder.build());
    }
}
 _
0