Alexander_Kolmachikhin
@Alexander_Kolmachikhin
Android Programming

Почему не получается сгруппировать уведомления?

Я не могу реализовать объединение уведомлений в группу. Причем уведомления перестали стакаться когда их 4 или больше, видимо, потому что я задал новую группу. Но она не работает!

Вот мой код:
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            notificationHelper.createChannel(); // созаю канал
            notificationHelper
                    .getManager()
                    .notify(-271, notificationHelper.createGroup().build()); // создаю группу
        }

        notificationHelper
                .getManager()
                .notify(id, notificationHelper.createNotification(id, title, message, icon).build());  // создаю уведомление


Метод createChannel:
@TargetApi(Build.VERSION_CODES.O)
    public void createChannel() {
        NotificationChannel channel = new NotificationChannel(CHANNEL_ID, CHANNEL_NAME, NotificationManager.IMPORTANCE_HIGH);
        channel.enableLights(true);
        channel.setLightColor(Color.RED);
        channel.setDescription(CHANNEL_DESCRIPTION);
        channel.enableVibration(true);
        channel.setVibrationPattern(new long[]{0, 1000, 500, 1000});
        channel.setLockscreenVisibility(Notification.VISIBILITY_PUBLIC);
        getManager().createNotificationChannel(channel);
    }


Метод createGroup:
public NotificationCompat.Builder createGroup() {
        return new NotificationCompat.Builder(getApplicationContext(), "-100")
                .setContentInfo(CHANNEL_NAME)
                .setSmallIcon(R.drawable.icon_default)
                .setChannelId(CHANNEL_ID)
                .setGroup(GROUP_KEY)
                .setGroupSummary(true);
    }


Метод createNotification:
public NotificationCompat.Builder createNotification(int id, String title, String message, int icon) {
        Intent intent = new Intent(getApplicationContext(), MainActivity.class);
        PendingIntent pendingIntent = PendingIntent.getActivity(getApplicationContext(), id, intent, PendingIntent.FLAG_CANCEL_CURRENT);

        String skill = "Задание";

        if (!title.equals("")) {
            skill = title;
        }

        return new NotificationCompat.Builder(getApplicationContext(), String.valueOf(id))
                .setDefaults(Notification.DEFAULT_ALL)
                .setContentTitle(skill)
                .setContentText(message)
                .setWhen(System.currentTimeMillis())
                .setTicker("Хей, у тебя есть не выполненное задание!")
                .setPriority(NotificationCompat.PRIORITY_HIGH)
                .setAutoCancel(true)
                .setChannelId(CHANNEL_ID)
                .setGroup(GROUP_KEY)
                .setContentIntent(pendingIntent)
                .setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher))
                .setSmallIcon(IconHelper.getIcon(icon));
    }


Мой манифест:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    package="kolmachikhin.alexander.todolist">

    <uses-permission android:name="android.permission.ACTION_LOCKED_BOOT_COMPLETED"/>
    <uses-permission android:name="android.permission.SET_ALARM"/>
    <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/Theme.AppCompat.Light.DarkActionBar"
        tools:ignore="GoogleAppIndexingWarning">
        <activity.../>
        <activity.../>
        <activity.../>
        <activity.../>

        <receiver
            android:name=".Notification.AlertReceiver"
            android:directBootAware="true"
            android:enabled="true"
            android:exported="false"
            android:permission="android.permission.RECEIVE_BOOT_COMPLETED">
            <intent-filter>
                <action android:name="android.intent.action.LOCKED_BOOT_COMPLETED"/>
                <action android:name="android.intent.action.BOOT_COMPLETED"/>
                <action android:name="android.intent.action.QUICKBOOT_POWERON" />
                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </receiver>

    </application>

</manifest>


Ну и на всякий случай скину константы, вдруг там ошибка...
private static final String CHANNEL_NAME = "To do list - name";
    private static final String CHANNEL_ID = "To do list - id";
    private static final String CHANNEL_DESCRIPTION = "To do list";
    private static final String GROUP_KEY = "To do list - group key";
  • Вопрос задан
  • 67 просмотров
Пригласить эксперта
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы