在处理Chrome的GCM推送通知时,当服务工作者接收到GCM的推送事件时,我已经设置了推送通知。
我正在启动一个服务调用。该服务返回一个数据对象,在该对象中我有一个URL,我希望在用户点击通知时打开它。
以下是我目前为止编写的代码。
我正在启动一个服务调用。该服务返回一个数据对象,在该对象中我有一个URL,我希望在用户点击通知时打开它。
以下是我目前为止编写的代码。
Var urlOpen = null;
self.addEventListener('push', function(event) {
event.waitUntil(
fetch(serviceLink).then(function(response) {
if (response.status !== 200) {
console.log('Looks like there was a problem. Status Code: ' +
response.status);
throw new Error();
}
return response.json().then(function(data) {
console.log(data);
var title = data.title;
var body = data.desc;
var icon = data.image;
var tag = 'notification tag';
urlOpen = data.clickUrl;
return self.registration.showNotification(title, {
body: body,
icon: icon,
tag: tag
})
});
})
);
});
self.addEventListener('notificationclick', function(event) {
event.waitUntil(
clients.openWindow(urlOpen).then(function (){
event.notification.close();}));
});
这段代码在大部分设备上能够正常工作,但有时候会出现通知问题。当点击通知时,浏览器的地址栏中显示 null
。
我做错了什么?