1

So I am using a WebView in my UWP application and I would like to handle HTML5 notifications. What I did was to add support for the ScriptNotify event to my webview (here).

 MyWebView.ScriptNotify += WebView_Notify;
 private void WebView_Notify(object sender, NotifyEventArgs e)
 {
    Debug.WriteLine("NOTIFIED " + e.Value);
 }

Then using InvokeScriptAsyncI ran the following javascript code:

(function() {
    var N = window.Notification;

    window.external.notify(N.permission);
    var P = function (title, options){
        window.external.notify(title)
    };
    P.permission = 'granted';
    N = P
})();

However, this does not work. In my debug output I do get:

"NOTIFIED default`

which means that ScriptNotify handler is being triggered. However, how can implement HTML5 Notifications support in my app?

reckless
  • 741
  • 12
  • 53

1 Answers1

2

I found a new solution that might help you.

A notification request is initiated when the page loads, WebView can intercept it, using the event PermissionRequested.

private void WebView_PermissionRequested(WebView sender, WebViewPermissionRequestedEventArgs args)
{
    if (args.PermissionRequest.PermissionType == WebViewPermissionType.WebNotifications)
    {
        args.PermissionRequest.Allow();
    }
}

You can also pop up a window when you listen for a request, letting the user choose whether to accept the notification.

Best regards.

Richard Zhang
  • 7,523
  • 1
  • 7
  • 13
  • This seems great! However, I found 2 main issues: first one is that upon restart the `WebView` doesn't "remember" that the permission has been granted previously and second is that I am not sure how to intercept when a notification is triggered. – reckless Aug 15 '19 at 18:53
  • Regarding the first point. Yes, every time you restart WebView, it is equivalent to creating a new browser, it will not save and record user data. Regarding the second point, unfortunately, I have not found a way to externally intercept the triggered notification and get the content. – Richard Zhang Aug 16 '19 at 01:01
  • yes, first problem can easily be solved but I am still looking for a solution for the second problem. See my attempt [here](https://stackoverflow.com/questions/57523910/intercept-html5-web-notifications-in-a-browser-environment) – reckless Aug 16 '19 at 11:56
  • @daljit97 can you outline how you solved the first problem? – Barak Gall Jun 27 '21 at 14:25