如何为不同的通知设置不同的声音?

时间:2019-04-29 11:30:04

标签: android android-notifications android-notification-bar

我想根据通知的内容为通知设置不同的通知声音。如果通知的标题包含数字“ 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);
    }

0 个答案:

没有答案