{"id":9891,"date":"2023-01-15T13:15:40","date_gmt":"2023-01-15T13:15:40","guid":{"rendered":"https:\/\/putridparrot.com\/blog\/?p=9891"},"modified":"2023-01-15T15:14:52","modified_gmt":"2023-01-15T15:14:52","slug":"android-notifications-using-maui-part-9-of-10","status":"publish","type":"post","link":"https:\/\/putridparrot.com\/blog\/android-notifications-using-maui-part-9-of-10\/","title":{"rendered":"Android notifications using MAUI (Part 9 of 10)"},"content":{"rendered":"<p>In this post we&#8217;re going to cover the tutorial <a href=\"https:\/\/www.youtube.com\/watch?v=kfaZTF95Pt4\" rel=\"noopener\" target=\"_blank\">Notifications Tutorial Part 9 &#8211; NOTIFICATION CHANNEL SETTINGS &#8211; Android Studio Tutorial<\/a> and we&#8217;re going to be notification channel settings.<\/p>\n<p><strong>Overwrite<\/strong><\/p>\n<p>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&#8217;s assume the user blocked a channel and now wants our application to notify them when something occurs within the application.<\/p>\n<p>Ofcourse we could popup an alert saying &#8220;Go to the channel settings and unblock it&#8221; or better still we can alert them then display the settings.<\/p>\n<p><strong>Implementation<\/strong><\/p>\n<p>We&#8217;re going to change our MainActivity.SendOnChannel1 method to check if notifications are enabled and whether they&#8217;re blocked. So let&#8217;s first look at how we check if notifications are enabled.<\/p>\n<p>We need access to the NotificationManagerCompat and we use it like this<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\nif (!notificationManager.AreNotificationsEnabled())\r\n{\r\n  OpenNotificationSettings(context);\r\n  return;\r\n}\r\n<\/pre>\n<p>In this case we&#8217;re not even bothering to try to send notifications if they&#8217;re disabled, but we use our new method OpenNotificationSettings to show the settings screen (in a real world app we&#8217;d probably display and alert to asking them if they wish to change the settings etc.<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\n&#x5B;RequiresApi(Api = 26)]\r\nprivate static void OpenNotificationSettings(Context context)\r\n{\r\n  \/\/ api 26\r\n  if (Build.VERSION.SdkInt &gt;= BuildVersionCodes.O)\r\n  {\r\n    var intent = new Intent(Settings.ActionAppNotificationSettings);\r\n    intent.PutExtra(Settings.ExtraAppPackage, context.PackageName);\r\n    context.StartActivity(intent);\r\n  }\r\n  else\r\n  {\r\n    var intent = new Intent(Settings.ActionApplicationDetailsSettings);\r\n    intent.SetData(Uri.Parse($&quot;package:{context.PackageName}&quot;));\r\n    context.StartActivity(intent);\r\n  }\r\n}\r\n<\/pre>\n<p>Next in SendOnChannel1 we&#8217;re execute this code to check if the specific channel is blocked and again alert\/open settings for the user if it is<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\nif (Build.VERSION.SdkInt &gt;= BuildVersionCodes.O &amp;&amp; IsChannelBlocked(context, MainApplication.Channel1Id))\r\n{\r\n  OpenChannelSettings(context, MainApplication.Channel1Id);\r\n  return;\r\n}\r\n<\/pre>\n<p>IsChannelBlocked looks like this<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\n&#x5B;RequiresApi(Api = 26)]\r\nprivate static bool IsChannelBlocked(Context context, string channelId)\r\n{\r\n  if (context.GetSystemService(NotificationService) is NotificationManager manager)\r\n  {\r\n    var channel = manager.GetNotificationChannel(channelId);\r\n    return channel is { Importance: NotificationImportance.None };\r\n  }\r\n\r\n  return false;\r\n}\r\n<\/pre>\n<p>Note that both these methods require API 21 or above.<\/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%209\" 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 9 &#8211; NOTIFICATION CHANNEL SETTINGS &#8211; Android Studio Tutorial and we&#8217;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 [&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-9891","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\/9891","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=9891"}],"version-history":[{"count":2,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/9891\/revisions"}],"predecessor-version":[{"id":9901,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/9891\/revisions\/9901"}],"wp:attachment":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/media?parent=9891"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/categories?post=9891"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/tags?post=9891"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}