PushManager.getSubscription()MDNの<code>pushManager</code>ドキュメントによると、 `pushManager.subscribe`と` pushManager.getSubscription`サービス労働者
Retrieves an existing push subscription. It returns a Promise that resolves to a PushSubscription object containing details of an existing subscription. If no existing subscription exists, this resolves to a null value.
[...]
PushManager.subscribe()
Subscribes to a push service. It returns a Promise that resolves to a PushSubscription object containing details of a push subscription. A new push subscription is created if the current service worker does not have an existing subscription.
の違いは何ですか。メソッドはほとんど同じですが、getSubcription()
の場合はnull値で解決される可能性があります。
私は基本的にsubscribe()
を使用することができます。サービスワーカーは、利用可能な場合にはサブスクリプションを取得しようとしますが、利用できない場合は新しいサービスを作成しようとします。
=>しかし、私は何か他のことをやろうとしていました。私はnull
で解決したら、まずサブスクリプションを取得しようとします。サブスクライブしようとします。
navigator.serviceWorker.register('./worker.js')
.then(function(reg) {
// Subscribe push manager
reg.pushManager.getSubscription()
.then(function(subscription) {
if(subscription){
// TODO... get the enpoint here
} else {
reg.pushManager.subscribe()
.then(function(sub){
// TODO... get the endpoint here
});
}
}, function(error) {
console.error(error);
})
});
しかし、私はエラーになってしまっています:
Uncaught (in promise) DOMException: Subscription failed - no active Service Worker
それは混乱して、そして私はこれを疑うのですがサービス労働者やバグおそらく缶のプッシュAPI上のクロムの制限です。この奇妙な振る舞いに関する情報は誰にもありますか?
もう一度おねがいします! :) –