无法订阅PushManager

时间:2016-02-12 05:11:04

标签: google-app-engine web-applications service-worker web-push push-api

我是GAE并且推送API /服务工作者,我正在尝试订阅pushManager但是,pushManager getSubscription方法处理程序返回空引用

navigator.serviceWorker.ready.then(function(serviceWorkerRegistration)
 {      console.log('in side ready ' );
     // Do we already have a push message subscription?  
     serviceWorkerRegistration.pushManager.getSubscription()  
       .then(function(subscription) {  

         // Enable any UI which subscribes / unsubscribes from  
         // push messages.  
         var pushButton = document.querySelector('.js-push-button');  
         pushButton.disabled = false;

         if (!subscription) {  
           // We aren't subscribed to push, so set UI  
           // to allow the user to enable push        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);  
       });     });

在有能力的代码getSubscription()中,然后将订阅值返回为null,这样,如果块和函数存在,则控制到来

如果我想调用

serviceWorkerRegistration.pushManager.subscribe() 

然后我收到以下错误

registration failed - no sender id provided

请在下方找到工作进度网址 https://dtatable-date-filter.googleplex.com/home

1 个答案:

答案 0 :(得分:1)

假设这是在chrome中,您需要在清单文件中包含发件人ID。

您可以通过在Google的开发者控制台中创建项目来获取此类发件人ID。

您可以在以下位置获得一系列详细步骤:

https://developers.google.com/web/updates/2015/03/push-notifications-on-the-open-web?hl=en

从以下部分开始阅读:“在Google Developer Console上制作项目”。

您可以在此处查看具有自己的发件人ID的清单文件的示例: https://johnme-gcm.appspot.com/manifest.json

请注意推送工作所需的两个相关键: “gcm_user_visible_only”:是的, “gcm_sender_id”:“...”< - 创建自己的并用它替换....