获取 PushManager 订阅时出错 - JavaScript
Error getting PushManager Subscription - JavaScript
我有以下代码注册了一个 service worker 并要求用户允许通知。在用户允许 serviceWorkerRegistration.pushManager.getSubscription()
返回的承诺为 null
的推送通知后,我收到错误消息。当我关闭浏览器并再次强制调用此函数时,它可以正常工作。我不明白为什么。这是我的代码:
window.vapidPublicKey = new Uint8Array([4, 45, ...]);
if (navigator.serviceWorker) {
navigator.serviceWorker.register('/serviceworker.js')
.then(function(reg) {
console.log('Service worker change, registered the service worker');
});
}
// Otherwise, no push notifications :(
else {
console.error('Service worker is not supported in this browser');
}
navigator.serviceWorker.ready.then((serviceWorkerRegistration) => {
console.log("ready");
serviceWorkerRegistration.pushManager
.subscribe({
userVisibleOnly: true,
applicationServerKey: window.vapidPublicKey
});
});
// application.js
// Let's check if the browser supports notifications
if (!("Notification" in window)) {
console.error("This browser does not support notifications.");
}
// Let's check whether notification permissions have already been granted
else if (Notification.permission === "granted") {
console.log("Permission to receive notifications has been granted");
}
// Otherwise, we need to ask the user for permission
else if (Notification.permission !== 'denied') {
Notification.requestPermission(function (permission) {
// If the user accepts, let's create a notification
if (permission === "granted") {
console.log("Permission to receive notifications has been granted");
saveSubscriptionToDatabase();
}
});
}
function saveSubscriptionToDatabase(){
navigator.serviceWorker.ready
.then((serviceWorkerRegistration) => {
console.log(serviceWorkerRegistration.pushManager.getSubscription());
serviceWorkerRegistration.pushManager.getSubscription()
.then((subscription) => {
$.post("/users/save_subscription", { subscription: subscription.toJSON() });
});
});
}
错误:
Uncaught (in promise) TypeError: Cannot read property 'toJSON' of null
at serviceWorkerRegistration.pushManager.getSubscription.then
更新:
所以我添加了以下按钮:
<input type="button" onclick="saveSubscriptionToDatabase();" value="test">
所有 JS 和 post 请求都按预期工作,只要我从按钮单击调用函数但是当我从检查权限状态的条件调用它时,它仍然像以前一样失败.
更新 2:
我试过从不同的角度重新编写代码。
function registerServiceWorker() {
return navigator.serviceWorker.register('serviceworker.js')
.then(function(registration) {
console.log('Service worker successfully registered.');
return registration;
})
.catch(function(err) {
console.error('Unable to register service worker.', err);
});
}
function askPermission() {
return new Promise(function(resolve, reject) {
const permissionResult = Notification.requestPermission(function(result) {
resolve(result);
});
if (permissionResult) {
permissionResult.then(resolve, reject);
}
})
.then(function(permissionResult) {
if (permissionResult !== 'granted') {
throw new Error('We weren\'t granted permission.');
}
});
}
function subscribeUserToPush() {
return navigator.serviceWorker.register('serviceworker.js')
.then(function(registration) {
const subscribeOptions = {
userVisibleOnly: true,
applicationServerKey: new Uint8Array("key goes here")
};
return registration.pushManager.subscribe(subscribeOptions);
})
.then(function(pushSubscription) {
console.log("never called");
console.log('Received PushSubscription: ', JSON.stringify(pushSubscription));
return pushSubscription;
});
}
registerServiceWorker();
askPermission();
subscribeUserToPush();
subscribeUserToPush()
命令链的最后一部分 console.log("never called");
永远不会执行,也没有错误。我正在 Chrome 67 中对此进行测试。我肯定会调用 registration.pushManager.subscribe(subscribeOptions);
。
所以在尝试不同的事情几个小时后,我终于找到了问题所在,而且它简单得令人讨厌。所以每次你调用 navigator.serviceWorker.register('serviceworker.js')
你必须在文件名前加上一个 /
。 service worker 在没有 /
的情况下也能正常注册,但获取订阅会失败。您在文档中找到的示例不包括 /
。
所以,代码应该是这样的:
navigator.serviceWorker.register('/serviceworker.js')
Tada 现在可以了....
我有以下代码注册了一个 service worker 并要求用户允许通知。在用户允许 serviceWorkerRegistration.pushManager.getSubscription()
返回的承诺为 null
的推送通知后,我收到错误消息。当我关闭浏览器并再次强制调用此函数时,它可以正常工作。我不明白为什么。这是我的代码:
window.vapidPublicKey = new Uint8Array([4, 45, ...]);
if (navigator.serviceWorker) {
navigator.serviceWorker.register('/serviceworker.js')
.then(function(reg) {
console.log('Service worker change, registered the service worker');
});
}
// Otherwise, no push notifications :(
else {
console.error('Service worker is not supported in this browser');
}
navigator.serviceWorker.ready.then((serviceWorkerRegistration) => {
console.log("ready");
serviceWorkerRegistration.pushManager
.subscribe({
userVisibleOnly: true,
applicationServerKey: window.vapidPublicKey
});
});
// application.js
// Let's check if the browser supports notifications
if (!("Notification" in window)) {
console.error("This browser does not support notifications.");
}
// Let's check whether notification permissions have already been granted
else if (Notification.permission === "granted") {
console.log("Permission to receive notifications has been granted");
}
// Otherwise, we need to ask the user for permission
else if (Notification.permission !== 'denied') {
Notification.requestPermission(function (permission) {
// If the user accepts, let's create a notification
if (permission === "granted") {
console.log("Permission to receive notifications has been granted");
saveSubscriptionToDatabase();
}
});
}
function saveSubscriptionToDatabase(){
navigator.serviceWorker.ready
.then((serviceWorkerRegistration) => {
console.log(serviceWorkerRegistration.pushManager.getSubscription());
serviceWorkerRegistration.pushManager.getSubscription()
.then((subscription) => {
$.post("/users/save_subscription", { subscription: subscription.toJSON() });
});
});
}
错误:
Uncaught (in promise) TypeError: Cannot read property 'toJSON' of null
at serviceWorkerRegistration.pushManager.getSubscription.then
更新:
所以我添加了以下按钮:
<input type="button" onclick="saveSubscriptionToDatabase();" value="test">
所有 JS 和 post 请求都按预期工作,只要我从按钮单击调用函数但是当我从检查权限状态的条件调用它时,它仍然像以前一样失败.
更新 2:
我试过从不同的角度重新编写代码。
function registerServiceWorker() {
return navigator.serviceWorker.register('serviceworker.js')
.then(function(registration) {
console.log('Service worker successfully registered.');
return registration;
})
.catch(function(err) {
console.error('Unable to register service worker.', err);
});
}
function askPermission() {
return new Promise(function(resolve, reject) {
const permissionResult = Notification.requestPermission(function(result) {
resolve(result);
});
if (permissionResult) {
permissionResult.then(resolve, reject);
}
})
.then(function(permissionResult) {
if (permissionResult !== 'granted') {
throw new Error('We weren\'t granted permission.');
}
});
}
function subscribeUserToPush() {
return navigator.serviceWorker.register('serviceworker.js')
.then(function(registration) {
const subscribeOptions = {
userVisibleOnly: true,
applicationServerKey: new Uint8Array("key goes here")
};
return registration.pushManager.subscribe(subscribeOptions);
})
.then(function(pushSubscription) {
console.log("never called");
console.log('Received PushSubscription: ', JSON.stringify(pushSubscription));
return pushSubscription;
});
}
registerServiceWorker();
askPermission();
subscribeUserToPush();
subscribeUserToPush()
命令链的最后一部分 console.log("never called");
永远不会执行,也没有错误。我正在 Chrome 67 中对此进行测试。我肯定会调用 registration.pushManager.subscribe(subscribeOptions);
。
所以在尝试不同的事情几个小时后,我终于找到了问题所在,而且它简单得令人讨厌。所以每次你调用 navigator.serviceWorker.register('serviceworker.js')
你必须在文件名前加上一个 /
。 service worker 在没有 /
的情况下也能正常注册,但获取订阅会失败。您在文档中找到的示例不包括 /
。
所以,代码应该是这样的:
navigator.serviceWorker.register('/serviceworker.js')
Tada 现在可以了....