{"id":104058,"date":"2020-08-10T07:00:00","date_gmt":"2020-08-10T14:00:00","guid":{"rendered":"https:\/\/devblogs.microsoft.com\/oldnewthing\/?p=104058"},"modified":"2020-08-10T07:58:58","modified_gmt":"2020-08-10T14:58:58","slug":"20200810-00","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/oldnewthing\/20200810-00\/?p=104058","title":{"rendered":"How can I tell whether the user has disabled toast notifications for my app?"},"content":{"rendered":"<p>A customer wanted to know how to get the complete list of programs from the <i>Settings<\/i>, <i>System<\/i>, <i>Notifications &amp; Actions<\/i>, <i>Get notifications from these senders<\/i> list, as well as whether the user has enabled notifications for each app.<\/p>\n<p>Upon closer questioning, it turns out that this isn&#8217;t really what they wanted. They didn&#8217;t need the complete list of apps and the user&#8217;s preference for each one. When asked what they were going to do with the information, they said that they were going to search through the list looking for their app, to see whether notifications are enabled. If notifications are disabled for their app, then they are going to warn the user, &#8220;You need to enable notifications for Contoso Sports to receive game alerts.&#8221;<\/p>\n<p>In a sense, this was a case of the &#8220;for\/if&#8221; antipattern: Instead of asking for the thing that they actually want, they ask for <i>everything<\/i> and then try to filter to the thing that they want.<\/p>\n<p>The way to find out whether notifications are enabled for your app is to check the <code>ToastNotifier.<\/code><code>Setting<\/code> property. Here&#8217;s some sample code:<\/p>\n<pre>\/\/ C#\r\nvar notifier = ToastNotificationManager.CreateToastNotifier();\r\nvar setting = notifier.Setting;\r\n\r\n\/\/ C++\/WinRT\r\nauto notifier = ToastNotificationManager::CreateToastNotifier();\r\nauto setting = notifier::Setting();\r\n\r\n\/\/ C++\/CX\r\nauto notifier = ToastNotificationManager::CreateToastNotifier();\r\nauto setting = notifier-&gt;Setting;\r\n\r\n\/\/ JS\r\nvar notifier = ToastNotificationManager.createToastNotifier();\r\nvar setting = notifier.setting;\r\n\r\n\/\/ VB\r\nDim notifier = ToastNotificationManager.CreateToastNotifier()\r\nDim setting = notifier.Setting\r\n<\/pre>\n<p>The resulting value in <code>setting<\/code> tells you whether toasts are enabled, or if not, why not. You can use this to display specific instructions to the user.<\/p>\n<pre>switch (setting) {\r\ncase NotificationSetting.Enabled:\r\n    \/\/ everything is great.\r\n    break;\r\n\r\ncase NotificationSetting.DisabledForApplication:\r\n    Warn(\"Please go to Settings, System, Notifications &amp; Actions \" +\r\n         \"and enable this application in the \"\r\n         \"'Get notifications from these senders' list.\");\r\n    break;\r\n\r\ncase NotificationSetting.DisabledForUser:\r\n    Warn(\"Please go to Settings, System, Notifications &amp; Actions \" +\r\n         \"and set \"\r\n         \"'Get notifications from apps and other senders' to On.\");\r\n    break;\r\n\r\ncase NotificationSetting.DisabledByGroupPolicy:\r\n    Warn(\"Your system administrator has prevented us from \" +\r\n         \"showing notifications.\");\r\n    break;\r\n\r\ncase NotificationSetting.DisabledByManifest:\r\n    Warn(\"Oops. We forgot to ask the operating system for permission \" + \r\n         \"to display toast notifications. Please file a bug.\");\r\n    break;\r\n\r\n\/\/ Catch-all case for reasons that are defined\r\n\/\/ in future versions of Windows.\r\ndefault:\r\n    Warn(\"It doesn't look like toast notifications are enabled, \" +\r\n         \"but we don't know why.\");\r\n    break;\r\n}\r\n<\/pre>\n<p>Note that this is even better than what the customer requested, because it also detects other cases, such as where the user left the notification enabled for your app, but then went and disabled all notifications globally.<\/p>\n<p><b>Bonus chatter<\/b>: You could go the extra mile and launch the Settings app directly to the <i>Notifications &amp; Actions<\/i> page.<\/p>\n<pre>await Launcher.LaunchUriAsync(\"ms-settings:notifications\");\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>The ToastNotifier settings will tell you.<\/p>\n","protected":false},"author":1069,"featured_media":111744,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[25],"class_list":["post-104058","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-oldnewthing","tag-code"],"acf":[],"blog_post_summary":"<p>The ToastNotifier settings will tell you.<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/104058","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/users\/1069"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/comments?post=104058"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/104058\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/media\/111744"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/media?parent=104058"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/categories?post=104058"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/tags?post=104058"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}