1

我正在使用QtWebkit创建一个简单的浏览器,我设法添加了对Notification Web API的支持,使用QWebPage::setFeaturePermission.

例子:

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>

我的代码:

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";
    }
}

每次单击“通知我”按钮时,桌面上都会出现以下消息:

桌面通知

可以自定义 QT 中的通知吗?换句话说,离开类似于 GoogleChrome 或 Firefox,像这样:

网络通知

4

2 回答 2

3

要自定义Notifications Web APIQtWebkit您必须使用“Webkit 插件”,换句话说,创建一个插件并放入qtdir/plugins/webkit.

注意:创建插件需要<QtWebKit/QWebKitPlatformPlugin>

创建插件:

  • 在 QtCreator 中创建一个项目
  • .pro文件使用中(示例src.pro):

    TARGET = $$qtLibraryTarget(mywebkitplugin)
    TEMPLATE = lib
    CONFIG += plugin
    
    HEADERS += $$[QT_INSTALL_HEADERS]/QtWebKit/qwebkitplatformplugin.h \
        mywebkitplugin.h
    
    SOURCES += \
        mywebkitplugin.cpp
    
    Release:DESTDIR     = $$PWD/bin/release
    Release:UI_DIR      = $${DESTDIR}/.ui
    Release:MOC_DIR     = $${DESTDIR}/.moc
    Release:RCC_DIR     = $${DESTDIR}/.rcc
    Release:OBJECTS_DIR = $${DESTDIR}/.obj
    
    Debug:DESTDIR       = $$PWD/bin/debug
    Debug:UI_DIR        = $${DESTDIR}/.ui
    Debug:MOC_DIR       = $${DESTDIR}/.moc
    Debug:RCC_DIR       = $${DESTDIR}/.rcc
    Debug:OBJECTS_DIR   = $${DESTDIR}/.obj
    
  • 创建 mywebkitplugin.h

    #ifndef MYWEBKITPLUGIN_H
    #define MYWEBKITPLUGIN_H
    
    #include <QtWebKit/QWebKitPlatformPlugin>
    
    class MyWebKitPlugin : public QObject, public QWebKitPlatformPlugin
    {
        Q_OBJECT
        Q_INTERFACES(QWebKitPlatformPlugin)
    
    #if QT_VERSION >= 0x050000
        Q_PLUGIN_METADATA(IID "org.qtwebkit.QtWebKit.QtWebPlugin")
    #endif
    
    public:
        explicit MyWebKitPlugin();
        ~MyWebKitPlugin();
    
        bool supportsExtension(Extension ext) const;
        QObject* createExtension(Extension ext) const;
    };
    
    #endif // MYWEBKITPLUGIN_H
    
  • 创建 mywebkitplugin.cpp

    #include "mywebkitplugin.h"
    #include "notification/notification.h"
    
    MyWebKitPlugin::MyWebKitPlugin()
    {
    }
    
    MyWebKitPlugin::~MyWebKitPlugin()
    {
    }
    
    bool MyWebKitPlugin::supportsExtension(Extension ext) const
    {
        return ext == Notifications;
    }
    
    QObject* MyWebKitPlugin::createExtension(Extension ext) const
    {
        switch (ext) {
            case Notifications:
                return new Notification();
    
            default:
                return 0;
        }
    }
    
    //for QT-4.8
    #if QT_VERSION < 0x050000
    Q_EXPORT_PLUGIN2(webkitplugin, MyWebKitPlugin);
    #endif
    
  • 创建notification文件夹

  • 在通知文件夹中放置通知类:

    通知.h

    #ifndef NOTIFICATION_H
    #define NOTIFICATION_H
    
    #include <QtWebKit/QWebKitPlatformPlugin>
    
    class Notification : public QWebNotificationPresenter
    {
        Q_OBJECT
    
    public:
        explicit Notification();
        ~Notification();
    
        void showNotification(const QWebNotificationData* data);
    
    signals:
        void notificationClosed();
        void notificationClicked();
    };
    
    #endif // NOTIFICATION_H
    

    通知.cpp

    #include "notification.h"
    #include <QDebug>
    
    Notification::Notification() : QWebNotificationPresenter()
    {
        qDebug() << "Create: Notification";
    }
    
    Notification::~Notification()
    {
        qDebug() << "Delete: this (Notification)";
    }
    
    void Notification::showNotification(const QWebNotificationData* data)
    {
        qDebug() << "title:" << data->title();
        qDebug() << "icon:" << data->iconUrl();
        qDebug() << "message:" << data->message();
        qDebug() << "opener page:" << data->openerPageUrl();
    }
    

用于创建您的通知自定义更改Notification::showNotification(const QWebNotificationData* data)内容并QWebNotificationData* data用于从JavaScript API.

  • 创建notification.pri(包含在 中src.pro):

    QT += network
    
    HEADERS += \
        $$PWD/notification.h
    
    SOURCES += \
        $$PWD/notification.cpp
    
  • 添加:notification.pri_src.pro

    include($$PWD/notification/notification.pri)
    

编译/构建:

  • src.pro在 QtCreator 中打开
  • 单击Build(在释放模式下)(或使用Ctrl+ B)按钮(不要单击Run按钮,不要使用 Ctrl+ R
  • 关闭src.pro
  • 转到位于src.pro
  • (如果是释放模式)打开bin/release文件夹
  • (如果是调试模式)打开bin/debug文件夹
  • (如果是释放模式)复制mywebkitplugin.dllQtDir/plugins/webkit/mywebkitplugin.dll(例如使用 mingw: C:/qt/qt5.4/mingw/plugin/webkit/mywebkitplugin.dll
  • (如果是调试模式)复制mywebkitplugind.dllQtDir/plugins/webkit/mywebkitplugind.dll(例如使用 mingw: C:/qt/qt5.4/mingw/plugin/webkit/mywebkitplugind.dll
  • 如果webkit文件夹不存在,则创建它。
  • 打开您的项目QWebView并进行测试Notification Web API

当运行一个使用 的项目时QWebView,它会自动加载dll(在你的项目中不需要额外的配置)并且会为你的“自定义小部件”“替换”默认值NotificationsQtWebkit在 Windows 中使用SystemTrayIconshow Notification Web API)。

插件项目的文件夹结构:

mywebkitplugin
├── `src.pro`
├── mywebkitplugin.h
├── mywebkitplugin.cpp
└── notification
    ├── notification.h
    ├── notification.cpp
    └── `notification.pri`
于 2015-03-11T16:40:23.840 回答
0

新通知可以采用以下 2 个参数:

var notification = new Notification(title, options);

作为选项对象的一部分,您可以传递要在通知中显示的“正文”和“图标”。

于 2015-03-11T15:26:49.197 回答