3

I am using Visual Studio 2017 Professional.

I have been following this guide: https://learn.microsoft.com/en-us/windows/uwp/design/shell/tiles-and-notifications/notification-listener

My problem code is as follows:

using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Windows.UI.Notifications;
using System;

namespace SeleniumWebChat.Utilities
{
    // This class handles Windows 'Toast' notifications that pop up when a new webchat/call is received
    static class WinNotificationHandler
    {
        static async Task<bool> TaskCheckWinNotification(string notifType, string guest)
        {
            Windows.UI.Notifications.Management.UserNotificationListener listener = Windows.UI.Notifications.Management.UserNotificationListener.Current;

            //only read notifications if we have access - this may need to be set in windows settings
            if (listener.GetAccessStatus().ToString() == "Allowed")
            {

                // Get the windows toast notifications as a list 
                IReadOnlyList<UserNotification> notifs = await listener.GetNotificationsAsync(NotificationKinds.Toast);
                //Console.Error.WriteLine("number of notifications found: " + notifs.Count());
            }
            return false;
        }
    }
}

The problem is with this line:

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

Which gives the error:

'IAsyncOperation<IReadOnlyList<UserNotification>>' does not contain a definition for 'GetAwaiter' and no extension method 'GetAwaiter' accepting a first argument of type 'IAsyncOperation<IReadOnlyList<UserNotification>>' could be found (are you missing a using directive for 'System'?)

I've tried everything I can find online, from adding references to:

  • C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework.NETCore\v4.5\System.Runtime.WindowsRuntime.dll
  • C:\Program Files (x86)\Windows Kits\10\UnionMetadata\Facade\Windows.WinMD

To adding NuGet packages:

  • UwpDesktop
  • Microsoft.NETCore.UniversalWindowsPlatform

To reinstalling VS and trying different versions of the SDK.

I really have no idea what is causing this issue, I would be very grateful for any pointers in the right direction.

H H
  • 263,252
  • 30
  • 330
  • 514
Thomas Fuzesi
  • 33
  • 1
  • 4
  • 2
    From [docs](https://learn.microsoft.com/en-us/uwp/api/windows.foundation.iasyncoperation_tresult_):, ".NET has the AsTask extension method" so... if you want to use `await`, try using `.AsTask()` ? - no idea if this will help – Marc Gravell Feb 04 '19 at 12:07
  • If that doesn't help, tell us exactly what kind of App you are making (which UWP template etc). Even better, write a [mcve] . – H H Feb 04 '19 at 12:25
  • It looks like you're doing it correct. Try rebuilding the project and also hover the mouse over the ```GetNotificationsAsync``` in Visual Studio and tell me what the intellisense is saying it returns please. – Michael Puckett II Feb 04 '19 at 14:00
  • Not the issue but it's also better to compare the enum to an enum value rather than using the string. ```listener.GetAccessStatus() == UserNotificationListenerAccessStatus.Allowed``` – Michael Puckett II Feb 04 '19 at 14:03
  • @MichaelPuckettII - It gives the same error as above - 'IAsyncOperation>' does not contain a definition for 'GetAwaiter' and no extension method 'GetAwaiter' accepting a first argument of type 'IAsyncOperation>' could be found (are you missing a using directive for 'System'?) – Thomas Fuzesi Feb 04 '19 at 17:13
  • @HenkHolterman - It is a .NET Framework class library and can be reproduced by making a fresh project with a class containing my code above, then adding a reference to C:\Program Files (x86)\Windows Kits\10\UnionMetadata\Windows.winmd and C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.6.1\Facades\System.Runtime.dll – Thomas Fuzesi Feb 04 '19 at 17:18

1 Answers1

4

You should add

 using System.WindowsRuntimeSystemExtensions;

this is from the System.Runtime.WindowsRuntime assembly and holds a class with the GetAwaiter extensions.

H H
  • 263,252
  • 30
  • 330
  • 514
  • 1
    Thanks! For anyone wondering, I added this assembly from C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETCore\v4.5\System.Runtime.WindowsRuntime.dll – Thomas Fuzesi Feb 07 '19 at 10:21