我正在使用QtWebkit创建一个简单的浏览器,我设法使用QWebPage::setFeaturePermission
添加对Notification Web API的支持。
示例:
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";
}
}
每次点击&#34;通知我&#34; 按钮,桌面上都会显示以下消息:
可以在QT中自定义通知吗?换句话说,类似于GoogleChrome或Firefox,就像这样:
答案 0 :(得分:3)
要在Notifications Web API
中自定义QtWebkit
,您必须使用&#34; Webkit插件&#34;,换句话说,创建一个插件并放入qtdir/plugins/webkit
。
注意:对于创建插件,需要
<QtWebKit/QWebKitPlatformPlugin>
class
在.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
文件夹
在通知文件夹中放置通知类:
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
notification.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
Build
(在发布模式下)(或使用 Ctrl + B )按钮(不要点击在Run
按钮中,不使用 Ctrl + R )src.pro
src.pro
bin/release
文件夹bin/debug
文件夹mywebkitplugin.dll
复制到QtDir/plugins/webkit/mywebkitplugin.dll
(例如,使用mingw:C:/qt/qt5.4/mingw/plugin/webkit/mywebkitplugin.dll
)mywebkitplugind.dll
复制到QtDir/plugins/webkit/mywebkitplugind.dll
(例如,使用mingw:C:/qt/qt5.4/mingw/plugin/webkit/mywebkitplugind.dll
)webkit
文件夹不存在,请创建它。QWebView
打开您的项目并测试Notification Web API
。运行使用QWebView
的项目时,它会自动加载dll
(您的项目中不需要额外配置),并且#34;替换&#34; Windows中的默认Notifications
(QtWebkit
使用SystemTrayIcon
for show Notification Web API
)为您的&#34;自定义小部件&#34;。
mywebkitplugin
├── `src.pro`
├── mywebkitplugin.h
├── mywebkitplugin.cpp
└── notification
├── notification.h
├── notification.cpp
└── `notification.pri`
答案 1 :(得分:0)
新通知可以采用以下两个参数:
var notification = new Notification(title, options);
作为选项对象的一部分,您可以传递&#39; body&#39;和&#39;图标&#39;显示在通知中。