Урок 119. PendingIntent – флаги, requestCode. AlarmManager

Обсуждение уроков
alaya
Сообщения: 2
Зарегистрирован: 19 дек 2019, 14:53

Re: Урок 119. PendingIntent – флаги, requestCode. AlarmManager

Сообщение alaya » 13 мар 2020, 14:24

Возможно кому-то окажется полезным.
Постоянно сталкиваюсь с проблемами из-за постоянно устаревающих методов и прочих изменений.
В этот раз телефон отказывался показывать уведомления, перекопала лог, ошибок никаких не выдало. Оказалось проблема в версии андройда.
Для API 26+ требуется создать канал уведомлений, иначе они уходят в dev/null.

Код: Выделить всё

void sendNotif(int id, PendingIntent pIntent){
        createNotificationChannel(this);
        Notification builder = new NotificationCompat.Builder(this, NOTIFICATION_CHANNEL_ID)
                .setContentTitle("title" + id)
                .setContentText("content" + id)
                .setSmallIcon(R.mipmap.ic_launcher)
                .setAutoCancel(true)
                .setContentIntent(pIntent).build();
                nm.notify(id, builder);
    }
    
Канал уведомлений:

Код: Выделить всё

public static final String NOTIFICATION_CHANNEL_ID = "NOTIFICATION_CHANNEL";
    public static void createNotificationChannel(final Context context) {
        // 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 = "name";
            String description = "description";
            int importance = NotificationManager.IMPORTANCE_DEFAULT;
            NotificationChannel channel = new NotificationChannel(NOTIFICATION_CHANNEL_ID, name, importance);
            channel.setDescription(description);
            NotificationManager notificationManager = context.getSystemService(NotificationManager.class);
            notificationManager.createNotificationChannel(channel);
        }
    }
    

Ответить