Android: уведомления не генерируют звук по умолчанию - PullRequest
0 голосов
/ 14 мая 2018

Я пытаюсь генерировать уведомления FCM со звуком. Я получаю уведомление и т.д. без проблем, но нет звука вообще. У меня все в порядке со звуком уведомлений по умолчанию. Пожалуйста, проверьте код ниже. Это для API 26 и выше.

NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

                // The id of the channel.
                String id = "xxx";

                // The user-visible name of the channel.
                CharSequence name = "xxx";

                // The user-visible description of the channel.
                String description = "xxx";

                int importance = NotificationManager.IMPORTANCE_DEFAULT;

                NotificationChannel mChannel = new NotificationChannel(id, name, importance);

                // Configure the notification channel.
                mChannel.setDescription(description);

                mChannel.enableLights(true);
                // Sets the notification light color for notifications posted to this
                // channel, if the device supports this feature.
                mChannel.setLightColor(Color.RED);

                mChannel.enableVibration(true);
                mChannel.setVibrationPattern(new long[]{100, 200, 300, 400, 500, 400, 300, 200, 400});


                mNotificationManager.createNotificationChannel(mChannel);

                mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);



                // The id of the channel.
                String CHANNEL_ID = "xxx";

                // Create a notification and set the notification channel.
                Notification notification = new Notification.Builder(this,"xxx")
                        .setSmallIcon(R.drawable.volusha_notifications)
                        .setContentText(text)
                        .setChannelId(CHANNEL_ID)
                        .setContentIntent(pendingIntent)
                        .setContentTitle(title)
                        .setAutoCancel(true)
                        .build();

                // Issue the notification.
                mNotificationManager.notify(new Random().nextInt(), notification);

Почему это происходит и как получить звук по умолчанию?

Ответы [ 2 ]

0 голосов
/ 14 мая 2018

Попробуйте использовать RingtoneManager, чтобы получить Uri уведомления по умолчанию как:

Uri uri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
builder.setSound(uri);
0 голосов
/ 14 мая 2018

Изменить эту часть:

// Create a notification and set the notification channel.
                Notification notification = new Notification.Builder(this,"xxx")
                        .setSmallIcon(R.drawable.volusha_notifications)
                        .setContentText(text)
                        .setChannelId(CHANNEL_ID)
                        .setContentIntent(pendingIntent)
                        .setContentTitle(title)
                        .setAutoCancel(true)
                        .build();

на

// Create a notification and set the notification channel.
                Notification notification = new Notification.Builder(this,"xxx")
                        .setSmallIcon(R.drawable.volusha_notifications)
                        .setContentText(text)
                        .setChannelId(CHANNEL_ID)
                        .setContentIntent(pendingIntent)
                        .setContentTitle(title)
                        .setSound(RingtoneManager
                              .getDefaultUri(RingtoneManager.TYPE_NOTIFICATION))
                        .setAutoCancel(true)
                        .build();
...