Невозможно создать уведомления в android - PullRequest
0 голосов
/ 14 июля 2020

Я набрал следующую функцию для получения уведомлений в android.

Но я получаю сообщение об ошибке: Не удалось отправить уведомление на канал «null» . Я знаю, что мне не хватает некоторого фрагмента кода, который, кажется, вставлен для устройств, работающих на некоторых более новых версиях.

Могу ли я получить точный код для вставки?

void create_notification(String string){
        Intent intent = new Intent(this, MainActivity.class);
    TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
    stackBuilder.addParentStack(MainActivity.class);
    stackBuilder.addNextIntent(intent);
    PendingIntent pendingIntent =
            stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT
            );
    Notification notification = new Notification.Builder(this)
            .setSmallIcon(R.mipmap.ic_launcher)
            .setContentTitle(getString(R.string.app_name))
            .setAutoCancel(true)
            .setPriority(Notification.PRIORITY_MAX)
            .setDefaults(Notification.DEFAULT_VIBRATE)
            .setContentIntent(pendingIntent)
            .setContentText(string)
            .build();
    NotificationManager notificationManager =
            (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    notificationManager.notify(NOTIFICATION_ID, notification);
}

Ответы [ 2 ]

0 голосов
/ 14 июля 2020

В вашем конструкторе вы можете добавить идентификатор канала, например:

Notification notification = new Notification.Builder(this)
            .setSmallIcon(R.mipmap.ic_launcher)
            .setContentTitle(getString(R.string.app_name))
            .setAutoCancel(true)
            .setChannelId("base_channel") //<-- set the name you feel is proper to your project
            .setPriority(Notification.PRIORITY_MAX)
            .setDefaults(Notification.DEFAULT_VIBRATE)
            .setContentIntent(pendingIntent)
            .setContentText(string)
            .build();
0 голосов
/ 14 июля 2020

Документация (первый результат в Google по запросу «android канал уведомления») дает следующий пример:

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);
    }
}

И затем в вашем уведомлении вам необходимо укажите ID канала:

Notification notification = new Notification.Builder(this, CHANNEL_ID)
   ...

А вы это пробовали?

...