K
K
Ksernik2018-11-28 11:49:23
Android
Ksernik, 2018-11-28 11:49:23

android notification channels?

Hello everyone =)
Does anyone have more than one notification channel in one application?
The essence of the problem: two channels .. one with the highest priority and always hangs because the service is foreground .. the second channel for displaying notifications .. and at some point notifications from the second channel are displayed in the first channel (permanent notification)
I have one way to create notifications for of the second channel .. and the ID of the second channel is indicated there .. that is, there are no options at all to “mix up” .. there can be no accidental substitution either ..
I would gladly debug if it weren’t for two but .. the first is a floating behavior, the second is reproduced only on release build
When channels were created:

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {

            NotificationChannel mainChannel = new NotificationChannel(MAIN_CHANNEL_ID, MAIN_CHANNEL_NAME,
                    NotificationManager.IMPORTANCE_HIGH);
            Uri soundUriMain = Uri.parse(ContentResolver.SCHEME_ANDROID_RESOURCE + "://" + this.getPackageName() + "/" + R.raw.online);
            AudioAttributes audioAttributesMain = new AudioAttributes.Builder()
                    .setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
                    .setUsage(AudioAttributes.USAGE_NOTIFICATION)
                    .build();

            mainChannel.setSound(soundUriMain, audioAttributesMain);

            ((NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE)).createNotificationChannel(mainChannel);

            NotificationChannel messageChannel = new NotificationChannel(MESSAGE_CHANNEL_ID, MESSAGE_CHANNEL_NAME,
                    NotificationManager.IMPORTANCE_DEFAULT);
            Uri soundUriMessage = Uri.parse(ContentResolver.SCHEME_ANDROID_RESOURCE + "://" + this.getPackageName() + "/" + R.raw.message);
            messageChannel.setSound(soundUriMessage, audioAttributesMain);
            ((NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE)).createNotificationChannel(messageChannel);

            Notification notification = new NotificationCompat.Builder(this, MAIN_CHANNEL_ID)
                    .setContentTitle("")
                    .setContentText("")
                    .setAutoCancel(true)
                    .build();

            startForeground(1, notification);
        }

Time of notification creation:
NotificationCompat.Builder mBuilder =
                new NotificationCompat.Builder(context.getApplicationContext(), MESSAGE_CHANNEL_ID);
 mBuilder.setSmallIcon(R.drawable.ic_launcher);
        mBuilder.setContentTitle(contentTitle);
        if (!TextUtils.isEmpty(message)) {
            mBuilder.setContentText(message);
        } else {
            mBuilder.setContentText(context.getString(R.string.incoming_files_from, contentTitle));
        }
 mBuilder.setPriority(checkNotificationPriorityMessage(context, user));
        mBuilder.setAutoCancel(true);
 int notificationID = (int) user.getId();
        mNotificationManager.notify(notificationID, mBuilder.build());

Answer the question

In order to leave comments, you need to log in

2 answer(s)
O
Oleg, 2018-11-28
@402d

Do you really need 2 channels?
just
mNotificationManager.notify(0/1, mBuilder.build());
?
0 - your constant that the service is running
1 - the last notification
can be rewritten a little cleaner to make it clear what and why
the YouApp method has a successor from the application class

public static void showNotify(@NonNull String message, int IdChanel) {
        NotificationCompat.Builder builder =
                new NotificationCompat.Builder(YouApp.context, YouApp.context.getString(R.string.app_name))
                        .setSmallIcon(R.drawable.ic_stat_print)
                        .setContentTitle(YouApp.context.getString(R.string.app_name))
                        .setContentText(message);

        Notification notification = builder.build();

        NotificationManager notificationManager =
                (NotificationManager) YouApp.context.getSystemService(NOTIFICATION_SERVICE);

        if (notificationManager != null) {
                notificationManager.notify(IdChanel, notification);
        }
    }

K
Ksernik, 2018-12-05
@Ksernik

it's unpleasant to feel stupid.. and also not to know why your "solution" worked..
at the moment "startForeground(1, notification);" instead of unity, I shoved some kind of large int value ..
no repetitions of the situation have been noticed yet

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question