Уведомление о кастомном звуке и вибрации не работает? - PullRequest
0 голосов
/ 28 апреля 2019

У меня есть приложение, которое получает уведомление, все работает.Однако пользовательский звук и вибрация не работают.Я тестирую его на Android 9 pie.

Uri sound = Uri.parse("android.resource://" +getApplicationContext().getPackageName()+ "/" +R.raw.siren); //ContentResolver.SCHEME_ANDROID_RESOURCE +

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

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

    notificationBuilder.setAutoCancel(true);
    notificationBuilder.setWhen(System.currentTimeMillis());
    notificationBuilder.setSmallIcon(R.drawable.marikinalogo);
    notificationBuilder.setContentTitle(title);
    notificationBuilder.setContentText(body);
    notificationBuilder.setSound(sound);
    notificationBuilder.setVibrate(new long[]{500, 1000, 500, 1000});
    if(title.toLowerCase().contains("1")){
        notificationBuilder.setContentIntent(pendingIntent);
    }
    else{
        notificationBuilder.setContentIntent(pendingIntent3);
    }

    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_ALARM)
                .build();

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

    notificationManager.notify(0, notificationBuilder.build());

Я не знаю своих ошибок.Влияет ли NOTIFICATION_CHANNEL_ID на поведение уведомления?

1 Ответ

0 голосов
/ 29 апреля 2019

Я использую MediaPlayer для пользовательского звука.И это работает хорошо для меня.Это работает для всех устройств.

private MediaPlayer player;

Для воспроизведения пользовательского звука:

 try {
       Uri uri = Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.siren);

       player = MediaPlayer.create(this, uri);
       player.setLooping(true); // This will play sound in repeatable mode.
       player.start();

//     mBuilder.setSound(uri);
     } catch (Exception e) {
        e.printStackTrace();
    }

Для звука остановки:

  if (player != null)
      player.stop();

Это работает для меня.Надеюсь, это также поможет вам.

...