Android notifications using MAUI (Part 8 of 10)

In this post we’re going to cover the tutorial Notifications Tutorial Part 8 – NOTIFICATION CHANNEL GROUPS – Android Studio Tutorial and we’re going to be notification channel groups

Overwrite

In the previous post we looked at notifications being grouped into summaries. We also have the concept of grouping notification channels themselves. These are ways of, for example, grouping your channels themselves by some business or logical grouping – maybe you group channels by importance or by business process or multiple user accounts etc.

Basically this allows us to fine grain our channels allowing the user to change settings to those groups.

Implementation

In MainApplication.cs just update our channel id’s etc. to look like this

public const string Group1Id = "group1";
public const string Group2Id = "group2";
public const string Channel1Id = "channel1";
public const string Channel2Id = "channel2";
public const string Channel3Id = "channel3";
public const string Channel4Id = "channel4";

We’ve got a couple of extra channels as well as some group id’s. Now change our OnCreate method to look like this

public override void OnCreate()
{
  base.OnCreate();

  if (Build.VERSION.SdkInt >= BuildVersionCodes.O)
  {
#pragma warning disable CA1416
    var group1 = new NotificationChannelGroup(Group1Id, "Group 1");
    var group2 = new NotificationChannelGroup(Group2Id, "Group 2");

    var channel1 = new NotificationChannel(Channel1Id, "Channel 1", NotificationImportance.High);
    channel1.Description = "This is Channel 1";
    channel1.Group = Group1Id;

    var channel2 = new NotificationChannel(Channel2Id, "Channel 2", NotificationImportance.Low);
    channel2.Description = "This is Channel 2";
    channel2.Group = Group1Id;

    var channel3 = new NotificationChannel(Channel3Id, "Channel 3", NotificationImportance.High);
    channel3.Description = "This is Channel 3";
    channel3.Group = Group2Id;

    var channel4 = new NotificationChannel(Channel4Id, "Channel 4", NotificationImportance.Low);
    channel4.Description = "This is Channel 4";    
    // purposefully no group

    if (GetSystemService(NotificationService) is NotificationManager manager)
    {
      manager.CreateNotificationChannelGroup(group1);
      manager.CreateNotificationChannelGroup(group2);

      manager.CreateNotificationChannel(channel1);
      manager.CreateNotificationChannel(channel2);
      manager.CreateNotificationChannel(channel3);
      manager.CreateNotificationChannel(channel4);
    }
#pragma warning restore CA1416
  }
}

As you can see, we’ve create two group channels and then assigned these, where required, to our notification channels (i.e. .Group = Group1Id etc.). That’s it.

Now if we run this application, go to the Android Settings | App & notifications click on our application name then select Notifications we’re see our channels grouped together in Group 1, Group 2 and channel 4 is not grouped, so become “Other”.

Code

Code for this an subsequent posts is found on my blog project.