mohabouje / WinToast

WinToast is a lightly library written in C++ which brings a complete integration of the modern toast notifications of Windows 8 & Windows 10. Toast notifications allows your app to inform the users about relevant information and timely events that they should see and take action upon inside your app, such as a new instant message, a new friend request, breaking news, or a calendar event.
MIT License
688 stars 127 forks source link

Feature request: notification listener #51

Open SamRSA opened 4 years ago

SamRSA commented 4 years ago

Is it possible to extend library with the ability to listen and read user notifications ? More details here: https://docs.microsoft.com/en-us/windows/uwp/design/shell/tiles-and-notifications/notification-listener (example code in article is completely in C#)

I have already started doing it (like 40% complete), but stock on "UserNotificationListenerAccessStatus accessStatus = await listener.RequestAccessAsync();" stage.

Here is code:

ComPtr<IUserNotificationListenerStatics> notificationListener;
HRESULT hr = DllImporter::Wrap_GetActivationFactory(WinToastStringWrapper(RuntimeClass_Windows_UI_Notifications_Management_UserNotificationListener).Get(), &notificationListener);
if (SUCCEEDED(hr)) {

    ComPtr<IUserNotificationListener> listener;
    hr = notificationListener->get_Current(&listener);
    if (SUCCEEDED(hr)) {

        // ComPtr<UserNotificationListenerAccessStatus> accessStatus;
        ComPtr<__FIAsyncOperation_1_Windows__CUI__CNotifications__CManagement__CUserNotificationListenerAccessStatus> accessStatus;

        // UserNotificationListenerAccessStatus accessStatus = await listener.RequestAccessAsync();
        hr = listener->RequestAccessAsync(&accessStatus);
        if (SUCCEEDED(hr)) {

            switch (accessStatus)
            {
                case UserNotificationListenerAccessStatus_Allowed:
                    std::wcout << L"Toast Allowed" << std::endl;
                    break;

                case UserNotificationListenerAccessStatus_Denied:
                    std::wcout << L"Toast Denied" << std::endl;
                    break;

                case UserNotificationListenerAccessStatus_Unspecified:
                    std::wcout << L"Toast Unspecified" << std::endl;
                    break;
            }

        }
    }

Has anyone already done this in C++ ? If not, let's add this functionality together.

mohabouje commented 1 year ago

Do you have a working version of this feature that we can merge into a PR? I am organizing the features for the coming version and thinking to add support for this.