我正在使用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,像这样: