小编Lin*_*ham的帖子

`pushManager.subscribe`和`pushManager.getSubscription` Service Worker之间有什么区别?

PushManager.getSubscription()

检索现有推送订阅.它返回一个Promise,它解析为包含现有订阅详细信息的PushSubscription对象.如果不存在现有订阅,则解析为空值.

[...]

PushManager.subscribe()

订阅推送服务.它返回一个Promise,它解析为一个PushSubscription对象,其中包含推送订阅的详细信息.如果当前服务工作者没有现有订阅,则会创建新的推送订阅.

根据MDN的pushManager文档.除了getSubcription()可能使用空值解析的点之外,方法几乎相同.

我基本上明白我可以简单地使用subscribe(),Service Worker将尝试获取订阅以备不时使用,并且如果不可用则创建新订阅.

=>但我正在尝试做别的事情.我想先尝试订阅,如果解决了,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);
        })
    });
Run Code Online (Sandbox Code Playgroud)

但后来我最终得到了错误:

未捕获(在承诺中)DOMException:订阅失败 - 没有活动的服务工作者

这很令人困惑,我怀疑这是Chrome服务工作者的推送API的限制,或者可能是一个错误.有没有人有关于这种奇怪行为的任何信息?

google-chrome service-worker push-api progressive-web-apps

7
推荐指数
1
解决办法
1723
查看次数

Web推送未知问题,WebPushError“收到意外的响应代码” 400

在使用GCM和chrome 进行网络推送的过程中。我最近遇到了这个问题(几个小时前,一切仍然正常)

我不确定是否由于GCM的某些更改或云的停机而发生这种情况...任何信息将不胜感激。

我的密码

var payload = JSON.stringify({
    title: 'notification title',
    body: 'message body'
});
webPush.sendNotification(params.endpoint, {
    userPublicKey: params.userPublicKey,
    userAuth: params.userAuth,
    payload: payload
}).then(function (res) { 
    // console.log of stuff
})
.catch(function(error){
    console.log(error);
    process.exit(1);
});
Run Code Online (Sandbox Code Playgroud)

跟踪:

{
  WebPushError: Received unexpected response code
  at IncomingMessage.<anonymous> (/var/www/***/pwa/node_modules/web-push /index.js:264:20)
  at emitNone (events.js:91:20)
  at IncomingMessage.emit (events.js:185:7)
  at endReadableNT (_stream_readable.js:926:12)
  at _combinedTickCallback (internal/process/next_tick.js:74:11)
  at process._tickCallback (internal/process/next_tick.js:98:9)
  name: 'WebPushError',
  message: 'Received unexpected response code',
  statusCode: 400,
  headers: {
    'content-type': 'text/plain; charset=UTF-8',
     date: 'Wed, 04 …
Run Code Online (Sandbox Code Playgroud)

google-chrome node.js google-cloud-messaging service-worker web-push

5
推荐指数
1
解决办法
1911
查看次数