我正在关注这个非常清晰的教程。
一切正常,除了当我到达使用 pushManager 订阅用户的阶段时,当我打电话时,pushManager.subscribe()
我没有从状态为pending
.
也许我的设置遗漏了一些明显的问题,如下所示:
if ('serviceWorker' in navigator && 'PushManager' in window) {
console.log('Service Worker and Push is supported');
navigator.serviceWorker.register('service-worker.js')
.then(function(swReg) {
console.log('Service worker successfully registered.');
navigator.serviceWorker.ready.then(function() {
const subscribeOptions = {
userVisibleOnly: true,
applicationServerKey: ... // My application server key.
};
let promise = swReg.pushManager.subscribe(subscribeOptions)
console.log(promise) // Promise {[[PromiseStatus]]: "pending", [[PromiseValue]]: undefined}
promise
.then(function(pushSubscription) {
console.log('Received PushSubscription: ', pushSubscription); // Never gets called.
})
.catch(function(err) {
console.error('Unable to subscribe.', err); // Never gets called.
});
})
})
.catch(function(err) {
console.error('Unable to register service worker.', err);
});
}
是否应该在其他地方进行额外的配置?任何建议表示赞赏。