Как установить разные звуки для разных уведомлений? - PullRequest
0 голосов
/ 29 апреля 2019

Я хочу установить другой звук уведомления на уведомление в зависимости от его содержимого.Я установил рабочий звук, если заголовок уведомления содержит цифры «1», «2» или «3».Тем не менее, звук должен быть другим, если заголовок содержит «Подтвердить», но он не работает, все еще звучит сигнал тревоги.

Uri defaultsound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
    Uri alarmsound = Uri.parse("android.resource://" + getApplicationContext().getPackageName() + "/" + R.raw.siren); //ContentResolver.SCHEME_ANDROID_RESOURCE +

    String NOTIFICATION_CHANNEL_ID = "com.example.bantay.bantay";
    NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

    NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this, NOTIFICATION_CHANNEL_ID);

    notificationBuilder.setAutoCancel(true);
    notificationBuilder.setPriority(Notification.PRIORITY_MAX);
    notificationBuilder.setDefaults(Notification.DEFAULT_ALL);
    notificationBuilder.setDefaults(Notification.DEFAULT_VIBRATE);
    notificationBuilder.setDefaults(Notification.DEFAULT_SOUND);
    notificationBuilder.setWhen(System.currentTimeMillis());
    notificationBuilder.setSmallIcon(R.drawable.marikinalogo);
    notificationBuilder.setContentTitle(title);
    notificationBuilder.setContentText(body);
    notificationBuilder.setVibrate(new long[]{500, 1000, 500, 1000, 500, 1000});

    if(title.toLowerCase().contains("1")){
        notificationBuilder.setContentIntent(pendingIntent);
        notificationBuilder.setSound(alarmsound);
    }
    else if(title.toLowerCase().contains("2")){
        notificationBuilder.setContentIntent(pendingIntent1);
        notificationBuilder.setSound(alarmsound);
    }
    else if(title.toLowerCase().contains("3")){
        notificationBuilder.setContentIntent(pendingIntent2);
        notificationBuilder.setSound(alarmsound);
    }
    else if(title.toLowerCase().contains("acknowledge")){
        notificationBuilder.setContentIntent(pendingIntent3);
        notificationBuilder.setSound(defaultsound);
    }

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        NotificationChannel notificationChannel = new NotificationChannel(NOTIFICATION_CHANNEL_ID, "Notification", NotificationManager.IMPORTANCE_HIGH);

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

        notificationChannel.enableLights(true);
        notificationChannel.setLightColor(Color.RED);
        notificationChannel.enableVibration(true);
        notificationChannel.setVibrationPattern(new long[]{500, 1000, 500, 1000, 500, 1000});

        if(title.toLowerCase().contains("1")){
            notificationChannel.setSound(alarmsound, audioAttributes);
        }
        else if(title.toLowerCase().contains("2")){
            notificationChannel.setSound(alarmsound, audioAttributes);
        }
        else if(title.toLowerCase().contains("3")){
            notificationChannel.setSound(alarmsound, audioAttributes);
        }
        else{
            notificationChannel.setSound(defaultsound, audioAttributes);
        }
        notificationManager.createNotificationChannel(notificationChannel);
    }
...