Transforming inputs in SpecFlow

One really useful option in SpecFlow, which maybe most people will never need, is the ability to intercept input and potentially transform it. So the example I have is, we want to include tokens wrapped in { }. These braces will include a token which can then be replaced by a “real” value. Let’s assume {configuration.XXX} means get the value XXX from the configuration file (App.config).

With this use case in mind, let’s assume we have a feature that looks like this

Feature: Transformer Tests

  Scenario: Transform a number
    Given The value {configuration.someValue1}

  Scenario: Transform a table
    Given The values 
    | One                        | Two                        |
    | {configuration.someValue1} | {configuration.someValue2} |

We’re going to hard code our transformer to simple change configuration.someValue1 to Value1 and configuration.someValue2 to Value2, but ofcourse the real data could be read from the App.Config or generated in some more useful way.

Our step file for the above feature file, might look like this

[Given(@"The value (.*)")]
public void GivenTheValue(string s)
{
  s.Should().Be("Value1");
}

[Given(@"The values")]
public void GivenTheValues(Table table)
{
  foreach (var row in table.Rows)
  {
    var i = 1;
    foreach (var rowValue in row.Values)
    {
      rowValue.Should().Be($"Value{i}");
      i++;
    }
  }
}

We create a class, usually stored in the Hooks folder which looks like the following, where we include methods with the StepArgumentTransformation attribute, meaning these methods are call for the given type to do something with the step argument.

[Binding]
public class StepTransformer
{
  private string SimpleTransformer(string s)
  {
    switch (s)
    {
      case "{configuration.someValue1}":
        return "Value1";
      case "{configuration.someValue2}":
        return "Value2";
      default:
        return s;
    }
  }
        
  [StepArgumentTransformation]
  public string Transform(string input)
  {
    return SimpleTransformer(input);
  }
        
  [StepArgumentTransformation]
  public Table Transform(Table input)
  {
    foreach (var row in input.Rows)
    {
      foreach (var kv in row)
      {
        row[kv.Key] = SimpleTransformer(kv.Value);
      }
    }
    return input;
  }
}

This is a very simple example of such code, what we’d do in a real world is create some more advanced transformer code and inject that via the constructor but the basic Transform method would look much like they are here.

Running code when a feature or scenario starts in SpecFlow

One of the useful capabilities within SpecFlow is for us to define hooks which starts when the feature or scenario starts and ends.

An obvious use of these is to log information about the feature or scenario – we’ll just use Debug.WriteLine to demonstrate this.

All we need to do is create a binding and then supply static methods marked with the BeforeFeature and AfterFeature attributes, which might look like this

[Binding]
public class FeatureHook
{
  [BeforeFeature]
  public static void BeforeFeature(FeatureContext featureContext)
  {
    Debug.WriteLine($"Feature starting: {featureContext.FeatureInfo.Title}");
  }

  [AfterFeature]
  public static void AfterFeature(FeatureContext featureContext)
  {
    Debug.WriteLine($"Feature ending: {featureContext.FeatureInfo.Title}");
  }
}

as you can probably guess, we can do the same with the BeforeScenario and AfterScenario attributes like this

[Binding]
public class ScenarioHook
{
  [BeforeScenario]
  public static void BeforeScenario(ScenarioContext scenarioContext)
  {
    Debug.WriteLine($"Scenario starting: {scenarioContext.ScenarioInfo.Title}");
  }

  [AfterScenario]
  public static void AfterScenario(ScenarioContext scenarioContext)
  {
    Debug.WriteLine($"Scenario ending: {scenarioContext.ScenarioInfo.Title}");
  }
}

Creating a SpecFlow code generation plugin

Over the last few months I’ve been helping develop an Appium/WinAppDriver framework (or library if you prefer) to allow one of the application to look to move one of my client’s application away from CodedUI (support is being phased out for this).

One of the problem I had is, I wanted to be able to put text into the Windows Clipboard and paste it into an various edit controls. The pasting made things slightly quicker but also, more importantly, bypassed some control’s autocomplete which occasionally messes up using SendKeys.

The problem is that the clipboard uses OLE and NUnit complains that it needs to be running in an STA.

To fix this, SpecFlow classes are partial so we can create new classes within another file with the same classname (and mark as partial) and namespace and then add the NUnit require attribute as below

[NUnit.Framework.Apartment(System.Threading.ApartmentState.STA)]
public partial class CalculatorFeature
{
}

This is all well and good. It’s hardly a big deal, but we have hundreds of classes and it’s just more hassle, remembering each time to do this, or you run the tests and when it hits a Paste from the Clipboard, it fails, wasting a lot of time before you’re reminded you need to create the partial class.

SpecFlow has the ability to intercept parameters passed into our steps, it has tags and hooks which allow you to customize behaviour and it also comes with a way to interact with the code generation process which we can use to solve this issue and generate the required attributes in the generated code.

The easiest way to get started is follow the steps below, although if you prefer, the code I’m presenting here is available on my PutridParrot.SpecFlow.NUnitSta GitHub repo., so you can bypass this post and go straight the code for this article if you prefer.

  • Grab the zip or clone the repo to get the following GenerateOnlyPlugin
  • You may want to update SampleGeneratorPlugin.csproj to
    <TargetFrameworks>net472;netcoreapp3.1</TargetFrameworks>
    

    I would keep to these two frameworks to begin with, as we have a few other places to change and it’s better to get something working before we start messing with the rest of the build properties etc.

  • In the build folder edit the .targets file to reflect the Core and !Core frameworks, i.e.
    <_SampleGeneratorPluginFramework Condition=" '$(MSBuildRuntimeType)' == 'Core'">netcoreapp3.1</_SampleGeneratorPluginFramework>
    <_SampleGeneratorPluginFramework Condition=" '$(MSBuildRuntimeType)' != 'Core'">net472</_SampleGeneratorPluginFramework>
    

    Again I’ve left _SampleGeneratorPluginFramework for now as I just want to get things working.

  • Next go to the .nuspec file and change the file location at the bottom of this file, i.e.
    <file src="bin\$config$\net472\PutridParrot.SpecFlow.NUnitSta.*" target="build\net472"/>
    <file src="bin\$config$\netcoreapp3.1\PutridParrot.SpecFlow.NUnitSta.dll" target="build\netcoreapp3.1"/>
    <file src="bin\$config$\netcoreapp3.1\PutridParrot.SpecFlow.NUnitSta.pdb" target="build\netcoreapp3.1"/>
    
  • Now go and change the name of the solution, the project names and the assembly namespace if you’d like to. Ofcourse this also means changing the PutridParrot.SpecFlow.NUnitSta in the above to your assembly name.

At this point you have a SpecFlow plugin which does nothing, but builds to a NuGet package and in your application you can set up your nuget.config with a local package pointing to the build from this plugin. Something like

<packageSources>
  <add key="nuget.org" value="https://api.nuget.org/v3/index.json" protocolVersion="3" />
  <add key="Local" value="F:\Dev\PutridParrot.SpecFlow.NUnitSta\PutridParrot.SpecFlow.NUnitSta\bin\Debug"/>
</packageSources>

Obviously change the Local package repository value to the location of your package builds.

Now if you like, you can create a default SpecFlow library using the default SpecFlow templates. Ensure the SpecFlow.NUnit version is compatible with your plugin (so you may need to update the package from the template one’s). Finally just add the package you built and then build your SpecFlow test library.

If all went well, nothing will have happened, or more importantly, no errors will be displayed.

Back to our Plugin, thankfully I found some code to demonstrated adding global:: to the NUnit attributes on GitHub, so my thanks to joebuschmann.

  • Create yourself a class, mine’s NUnit3StaGeneratorProvider
  • This should implement the interface IUnitTestGeneratorProvider
  • The constructor should look like this (along with the readonly field)
    private readonly IUnitTestGeneratorProvider _unitTestGeneratorProvider;
    
    public NUnit3StaGeneratorProvider(CodeDomHelper codeDomHelper)
    {
       _unitTestGeneratorProvider = new NUnit3TestGeneratorProvider(codeDomHelper);
    }
    
  • We’re only interested in the SetTestClass, which looks like this
    public void SetTestClass(TestClassGenerationContext generationContext, string featureTitle, string featureDescription)
    {
       _unitTestGeneratorProvider.SetTestClass(generationContext, featureTitle, featureDescription);
    
       var codeFieldReference = new CodeFieldReferenceExpression(
          new CodeTypeReferenceExpression(typeof(ApartmentState)), "STA");
    
       var codeAttributeDeclaration =
          new CodeAttributeDeclaration("NUnit.Framework.Apartment", new CodeAttributeArgument(codeFieldReference));
             generationContext.TestClass.CustomAttributes.Add(codeAttributeDeclaration);
    }
    

    All other methods will just have calls to the _unitTestGeneratorProvider method that matches their method name.

  • We cannot actually use this until we register out new class with the generator plugins, so in your Plugin class, mine’s NUnitStaPlugin, change the Initialize to look like this
    public void Initialize(GeneratorPluginEvents generatorPluginEvents, 
       GeneratorPluginParameters generatorPluginParameters,
       UnitTestProviderConfiguration unitTestProviderConfiguration)
    {
       generatorPluginEvents.CustomizeDependencies += (sender, args) =>
       {
          args.ObjectContainer
             .RegisterTypeAs<NUnit3StaGeneratorProvider, IUnitTestGeneratorProvider>();
       };
    }
    
  • If you changed the name of your plugin class ensure the assembly:GeneratorPlugin reflects this new name, the solution will fail to build if you haven’t updated this anyway.

Once all these things are completed, you can build your NuGet package again. It might be worth incrementing the version and then update in your SpecFlow test class library. Rebuild that and the generated .feature.cs file should have classes with the [NUnit.Framework.Apartment(System.Threading.ApartmentState.STA)] attribute now. No more writing partial classes by hand.

This plugin is very simple, all we’re really doing is creating a really minimal implementation of IUnitTestGeneratorProvider which just adds an attribute to a TestClass and we registered this with the GeneratorPluginEvents, there’s a lot more why could potentially do.

Whilst this was very simple in the end. The main issues I had previously with this are that we need to either copy/clone or handle these build props and targets as well as use .nuspec to get our project packaged in a way that works with SpecFlow.

Getting started with MassTransit

What is MassTransit?

MassTransit is an abstraction over various messaging transports, such as RabbitMQ, Azure Service Bus, Amazon SQS and others. We can use MassTransit in place of these transports and, ofcourse, change the underlying transport without affecting our code. It also includes some microservice based patterns, as Sagas, Routing Slip etc.

MassTransit offers an in-memory transport for testing code against.

Creating a simple test application

Note: I’m basically going to reproduce the code from In Memory, so I’d suggest going there to check the latest steps.

First off let’s add the MassTransit project templates to our machine

dotnet new --install MassTransit.Templates

Now this will add a bunch of useful templates that we can use via the dotnet CLI or via an IDE such as Visual Studio. In this case we’ll follow the In Memory tutorial by running the following command to create our project

dotnet new mtworker -n HelloWorld

or from Visual Studio or Rider you can create a MassTransit project from the MassTransit Worker project template.

Note: I’m differing from the MassTransit tutorial by naming my project HelloWorld, for no other reason that I pretty much always start these sorts of things with a HelloWorld project. So just in case you’re following along both the Mass Transit tutorial and this post, this is the only real difference in code.

Default project

At the time of writing, the default project targets .NET 6.0 so I’ve changed that to .NET 7.0 and language support to C# 11 in Rider, so my .csproj has the following

<PropertyGroup>
  <TargetFramework>net7.0</TargetFramework>
  <LangVersion>11</LangVersion>
</PropertyGroup>

All the code is currenting within the Program.cs, the code that matters at the moment is

services.AddMassTransit(x =>
{
  x.SetKebabCaseEndpointNameFormatter();

  // By default, sagas are in-memory, but should be changed to a durable
  // saga repository.
  x.SetInMemorySagaRepositoryProvider();

  var entryAssembly = Assembly.GetEntryAssembly();

  x.AddConsumers(entryAssembly);
  x.AddSagaStateMachines(entryAssembly);
  x.AddSagas(entryAssembly);
  x.AddActivities(entryAssembly);

  x.UsingInMemory((context, cfg) => { cfg.ConfigureEndpoints(context); });
});

As you can see we add MassTransit and at the end of the code we set MassTransit to use the in memory transport. Whilst this will build and run, it’s not really doing anything for us as we need to add at least one contract.

Adding a contract

From the MassTransit tutorial we simply run the following command in the CLI from the .csproj folder

dotnet new mtconsumer

This will add the Consumers folder along with the Contracts folder along with the consumer and contract code. Within the Consumer folder we have two files, the HelloWorldConsumer.cs file looks like this

Note: I’m not include the using and namespace lines in these files so that we can concentrate on the implementation code.

public class HelloWorldConsumer :
  IConsumer<HelloWorld>
{
  public Task Consume(ConsumeContext<HelloWorld> context)
  {
    return Task.CompletedTask;
  }
}

The other file in the HelloWorldConsumerDefinition.cs looks like this

public class HelloWorldConsumerDefinition :
  ConsumerDefinition<HelloWorldConsumer>
{
  protected override void ConfigureConsumer(IReceiveEndpointConfigurator endpointConfigurator, IConsumerConfigurator<HelloWorldConsumer> consumerConfigurator)
  {
    endpointConfigurator.UseMessageRetry(r => r.Intervals(500, 1000));
  }
}

The Contracts folder includes a single file, HelloWorld.cs which looks like this

public record HelloWorld
{
  public string Value { get; init; }
}

At this point we now have a consumer and a message/contract.

When a message arrives on the transport, our consumer, Consume message is called. So let’s add some logging to the consumer, exactly like the MassTransit example, so add the following to the HelloWorldConsumer class

private readonly ILogger<HelloWorldConsumer> _logger;

public HelloWorldConsumer(ILogger<HelloWorldConsumer> logger)
{
  _logger = logger;
}

Now in the Consume method we’ll just add a line of code to log information from the message so the method looks like this

public Task Consume(ConsumeContext<HelloWorld> context)
{
  _logger.LogInformation("Hello {Name}", context.Message.Value);
  return Task.CompletedTask;
}

The HelloWorldConsumerDefinition class can be used for setting up out configuration, we’re not going to do anything with this at the moment. Instead we need some code to publish methods. We are going to add a background service.

Background service

Add a new file/class to the root folder, named Worker. Here’s the one in the MassTransit tutorial

public class Worker : BackgroundService
{
    private readonly IBus _bus;

    public Worker(IBus bus)
    {
        _bus = bus;
    }
    protected override async Task ExecuteAsync(CancellationToken stoppingToken)
    {
        while (!stoppingToken.IsCancellationRequested)
        {
            await _bus.Publish(new Contracts.HelloWorld(value: $"The time is {DateTimeOffset.Now}"), stoppingToken);
            await Task.Delay(1000, stoppingToken);
        }
    }
}

Next, we need to register our work, by adding the following code to the Program.cs

services.AddHostedService<Worker>();

Does it run?

If all’s gone to plan, we can either

dotnet run

or run via Visual Studio or Rider. This will run our service and we’ll see output to the console from our logging code.

What next?

This is fun, but let’s face it we really want to see this working with a “real” transport, so let’s make changes to work with RabbitMQ. To host an instance of RabbitMQ within Docker we can use the MassTransit image

docker run -p 15672:15672 -p 5672:5672 masstransit/rabbitmq

To check whether RabbitMQ is up and running, use your browser to connect to the machine hosting it, on port 15672 (i.e. http://192.168.1.123:15672/). To login, the default username and password are both guest.

Now we need to add the MassTransit RabbitMQ transport to our project, so add MassTransit.RabbitMQ via the nuget package manager or from the CLI use

dotnet add package MassTransit.RabbitMQ

Finally, replace everything x.UsingInMemory((context, cfg) => { cfg.ConfigureEndpoints(context); }); within Program.cs with

x.UsingRabbitMq((context,cfg) =>
{
  cfg.Host("192.168.1.123", "/", h => {
    h.Username("guest");
    h.Password("guest");
 });
 cfg.ConfigureEndpoints(context);
});

Note: If you’ve changed the user or password, obviously replace those in the code above. Also replace 192.168.1.123 with localhost or your remote server name/ip.

Run your MassTransit service.

Next, access your RabbitMQ dashboard and you should see messages coming into RabbitMQ (note: if you’ve changed the port, then the port number goes into the Host code like this, we’re using port 1234 to demonstrate a different port number)

x.UsingRabbitMq((context,cfg) =>
{
  cfg.Host("192.168.1.123", 1234, "/", h => {
    h.Username("guest");
    h.Password("guest");
 });
 cfg.ConfigureEndpoints(context);
});

Note: remember to allow access to the remote server’s port 5672, if it’s not already accessible.

Is the checkbox checked on my AppiumWebElement ?

From Appium/WinAppDriver you’ll probably want to check if a checkbox or radio button element is checked at some point. For such elements we use the AppiumWebElement Selected property to get the current state. To set it within UI automation we would click on it, so for example we might have code like this

public bool IsChecked
{
   get => Selected;
   set
   {
      if(IsChecked != value)
      {
         Click();
      }
   }
}

Note: In my case I wrap the AppiumWebElement in a CheckBoxElement, but this could be in a subclass or extension method, you get the idea.

Plugin.Maui.Audio plays sound even if Android device is set to vibrate

I have a MAUI application which alerts the user with a sound and vibration when a task is completed. I noticed that turning the phone to vibrate didn’t actually stop the sound being played via the Plugin.Maui.Audio (as I sort of expected it to do).

It looks like what we need to do is write some Android specific code to check the Android AudioManager’s RingerMode.

If we assume we have a device independent enum such as the following (this simply mirrors the Android RingerMode)

public enum AudioMode
{
    Silent = 0,
    Vibrate = 1,
    Normal = 2
}

Now, from our Android code we would expose the RingerMode like this

if (Context.GetSystemService(AudioService) is not AudioManager audioService)
  return AudioMode.Normal;

switch (audioService.RingerMode)
{
  case RingerMode.Normal:
    return AudioMode.Normal;
  case RingerMode.Silent:
    return AudioMode.Silent;
  case RingerMode.Vibrate:
    return AudioMode.Vibrate;
}

return AudioMode.Normal;

Now we would simply wrap our Plugin.Maui.Audio code in an if statement, checking the AudioMode of the device. i.e. if Vibrate or Silent, don’t play the audio. For example

if (_deviceAudioService.AudioMode == AudioMode.Normal)
{
  var audioPlayer =
    _audioManager.CreatePlayer(await FileSystem.OpenAppPackageFileAsync("chimes.wav"));
  audioPlayer.Volume = Math.Clamp(_configuration.NotificationSoundVolume / 100.0, 0, 1);
  audioPlayer.Play();
}

Changing the origin of your local git repo.

Git, being a distributed source control system, allows us to switch the remote/origin for the push or fetch of our local repository. Or to put it another way…

We’ve just moved from one remote git repository to another, how do we update our local code base to “retarget” to the new location?

When you’re using GitHub, GitLab, bitbucket etc. you might come to a point where you migrate from one server to another. Ofcourse you can simply clone your repo. again from the new server OR you can just target you fetch/push origin to the new location like this…

To check your current remote/origin, just run

git remote -v

Now if you wish to change the remote/origin, simply use

git remote set-url remote_name remote_url

Where remote_name might be origin and the remote_url is the new .git URL of your server.

Android notifications using MAUI (Part 10 of 10)

In this post we’re going to cover the tutorial Notifications Tutorial Part 10 – DELETE NOTIFICATION CHANNELS – Android Studio Tutorial and we’re going to be look at deleting notification channels.

Overwrite

We can delete notification channels if they’re no longer required, so let’s create some code by changing the UI slightly and updating with a new message – ofcourse in a real-world scenario, it’s more likely you’re either deleting a channel that’s been dynamically created or deleting a legacy channel.

Implementation

Go to to MainPage.xaml and add

<Button Text="Delete Channels" Clicked="DeleteChannel_OnClick" Margin="10" />

In the code behind MainPage.xaml.cs add the following

private void DeleteChannel_OnClick(object sender, EventArgs e)
{
  _messenger.Send(new DeleteChannelData());
}

As you can see we’ve added a new class DeleteChannelData to be sent to anything that wishes to listen to tell them to delete a channel. The code for this is simply

public record DeleteChannelData;

(It’s just really the equivalent of a command).

In the Android MainActivity constructor, add

messenger.Register<DeleteChannelData>(this, (recipient, message) =>
{
  if (Build.VERSION.SdkInt >= BuildVersionCodes.O)
  {
    if (GetSystemService(NotificationService) is NotificationManager manager)
    {
      // hard coded to delete channel 3
      //manager.DeleteNotificationChannel(MainApplication.Channel3Id);
      manager.DeleteNotificationChannelGroup(MainApplication.Group1Id);
    }
  }
});

So this receives the DeletChannelData message and deletes either channel 3 (commented out) or a given group via the NotificationManager. Notice this is NOT the NotificationManagerCompat.

Now if you run this (after running the version prior to this) and go to the settings page for your application’s notifications (Settings | Apps & notifications) you’ll noticed it says something like 2 categories deleted if you’re deleting Group1Id group. This is telling the user that something deleted the channels.

Code

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

Android notifications using MAUI (Part 9 of 10)

In this post we’re going to cover the tutorial Notifications Tutorial Part 9 – NOTIFICATION CHANNEL SETTINGS – Android Studio Tutorial and we’re going to be notification channel settings.

Overwrite

In a previous post we looked at the fact that we cannot change the notification settings once created, we need to uninstall and reinstall. This is ofcourse not a lot of help if, whilst our application is running it determines that the user should be given the opportunity to change a setting. For example let’s assume the user blocked a channel and now wants our application to notify them when something occurs within the application.

Ofcourse we could popup an alert saying “Go to the channel settings and unblock it” or better still we can alert them then display the settings.

Implementation

We’re going to change our MainActivity.SendOnChannel1 method to check if notifications are enabled and whether they’re blocked. So let’s first look at how we check if notifications are enabled.

We need access to the NotificationManagerCompat and we use it like this

if (!notificationManager.AreNotificationsEnabled())
{
  OpenNotificationSettings(context);
  return;
}

In this case we’re not even bothering to try to send notifications if they’re disabled, but we use our new method OpenNotificationSettings to show the settings screen (in a real world app we’d probably display and alert to asking them if they wish to change the settings etc.

[RequiresApi(Api = 26)]
private static void OpenNotificationSettings(Context context)
{
  // api 26
  if (Build.VERSION.SdkInt >= BuildVersionCodes.O)
  {
    var intent = new Intent(Settings.ActionAppNotificationSettings);
    intent.PutExtra(Settings.ExtraAppPackage, context.PackageName);
    context.StartActivity(intent);
  }
  else
  {
    var intent = new Intent(Settings.ActionApplicationDetailsSettings);
    intent.SetData(Uri.Parse($"package:{context.PackageName}"));
    context.StartActivity(intent);
  }
}

Next in SendOnChannel1 we’re execute this code to check if the specific channel is blocked and again alert/open settings for the user if it is

if (Build.VERSION.SdkInt >= BuildVersionCodes.O && IsChannelBlocked(context, MainApplication.Channel1Id))
{
  OpenChannelSettings(context, MainApplication.Channel1Id);
  return;
}

IsChannelBlocked looks like this

[RequiresApi(Api = 26)]
private static bool IsChannelBlocked(Context context, string channelId)
{
  if (context.GetSystemService(NotificationService) is NotificationManager manager)
  {
    var channel = manager.GetNotificationChannel(channelId);
    return channel is { Importance: NotificationImportance.None };
  }

  return false;
}

Note that both these methods require API 21 or above.

Code

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

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.