{"id":9858,"date":"2023-01-15T12:07:51","date_gmt":"2023-01-15T12:07:51","guid":{"rendered":"http:\/\/putridparrot.com\/blog\/?p=9858"},"modified":"2023-01-15T15:16:27","modified_gmt":"2023-01-15T15:16:27","slug":"android-notifications-using-maui-part-3-of-10","status":"publish","type":"post","link":"https:\/\/putridparrot.com\/blog\/android-notifications-using-maui-part-3-of-10\/","title":{"rendered":"Android notifications using MAUI (Part 3 of 10)"},"content":{"rendered":"<p>Extending on what we did in &#8220;Android notifications using MAUI (Part 2 of 10)&#8221; we&#8217;re going to look at adding text style and inbox style as per <a href=\"https:\/\/www.youtube.com\/watch?v=lVzhzi2e_Zw\" rel=\"noopener\" target=\"_blank\">Notifications Tutorial Part 3 &#8211; BIG TEXT STYLE + INBOX STYLE &#8211; Android Studio Tutorial<\/a>.<\/p>\n<p><strong>Overview<\/strong><\/p>\n<p>We&#8217;re going to continue using the code we implemented in the last part of this set of posts. We&#8217;re going to add a large icon to the notification as well using some built-in styling to style our notification. We&#8217;ll also use another style for our channel 2 notifications called the InboxStyle which will display messages more like a list.<\/p>\n<p>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&#8217;t seem to work as expected in general, but we&#8217;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.<\/p>\n<p><strong>Let&#8217;s get started<\/strong><\/p>\n<p>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&#8217;m using my application&#8217;s icon which I exported\/saved to a .PNG. My file is named AppIcon.png.<\/p>\n<p>Now, in the MainActivity.SendOnChannel1 method, before we create the notification add the following line<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\nvar largeIcon = BitmapFactory.DecodeResource(\r\n   Android.App.Application.Context.Resources, Resource.Drawable.AppIcon\r\n);\r\n<\/pre>\n<p>Notice how our AppIcon is accessible (or will be when you build the project) from <em>Resource.Drawable.AppIcon<\/em> 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&#8217;re using the BitmapFactory to decode the resource. <\/p>\n<p>Now, if we add the following to the notification builder we should see a large icon displayed alongside the message<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\n.SetLargeIcon(largeIcon)\r\n<\/pre>\n<p>Remember that a small icon is required for a notification. The large icon is not required. Let&#8217;s also change our small icon to use the same icon, so change <em>SetSmallIcon<\/em> to this<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\n.SetSmallIcon(Resource.Drawable.AppIcon)\r\n<\/pre>\n<p>Next we want to change our notification style. This is done through <em>SetStyle<\/em> and we can supply different built-in styles, so the code for this looks like this (added to the <em>NotificationCompat.Builder<\/em>)<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\n.SetStyle(new NotificationCompat\r\n                .BigTextStyle()\r\n                .BigText(&quot;Some Big Text&quot;)\r\n                .SetBigContentTitle(&quot;Big Content Title&quot;)\r\n                .SetSummaryText(&quot;Summary Text&quot;))\r\n<\/pre>\n<p>Lastly we&#8217;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<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\n.SetStyle(new NotificationCompat\r\n  .InboxStyle()\r\n    .AddLine(&quot;This is line 1&quot;)\r\n    .AddLine(&quot;This is line 2&quot;)\r\n    .AddLine(&quot;This is line 3&quot;)\r\n    .SetBigContentTitle(&quot;Big Content Title&quot;)\r\n    .SetSummaryText(&quot;Summary Text&quot;))\r\n<\/pre>\n<p><strong>Code<\/strong><\/p>\n<p>Code for this an subsequent posts is found on my <a href=\"https:\/\/github.com\/putridparrot\/blog-projects\/tree\/master\/AndroidNotificationInMaui\/Part%203\" rel=\"noopener\" target=\"_blank\">blog project<\/a>. <\/p>\n","protected":false},"excerpt":{"rendered":"<p>Extending on what we did in &#8220;Android notifications using MAUI (Part 2 of 10)&#8221; we&#8217;re going to look at adding text style and inbox style as per Notifications Tutorial Part 3 &#8211; BIG TEXT STYLE + INBOX STYLE &#8211; Android Studio Tutorial. Overview We&#8217;re going to continue using the code we implemented in the last [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[120,330],"tags":[],"class_list":["post-9858","post","type-post","status-publish","format-standard","hentry","category-android","category-maui"],"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/9858","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/comments?post=9858"}],"version-history":[{"count":5,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/9858\/revisions"}],"predecessor-version":[{"id":9907,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/9858\/revisions\/9907"}],"wp:attachment":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/media?parent=9858"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/categories?post=9858"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/tags?post=9858"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}