Android notifications using MAUI (Part 3 of 10)

Extending on what we did in “Android notifications using MAUI (Part 2 of 10)” we’re going to look at adding text style and inbox style as per Notifications Tutorial Part 3 – BIG TEXT STYLE + INBOX STYLE – Android Studio Tutorial.

Overview

We’re going to continue using the code we implemented in the last part of this set of posts. We’re going to add a large icon to the notification as well using some built-in styling to style our notification. We’ll also use another style for our channel 2 notifications called the InboxStyle which will display messages more like a list.

Before we get into this the code for setting the large icon does not appear to work correctly and as mentioned in part 1 of this series of posts, some styles don’t seem to work as expected in general, but we’ll go through the process of writing the code and maybe I can return to in the future if I find a way to get things to work.

Let’s get started

To add an icon to the Android resources, go to Platforms/Android/Resources and add a folder named drawables. Within this add a .PNG. I’m using my application’s icon which I exported/saved to a .PNG. My file is named AppIcon.png.

Now, in the MainActivity.SendOnChannel1 method, before we create the notification add the following line

var largeIcon = BitmapFactory.DecodeResource(
   Android.App.Application.Context.Resources, Resource.Drawable.AppIcon
);

Notice how our AppIcon is accessible (or will be when you build the project) from Resource.Drawable.AppIcon in other words the filename excluding the extension becomes our resource id. To use this icon we need to pass a bitmap to the SetLargeIcon method, hence we’re using the BitmapFactory to decode the resource.

Now, if we add the following to the notification builder we should see a large icon displayed alongside the message

.SetLargeIcon(largeIcon)

Remember that a small icon is required for a notification. The large icon is not required. Let’s also change our small icon to use the same icon, so change SetSmallIcon to this

.SetSmallIcon(Resource.Drawable.AppIcon)

Next we want to change our notification style. This is done through SetStyle and we can supply different built-in styles, so the code for this looks like this (added to the NotificationCompat.Builder)

.SetStyle(new NotificationCompat
                .BigTextStyle()
                .BigText("Some Big Text")
                .SetBigContentTitle("Big Content Title")
                .SetSummaryText("Summary Text"))

Lastly we’ll display the messages in channel 2 using the InboxStyle, which will allows us to add up to seven lines, like a list. To add this go to SendOnChannel2 and add the following code

.SetStyle(new NotificationCompat
  .InboxStyle()
    .AddLine("This is line 1")
    .AddLine("This is line 2")
    .AddLine("This is line 3")
    .SetBigContentTitle("Big Content Title")
    .SetSummaryText("Summary Text"))

Code

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