createNotificationChannel() asks the user for permissions

I have an Android application for which I need to ask for Notifications permission. For this I am creating a channel and, after creating the channel I am asking for permissions. The code that asks for the permissions looks like this:

public void onClick(View view) {
        if (view.getId() == R.id.allow_button) {
            if (android.os.Build.VERSION.SDK_INT > Build.VERSION_CODES.TIRAMISU) {
                 // From API 34, we can't ask for notifications permissions, with zero channels established.
                 // Since most of our channels requires to be set up later (requires account name), here we create
                 // the channels for Other group, so we are able to ask for permissions.
                NotificationChannelHelper.createChannelsForOtherGroup();
                requestPermission(NOTIFICATION_PERMISSION_REQUEST_CODE, Manifest.permission.POST_NOTIFICATIONS);
            }

        }
    }

The method that adds the channels is the following:

   public static void createChannelsForOtherGroup() {
        List<NotificationChannel> channels = new ArrayList<>();

        NotificationChannelGroup otherGroup = new NotificationChannelGroup(OTHER_GROUP_ID, resources.getString(R.string.other_group_name));
        if (!existingChannelGroupNames.contains(otherGroup.getId())) {
            notificationManager.createNotificationChannelGroup(otherGroup);
        }

        NotificationChannel miscellaneousChannel = new NotificationChannel(
                NotificationChannelType.MISCELLANEOUS.name(),
                resources.getString(R.string.miscellaneous_channel_name),
                NotificationManager.IMPORTANCE_MIN);
        miscellaneousChannel.setGroup(OTHER_GROUP_ID);
        miscellaneousChannel.setShowBadge(false);

        if (!existingChannelNames.contains(miscellaneousChannel.getId())) {
            channels.add(miscellaneousChannel);
        }

        // adding other channels in a similar way

        if ( !channels.isEmpty() ) {
            notificationManager.createNotificationChannels(channels);
        }
    }

The problem is that the system prompts the user with the notification permission after calling notificationManager.createNotificationChannels(channels);.

Is there a way to stop it from displaying the prompt? Or get the selection from the user?

Upvotes: 0

Views: 32

Answers (0)

Related Questions