How can I tell whether the user has disabled toast notifications for my app?

Raymond Chen

A customer wanted to know how to get the complete list of programs from the Settings, System, Notifications & Actions, Get notifications from these senders list, as well as whether the user has enabled notifications for each app.

Upon closer questioning, it turns out that this isn’t really what they wanted. They didn’t need the complete list of apps and the user’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, “You need to enable notifications for Contoso Sports to receive game alerts.”

In a sense, this was a case of the “for/if” antipattern: Instead of asking for the thing that they actually want, they ask for everything and then try to filter to the thing that they want.

The way to find out whether notifications are enabled for your app is to check the ToastNotifier.Setting property. Here’s some sample code:

// C#
var notifier = ToastNotificationManager.CreateToastNotifier();
var setting = notifier.Setting;

// C++/WinRT
auto notifier = ToastNotificationManager::CreateToastNotifier();
auto setting = notifier::Setting();

// C++/CX
auto notifier = ToastNotificationManager::CreateToastNotifier();
auto setting = notifier->Setting;

// JS
var notifier = ToastNotificationManager.createToastNotifier();
var setting = notifier.setting;

// VB
Dim notifier = ToastNotificationManager.CreateToastNotifier()
Dim setting = notifier.Setting

The resulting value in setting tells you whether toasts are enabled, or if not, why not. You can use this to display specific instructions to the user.

switch (setting) {
case NotificationSetting.Enabled:
    // everything is great.
    break;

case NotificationSetting.DisabledForApplication:
    Warn("Please go to Settings, System, Notifications & Actions " +
         "and enable this application in the "
         "'Get notifications from these senders' list.");
    break;

case NotificationSetting.DisabledForUser:
    Warn("Please go to Settings, System, Notifications & Actions " +
         "and set "
         "'Get notifications from apps and other senders' to On.");
    break;

case NotificationSetting.DisabledByGroupPolicy:
    Warn("Your system administrator has prevented us from " +
         "showing notifications.");
    break;

case NotificationSetting.DisabledByManifest:
    Warn("Oops. We forgot to ask the operating system for permission " + 
         "to display toast notifications. Please file a bug.");
    break;

// Catch-all case for reasons that are defined
// in future versions of Windows.
default:
    Warn("It doesn't look like toast notifications are enabled, " +
         "but we don't know why.");
    break;
}

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.

Bonus chatter: You could go the extra mile and launch the Settings app directly to the Notifications & Actions page.

await Launcher.LaunchUriAsync("ms-settings:notifications");

6 comments

Discussion is closed. Login to edit/delete existing comments.

  • Martin Soles 0

    I appreciate the effort to be inclusive. I do. VB doesn’t use a semicolon to indicate the end of statement. I guess you could use a colon if you wanted to put everything on a single line. A VB statement is normally considered complete at the end of line marker. (You can use a trailing underscore to indicate that the statement continues on the next line or let the compiler infer it based on the context.)

    If the official Microsoft statement is that VB is “feature complete and will not see another language update”, why bother with new samples in that language?

    • Raymond ChenMicrosoft employee 0

      Oops, sorry, too much copy/pasta in the morning. The purpose here was to show people how to do it in VB. Maybe they have an existing program in VB and they want to add this tiny feature to it without having to scratch it all and rewrite in C#.

      • 紅樓鍮 0

        Also I think it should be notifier.Setting() instead of notifier::Setting() for C++/WinRT.

  • Alex Martin 0

    By mid-next week there will be 6000 posts on this blog. Congrats, Raymond. And also condolences for your loss of free/productive time.

  • Mystery Man 0

    Well, aren’t you an angel, Mr. Chen. I was about to look this topic up right now.

    Still, this is Windows Runtime API… so, I guess there are still a few things I need to look up.

  • Christopher Lee 0

    Note that accessing the ToastNotifier.Setting property might raise an exception if your application is not fully registered with the system. Rudy Huyn discusses this in ToastNotifier::Setting: Careful with non-UWP applications! with the details. However, I’ve also personally observed error code 0x80070490 (SEVERITY_ERROR, FACILITY_WIN32, ERROR_NOT_FOUND) raised from a JavaScript app when running on an Xbox One device in Developer Mode.

Feedback usabilla icon