Notification Listener Documentation – Windows 10


Content moved

Please see the article on docs.microsoft.com for the latest documentation.

Comments (41)

Cancel reply

  1. Neil Kay says:

    Interesting stuff, Andrew. I watched a couple of the action center / toast build conferences, and this feels like it could be useful and a time saver on cross platform notification infrastructure.

    We have a use case that feels like this can help, but it’s not one covered in the build sessions nor can I find any documentation online.

    We have a heavy desktop PC (win32) application – kind of like embedded software for a physical machine (let’s call it a robot) – which an operator will push a button and come back to it hours later when it should be done. If there’s errors, we want to alert them to go back to the machine. We have sample code to cause a toast to be sent, so if the operator is logged into another PC, then the toast can be seen. We were desiring this also to be able to send the toast to mobile (if they are using Windows Phone or Cortana on Android/iOS).

    There isn’t and would never be a full App of our product on mobile, as it’s far too heavy and requires PC drivers to move the hardware (robot). We were thinking would would need to start writing a Xamarin app in order to get push notifications displayed on their mobile.

    What we saw at build seems like it opens up new, simpler, possibilities. However, the mirroring of the mobile notifications demod seemed to be synching mobile TO PC, but not the other way. i.e. For us, we want a notification our Application creates on a PC, which appears in the PC Action Center, we want to synch so it also shows as a mobile notification. Is that currently possible with the mirrored notifications? Initial tests didn’t appear to work, even though I’m running Win10 latest, I’m signed in to same microsoft account on both, and I’ve got latest Cortana on Android (side loaded as not released in UK yet)…

    Are the mirrored notifications supposed to be bi-directional, either now, or potentially in the not too distant future?
    If currently it’s only mirroring mobile to PC, would writing a light mobile App of the same name/ID as our desktop app, where the notification listener technique (published here) is used to look for notifications from our PC application, and generating local notifications on the phone, work for this scenario?

    Thanks

    1. andrewbares7 says:

      Hey Neil! Your best option right now would be to make a basic mobile app that users install on their phones, which you can then send push notifications to. The app wouldn’t need to do anything else, so it should be relatively simple.

      You’re correct that notification mirroring only works from Mobile to PC, and not the other direction. We’ve considered doing the other direction, but we’re not sure most users would find that valuable. But we haven’t closed the door on it. There’s just no immediate plans.

      Rather than writing an app that uses Notification Listener on the PC and then sends the notifications to the phone (which would mean writing two apps, or at least two major components), I would go with the solution I proposed at the beginning – because in either case you still need your user to install something on their phone.

  2. Ehad Akeila says:

    Hi Andrew,

    Thanks a lot for sharing this. I’m still in the process of testing feature. I noticed that “Listener_NotificationChanged” event is not triggered in the foreground. The background process seems to work fine.

    Also, I was wondering if the following events can be captured in this feature:
    – When a phone call is received. I dont think this will be placed in the notification until the call is missed. Is this correct?
    – Is there any events/control for the main music in the phone/PC. It would be useful to control the running music from external device like smartwatch for example.

    Kind Regards
    Ehad

    1. andrewbares7 says:

      Hey Ehad! Hmm, I’ll check out the foreground event and see if it doesn’t work for me either, I’m not sure if I ever tried that one out since the primary use case is background tasks (since most apps wouldn’t be running while listening to notifications). But either way, if it’s broken, that’s a good find, so thanks!

      Phone calls – those actually use toast notifications, so they should theoretically appear in the listener. I haven’t tested that though.

      Media controls – Our team doesn’t own that, and I’m not sure if there’s a public API available for that. Accessory apps like Microsoft Band historically used AccessoryManager, which includes access to things like notifications, and I believe media controls, but AccessoryManager is hidden behind a restricted capability and we’re working on deprecating it (hence why we created Notification Listener, which is a public capability). Try posting this question on the MSDN forums.

      1. Ehad Akeila says:

        Hi Andrew,

        I’ve been experimenting with this API. I found out another thing. The phone call itself is not placed in the notification while the call is in progress. It is only available in the notification once the call is missed. Is this something you can fix as part of this feature or is it the responsibility of another team?

        Kind Regards
        Ehad

        1. andrewbares7 says:

          Ah dang, that’s too bad phone calls don’t come through. However, with the current limited information that the Listener provides (it doesn’t include any buttons from the toast), it wouldn’t be very useful surfacing those phone calls anyways, since you wouldn’t be able to answer/reject.

          There’s an API for phone calls: Windows.ApplicationModel.Calls. But it doesn’t look like it includes events for incoming calls.

          I’m not sure whether incoming calls will become their own thing, separate from notifications. There’s good arguments for both options. We’ll just have to see what the future brings!

          1. Ehad Akeila says:

            Hi Andrew,

            I really hope these phone call events can be triggered. Otherwise, those notification API will be less useful for wearables.
            On another note, I’m not sure if you are aware of this issue. The notification event so far gives me the source and content of the notification, however, I could not find where to get the sender or the subject if it is an email. I’ve tested that in some apps like facebook, sms and viber. I’m not sure if this is an issue or is it something I didnt do properly!

            Kind Regards
            Ehad

        2. andrewbares7 says:

          (seems like I can’t reply to your most recent comment, so replying up here)

          Notifications don’t have any strongly typed information like email sender or email subject. Notifications are just a set of text elements. We show how to retrieve those in the “Displaying the notifications” section, basically: toastBinding.GetTextElements();

    2. andrewbares7 says:

      After testing, it looks like you’re right, the foreground NotificationChanged event doesn’t work! Whoops ūüėõ We’re working on a fix. I’ll reply to this when the fix is in Insider builds.

      1. Ehad Akeila says:

        Thanks for the update ūüėČ

        1. Ehad Akeila says:

          Ok, here are another thing to test. The “toastBinding” returns null when the notification is Facebook or Email. I’ve tested with alarm and it works. So far I can capture the source of all notifications

          1. andrewbares7 says:

            That’s another known big that we’ve fixed (those notifications were using the legacy ToastText02/etc toast payloads). I don’t think our fix for that is in Insider builds yet. I need to update the article with a Known Preview Issues section.

            Either way, you should always check whether the toast binding is null for error proof code, and then skip those notifications. They’ll start working soon though!

        2. andrewbares7 says:

          Hey Ehad, I have an update about the foreground event bug.

          The foreground event won’t work for the Anniversary Update. We’ll look at fixing that in the future. You’ll have to use the background trigger instead. Hopefully that’s fine for your app?

          1. Ehad Akeila says:

            Sorry I just noticed your post. I think it’s fine. I was only using it to test the notifications in the background ūüėČ

  3. Is this API available for HTML-based apps?

    1. andrewbares7 says:

      Yep! All UWP API’s should work in Javascript UWP apps!

  4. Sergey Vasiliev says:

    Hi! That is great, but. How should I deal with it if my wearable is a “write-only” device? It’s just a display without any memory. I have no way to “sync”. AccessoryManager API has it’s own “per-app” queue for each event.
    Should I create some history cache in my app?

    1. andrewbares7 says:

      Hey Sergey! Yep, you should create a cache in your app. The easiest thing to do would be to use local settings, and store a list of uint in a setting entry.

      Using the code in the “Determining which notifications were added and removed” section, the GetNotificationsOnWearable() method would simply return the list value from your settings, and then at the end of that entire code section, you can update the list in settings by simply using a linq query to select the current notification ID’s, something like: userNotifications.Select(n => n.Id).ToList();

  5. Ehad Akeila says:

    Hi Andrew,

    I’ve started experiencing instability issues with the Notifications feature in the latest build 10.0.14385. Sometimes the backgroundtask is not triggered at all. And recently I tracked the issue and discovered that the app crashes when calling:

    IReadOnlyList notifs = await listener.GetNotificationsAsync(NotificationKinds.Toast);

    The error is :The data is invalid. (Exception from HRESULT: 0x8007000D)

    I don’t know if there is something wrong with my app or the new build has introduced something I’m not aware of..

    Kind Regards
    Ehad

    1. andrewbares7 says:

      I think there were a few bugs around the 14385 build that could be causing that. I’m not seeing any exceptions on 14387 today, I’ve been monitoring it today.

      Were you seeing this on Mobile or Desktop?

      1. Ehad Akeila says:

        It was more on Mobile. Desktop was a bit more stable. On mobile, the error used to be annoying but after some time it’s been working well.

    2. andrewbares7 says:

      As for the reliability and timeliness of the background task, we found an issue where it will occasionally be delayed, especially on Mobile in cases where there are battery concerns.

      We’re looking at a fix, it’d help to know if you’re planning on releasing an app using this, and therefore we know someone would benefit!

      1. Ehad Akeila says:

        Hi Andrew,

        Thanks for the update. I’ve been testing this feature for about over week. It’s working well so far especially after installing the latest Windows 10 builds. Do you know when we can start publishing apps with this feature? I’m not sure if everything will be ready on August, 2nd.

        Kind Regards
        Ehad

        1. andrewbares7 says:

          That’s awesome to hear! Do you mind sharing what app you’re creating? We’d love to know how you’re using the Listener! You can find our email address in the Notifications Visualizer app (on the About page) if you don’t want to share your app publically.

          As for releasing apps that use the Anniversary Update SDK… you have to wait till the official SDK is available, unfortunately you’re not able to submit apps using the preview SDK. I don’t know the timeline for when the official SDK will be released (and I probably wouldn’t be able to share that information anyways).

          1. EhadAkeila says:

            Yes sure. I’ll contact you soon.

  6. Jahankohan says:

    Hi Andrew,

    Thanks for your great support,

    I’ve got some trouble with this, here is my questions:

    1- What build version of windows insider should I use? Currently I’m using insider build 14366.0, should I Change it?
    2- What changes/upgrades should I do in Visual studio? Currently I’m using visual studio v3.
    3- Is there any sample that I can use? any thing I can learn from? I just find a documentation of it but it is not completely practical?

    Thanks.

    1. andrewbares7 says:

      Hey Reza!

      1. If you’re talking about the OS that you’re using on your computer/phone, I would definitely recommend upgrading to build 14393 (the latest Insider build), there were a number of Listener bug fixes that probably weren’t available in your current build.

      If you’re talking about which SDK to use, I believe the 14366 SDK is the latest one¬†available? The SDK won’t change anything – our bug fixes all occurred in the OS itself.

      2. Doesn’t matter which version of Visual Studio you use – Update 3 is the latest, that’ll be fine.

      3. We have a sample app that replicates Action Center, you can check it out here: https://github.com/WindowsNotifications/NotificationListenerActionCenterApp

      We don’t have any other samples. What type of sample would you find most useful? I’d love to know, so that we know what we should focus on!

      Thanks!
      Andrew

      1. Serna Wong says:

        Hi Andrew,

        Does this sample have a C++ version?

        1. andrewbares7 says:

          Hey Serna! No, we don’t have a C++ sample of this, hopefully it’s not too difficult to translate to C++.

          If we get more requests for C++ samples, we’ll consider including C++ for this and future documentation/samples.

          1. Serena Wong says:

            Hi Andrew, Thanks for the reply.
            For our case, we just want to get notified if any new toast notification comes in, and we doesn’t care about its content or any dismissed notification. Is there any simpler interface to do this? or, I still have to compare the delta?

            Thanks

  7. Luis Silva says:

    Hey there!

      string titleText = textElements.FirstOrDefault()?.Text;

    and

    ¬†¬†string bodyText = string.Join(“\n”, textElements.Skip(1).Select(t => t.Text));

    are returning non-alphabetical characters (low-values mixed with letters).

    Kind Regards
    Luis Silva

    1. andrewbares7 says:

      Hmm interesting, do you have a specific repro of that issue? Is it happening for every single notification? Or just a specific app?

      Mobile or Desktop? What build are you running? What language/culture is your machine?

  8. EhadAkeila says:

    Hi Andrew,

    I noticed a similar thing with the new update (version 14905). Apps like timer and some games have the first value (usually the name of the app) is returned as the path (@C:/…..).

    Kind Regards
    Ehad

    1. andrewbares7 says:

      Hmm interesting, are these all third party apps/games? Can you give me specific names of the apps? I’m not seeing any “C:” paths from my phone, but I likely don’t have those same apps installed. Thanks!

      1. Ehad Akeila says:

        Hi Andrew,
        Sorry for the late reply. I just saw your comment. I happened on some games that send notifications. I’ll try to capture that when it happens again.

        Kind Regards
        Ehad

      2. Ehad Akeila says:

        Hi Andrew,

        I just captured one of the apps. It’s a game called Dragon Mania Legends. The first line of the notification is:
        “@C:\Data\Programs\{8ae2ccd0-4c54-4951-bf0f-83d714fc0dc0\-Install”

  9. Shibly says:

    I am working with notifications listener currently and I found few things that I need to ask you, it would be great if you clear me these things.

    1. await Launcher.LaunchUriAsync(new Uri(“ms-settings:privacy-notifications”)); should launch the notification section in privacy settings but it doesn’t. is it a problem or I am getting this wrong ?.
    2. if change access in settings > privacy > notifications it closes the foreground app , is there any way to fix it?

    1. andrewbares7 says:

      Hey Shibly, I think we talked on FB, but I’m going to post a comment here for the benefit of everyone else too!

      1)¬†We’re looking into¬†why the URL¬†scheme for settings doesn’t work. Thank you¬†for reporting that!

      2)¬†For when¬†the user¬†*revokes* access from the settings,¬†we purposefully close apps using the API to respect the user’s decision to revoke access (otherwise it might feel like the app still has access¬†since the app could have cached some notifications in memory).

      However, when the user *grants* access, you might be right that¬†it’d be a better experience to not close the app.¬†One of the reasons to close the app, however, is because some apps might¬†only do their registration code on first launch, rather than¬†on resume, so if the user returns to the app after granting access, the app still won’t be behaving correctly. But we’re¬†re-evaluating this¬†decision thanks to¬†your feedback!

  10. Asad says:

    i am getting error in app.xaml.cs in onBackgroundActivated function at ” await MyWearableHelpers.SyncNotifications();”
    system shows “task doesn’t contains definition for ‘syncNotifications’ and no extension method is accepting first argument of type ‘Task'”

    1. andrewbares7 says:

      Hey Asad! The “MyWearableHelpers.SyncNotifications()” function is a function that you would have to create yourself. I’ll add a comment in the code snippet to make that clear.

      That function should¬†do something similar to the code provided in the “Determining which notifications were added and removed” section. We also have a sample app on GitHub that replicates Action Center which you could try out if you need some more help with the API’s: https://github.com/WindowsNotifications/NotificationListenerActionCenterApp

      Let me know if you have any further questions!!

Skip to main content