我是服务人员和 GAE 的新手,我可以注册服务人员但无法订阅 PushManager,出现订阅空错误。找到以下代码以供参考。
serviceWorkerRegistration.pushManager.getSubscription()
.then(function(subscription) {
var pushButton = document.querySelector('.js-push-button');
pushButton.disabled = false;
if (!subscription) {
console.log('subscription error ');
return;
}
console.log('subscriptioned ');
// Keep your server in sync with the latest subscriptionId
sendSubscriptionToServer(subscription);
// Set your UI to show they have subscribed for
// push messages
pushButton.textContent = 'Disable Push Messages';
isPushEnabled = true;
})
.catch(function(err) {
console.warn('Error during getSubscription()', err);
});
});
在上面的代码中,然后将“订阅”值设为“null”,这样,控制就进入了 if 块并简单地返回。