Android push-заголовок/уведомление о начале не работает

#android #push-notification #android-notifications #heads-up-notifications

Вопрос:

В настоящее время я пытаюсь получить уведомление о заголовке на Nexus S (API 29), но безуспешно. Я видел несколько потоков с различными решениями, но ни одно из них не работало. Код выглядит следующим образом:

 Context applicationContext = getApplicationContext();    
NotificationCompat.Builder builder = new NotificationCompat.Builder(applicationContext, CHANNEL_ID)
                                .setDefaults(NotificationCompat.DEFAULT_ALL)
                                .setDefaults(NotificationCompat.DEFAULT_SOUND)
                                .setSmallIcon(R.drawable.ic_account_circle)
                                .setContentTitle(notificationTitle)
                                .setContentText(notificationTextBody)
                                .setPriority(NotificationCompat.PRIORITY_HIGH)
                                .setLights(Color.RED, 3000, 3000);

NotificationManagerCompat notificationManager = NotificationManagerCompat.from(applicationContext);



int notificationId = new Random().nextInt();
//launches the notification on the device
notificationManager.notify(notificationId, builder.build());
 

Я пробовал включить звук, вибрации, чтобы установить приоритет как высокий или максимальный. Я даже использовал этот метод setFullScreenIntent , отдельно и все сразу, но ничего не достиг, чего я ищу.

Комментарии:

1. покажите, как вы настраиваете свой канал с помощью CHANNEL_ID

2. private static final String CHANNEL_ID = "Test_Channel_ID_Worker";

Ответ №1:

на Android 8 вам нужно создать канал уведомлений, как в DOC

 private void createNotificationChannel() {
    // Create the NotificationChannel, but only on API 26  because
    // the NotificationChannel class is new and not in the support library
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        CharSequence name = getString(R.string.channel_name);
        String description = getString(R.string.channel_description);
        int importance = NotificationManager.IMPORTANCE_DEFAULT;
        NotificationChannel channel = new NotificationChannel(CHANNEL_ID, name, importance);
        channel.setDescription(description);
        // Register the channel with the system; you can't change the importance
        // or other notification behaviors after this
        NotificationManager notificationManager = getSystemService(NotificationManager.class);
        notificationManager.createNotificationChannel(channel);
    }
}
 

обратите внимание, что канал также имеет важное значение. если вы хотите получить уведомление о начале, вы должны установить высокий приоритет для этого канала, ДОК

Примеры условий, которые могут вызвать уведомления о предупреждениях, включают следующее:

Канал уведомлений имеет большое значение на устройствах под управлением Android 8.0 (уровень API 26) и выше.