{"id":9893,"date":"2023-01-15T15:14:03","date_gmt":"2023-01-15T15:14:03","guid":{"rendered":"https:\/\/putridparrot.com\/blog\/?p=9893"},"modified":"2023-01-15T15:14:38","modified_gmt":"2023-01-15T15:14:38","slug":"android-notifications-using-maui-part-10-of-10","status":"publish","type":"post","link":"https:\/\/putridparrot.com\/blog\/android-notifications-using-maui-part-10-of-10\/","title":{"rendered":"Android notifications using MAUI (Part 10 of 10)"},"content":{"rendered":"<p>In this post we&#8217;re going to cover the tutorial <a href=\"https:\/\/www.youtube.com\/watch?v=3Je9ZEAAjfA\" rel=\"noopener\" target=\"_blank\">Notifications Tutorial Part 10 &#8211; DELETE NOTIFICATION CHANNELS &#8211; Android Studio Tutorial<\/a> and we&#8217;re going to be look at deleting notification channels.<\/p>\n<p><strong>Overwrite<\/strong><\/p>\n<p>We can delete notification channels if they&#8217;re no longer required, so let&#8217;s create some code by changing the UI slightly and updating with a new message &#8211; ofcourse in a real-world scenario, it&#8217;s more likely you&#8217;re either deleting a channel that&#8217;s been dynamically created or deleting a legacy channel.<\/p>\n<p><string>Implementation<\/strong><\/p>\n<p>Go to to MainPage.xaml and add<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\n&lt;Button Text=&quot;Delete Channels&quot; Clicked=&quot;DeleteChannel_OnClick&quot; Margin=&quot;10&quot; \/&gt;\r\n<\/pre>\n<p>In the code behind MainPage.xaml.cs add the following<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\nprivate void DeleteChannel_OnClick(object sender, EventArgs e)\r\n{\r\n  _messenger.Send(new DeleteChannelData());\r\n}\r\n<\/pre>\n<p>As you can see we&#8217;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<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\npublic record DeleteChannelData;\r\n<\/pre>\n<p>(It&#8217;s just really the equivalent of a command).<\/p>\n<p>In the Android MainActivity constructor, add<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\nmessenger.Register&lt;DeleteChannelData&gt;(this, (recipient, message) =&gt;\r\n{\r\n  if (Build.VERSION.SdkInt &gt;= BuildVersionCodes.O)\r\n  {\r\n    if (GetSystemService(NotificationService) is NotificationManager manager)\r\n    {\r\n      \/\/ hard coded to delete channel 3\r\n      \/\/manager.DeleteNotificationChannel(MainApplication.Channel3Id);\r\n      manager.DeleteNotificationChannelGroup(MainApplication.Group1Id);\r\n    }\r\n  }\r\n});\r\n<\/pre>\n<p>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.<\/p>\n<p>Now if you run this (after running the version prior to this) and go to the settings page for your application&#8217;s notifications (Settings | Apps &#038; notifications) you&#8217;ll noticed it says something like <em>2 categories deleted<\/em> if you&#8217;re deleting Group1Id group. This is telling the user that something deleted the channels. <\/p>\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%2010\" rel=\"noopener\" target=\"_blank\">blog project<\/a>. <\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this post we&#8217;re going to cover the tutorial Notifications Tutorial Part 10 &#8211; DELETE NOTIFICATION CHANNELS &#8211; Android Studio Tutorial and we&#8217;re going to be look at deleting notification channels. Overwrite We can delete notification channels if they&#8217;re no longer required, so let&#8217;s create some code by changing the UI slightly and updating with [&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-9893","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\/9893","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=9893"}],"version-history":[{"count":3,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/9893\/revisions"}],"predecessor-version":[{"id":9900,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/9893\/revisions\/9900"}],"wp:attachment":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/media?parent=9893"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/categories?post=9893"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/tags?post=9893"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}