/// <summary>
        /// Create Notification Channel Group when API >= 26.
        /// If you'd like to further organize the appearance of your channels in the settings UI, you can create channel groups.
        /// This is a good idea when your app supports multiple user accounts (such as for work profiles),
        /// so you can create a notification channel group for each account.
        /// This way, users can easily identify and control multiple notification channels that have identical names.
        /// </summary>
        /// <param name="request"></param>
        public static void CreateNotificationChannelGroup(NotificationChannelGroupRequest request)
        {
            if (Build.VERSION.SdkInt < BuildVersionCodes.O)
            {
                return;
            }

            if (!(Application.Context.GetSystemService(Context.NotificationService) is NotificationManager
                  notificationManager))
            {
                return;
            }

            if (request is null ||
                string.IsNullOrWhiteSpace(request.Group) ||
                string.IsNullOrWhiteSpace(request.Name))
            {
                return;
            }

            using (var channelGroup = new NotificationChannelGroup(request.Group, request.Name))
            {
                notificationManager.CreateNotificationChannelGroup(channelGroup);
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// Create Notification Channel Group when API >= 26.
        /// If you'd like to further organize the appearance of your channels in the settings UI, you can create channel groups.
        /// This is a good idea when your app supports multiple user accounts (such as for work profiles),
        /// so you can create a notification channel group for each account.
        /// This way, users can easily identify and control multiple notification channels that have identical names.
        /// </summary>
        /// <param name="request"></param>
        public static bool CreateNotificationChannelGroup(NotificationChannelGroupRequest request = null)
        {
            if (Build.VERSION.SdkInt < BuildVersionCodes.O)
            {
                return(false);
            }

            if (!(Application.Context.GetSystemService(Context.NotificationService) is NotificationManager
                  notificationManager))
            {
                return(false);
            }

            request ??= new NotificationChannelGroupRequest();

            if (string.IsNullOrWhiteSpace(request.Group))
            {
                request.Group = AndroidOptions.DefaultGroupId;
            }

            if (string.IsNullOrWhiteSpace(request.Name))
            {
                request.Name = AndroidOptions.DefaultGroupName;
            }

            using var channelGroup = new NotificationChannelGroup(request.Group, request.Name);
            notificationManager.CreateNotificationChannelGroup(channelGroup);
            return(true);
        }