MacGapProject / MacGap2

MacGap 2
MIT License
1.2k stars 84 forks source link

Added HTML5 Notifications support #89

Closed loretoparisi closed 6 years ago

loretoparisi commented 6 years ago

I have added support for HTML5 Notifications through the NotificationProvider class and the WebNotification interface.

Examples of usage: In the html page, define this function to ask user permission to receive notifications:

var notificationShow = function (notif) {
        if (!Notification) {
            return;
        }
        if (Notification.permission !== "granted") {
            Notification.requestPermission();
        } else {
            var notification = new Notification(notif.title, {
                icon: notif.icon,
                body: notif.body,
            });
            notification.onclick = function () {
                window.open(notif.link);
            };
        }
    }//notifyUser
var myNotif={
 title: "New message",
 link: "http://localhost/deep_link",
 body: "You have got new messages(5)",
 icon: "http://someicon"
};
notificationShow(myNotif);

The code is based on https://github.com/jnordberg/irccloudapp/blob/master/NotificationProvider.m.

NOTE. I'm not handling deep linking at this time, so the notification will only put the app in foreground / open the app, but will not link right to the specified page.