c++qtqtwebkitqwebpage

How to customize "Notification Web API" in the QT


I'm creating a simple browser using QtWebkit, I managed to add support for Notification Web API it, using QWebPage::setFeaturePermission.

Example:

function notifyMe() {
    if (Notification.permission === "granted") {
        var notification = new Notification("Hi there!");
    } else if (Notification.permission !== "denied") {
        Notification.requestPermission(function(permission) {
            if (permission === "granted") {
                var notification = new Notification("Hi there!");
            }
        });
    }
}

<button onclick="notifyMe();">Notify me</button>

My code:

QObject::connect(page,
    SIGNAL(featurePermissionRequested(QWebFrame*, QWebPage::Feature)), this,
    SLOT(featurePermissionRequested(QWebFrame*,QWebPage::Feature))
);

...

void Form::featurePermissionRequested(QWebFrame* frame, QWebPage::Feature feature) {
    switch (feature) {
        case QWebPage::Notifications:
            qDebug() << "Notification";
            page->setFeaturePermission(frame, feature, QWebPage::PermissionGrantedByUser);
        break;
        case QWebPage::Geolocation:
            qDebug() << "GEO";
        break;
        default:
            qDebug() << "Unknown feature";
    }
}

Every time I click on the "Notify me" button the following message appears on the desktop:

Desktop Notification

It is possible to customize the notifications in QT? In other words, leave similar to the GoogleChrome or Firefox, like this:

Web Notification


Solution

  • To customize Notifications Web API in QtWebkit you must use the "Webkit plugins", in other words create a plugin and put in the qtdir/plugins/webkit.

    Note: For create plugin is needed <QtWebKit/QWebKitPlatformPlugin> class

    Create the plugin:

    For create your notification customized change Notification::showNotification(const QWebNotificationData* data) content and use QWebNotificationData* data for get data from JavaScript API.

    Compiling/build:

    When running a project that uses QWebView, it will automatically load the dll (no needs extras configurations in your project) and will "replace" the default Notifications (QtWebkit in Windows uses SystemTrayIcon for show Notification Web API) for your "custom widget".

    Folder structure for plugin projetct:

    mywebkitplugin
    ├── `src.pro`
    ├── mywebkitplugin.h
    ├── mywebkitplugin.cpp
    └── notification
        ├── notification.h
        ├── notification.cpp
        └── `notification.pri`