简体   繁体   English

如何链接GCM chrome推送通知和有效载荷数据?

[英]How do you link GCM chrome push notifications and payload data?

Push notifications in Chrome via GCM are driving me crazy. 通过GCM在Chrome中发布的推送通知使我发疯。

I've got everything up and running. 我已经准备好一切并开始运行。 I serve the push using my python server to GCM. 我使用python服务器将推送服务发送到GCM。 A service worker displays the push notification fine. 服务人员会很好地显示推送通知。

To my knowledge, there is NO data passed with push events. 据我所知,没有数据与推送事件一起传递。 Sounds like it's coming soon but not available yet. 听起来好像即将到来,但尚不可用。

So just before the push notification shows, I call my server to get extra data for the push notification. 因此,在显示推送通知之前,我致电服务器以获取推送通知的其他数据。 But I have no information on the push notification to send to my server to match and return relevant data. 但是我没有要发送到服务器以匹配并返回相关数据的推送通知的信息。

Everything I can think of to match a notification and user data is purely speculative. 我能想到的与通知和用户数据相匹配的所有内容纯属推测。 The closest thing I can find is a timestamp object on the PushEvent{} that roughly matches the successful return of the GCM call for each user. 我能找到的最接近的东西是PushEvent {}上的时间戳记对象,它与每个用户的GCM调用成功返回大致匹配。

So how are other people handling custom payload data to display Chrome push notifications? 那么其他人如何处理自定义有效载荷数据以显示Chrome推送通知?

The PushEvent{} does not seem to have any ID associated with it. PushEvent {}似乎没有任何关联的ID。 I know the user that the push is for because I've previously stored that information at the time of registration. 我知道推送的用户,因为我之前在注册时已经存储了该信息。

But once I receive a push, I have no idea of knowing what the push was for. 但是,一旦收到推送,我就不会知道推送的目的。

I would like to avoid: 我想避免:

  • Trying to match based on timestamp (since notifications displays are not guaranteed to be instant). 尝试根据时间戳进行匹配(因为不能保证通知显示是即时的)。
  • Trying to pull the 'latest' data for a user because in my case, there could be several notifications that are sent for different bits of data around the same time. 尝试为用户提取“最新”数据,因为在我的情况下,大约在同一时间可能会针对不同的数据位发送多个通知。

How are other sites like Whatsapp and Facebook linking custom payload data with a seemingly sterile event data as a result of a push notification? 作为推送通知的结果,Whatsapp和Facebook等其他网站如何将自定义有效负载数据与看似不育的事件数据链接起来?

How are you doing it? 你好吗 Any suggestions? 有什么建议么?

Here's what my receiver code looks like: 这是我的接收器代码如下所示:

self.addEventListener('push', function(event) {

    event.waitUntil(
    fetch("https://oapi.co/o?f=getExtraPushData&uid=" + self.userID + "&t=" + self.userToken).then(function(response) {  
      if (response.status !== 200) {  
        console.log('Looks like there was a problem. Status Code: ' + response.status);
        throw new Error();  
      }

      return response.json().then(function(data) {  
        if (data.error || !data.notification) {  
          console.error('The API returned an error.', data.error);  
          throw new Error();  
        }  

        var title = data.notification.title;  
        var message = data.notification.message;  
        var icon = data.notification.icon;  

        return self.registration.showNotification(title, {  
          body: message,  
          icon: icon,  
        });  
      });  
    }).catch(function(err) {  
      console.error('Unable to retrieve data', err);

      var title = 'An error occurred';
      var message = 'We were unable to get the information for this push message';  
      var icon = "https://oapi.co/occurrences_secure/img/stepNotify_1.png";  
      var notificationTag = 'notification-error';  
      return self.registration.showNotification(title, {  
          body: message,  
          icon: icon,  
          tag: notificationTag  
        });  
    })  
);  
});

I understand your problem, and i've been fiddling with the same when i wanted to use chrome notification. 我了解您的问题,当我想使用Chrome通知时,我一直在摆弄同样的问题。 You can use indexedDB to save ObjectStore and retrieve data in webServices. 您可以使用indexedDB保存ObjectStore并在webServices中检索数据。

IndexedDB is accessible to webservices. Web服务可访问IndexedDB。 I am using it to store user information and when the user recieves a push notification i pass the stored access key to identify the user and pass him relevent information. 我正在使用它来存储用户信息,并且当用户收到推送通知时,我会通过存储的访问密钥来识别用户并向其传递相关信息。 Here's matt gaunt's tutorial which says indexed db is accessible to web services: http://www.html5rocks.com/en/tutorials/service-worker/introduction/ 这是matt gaunt的教程,其中说索引db可通过Web服务访问: http : //www.html5rocks.com/zh-CN/tutorials/service-worker/introduction/

Here's a good indexedDB tutorial: http://blog.vanamco.com/indexeddb-fundamentals-plus-a-indexeddb-example-tutorial/ 这是一个很好的indexedDB教程: http ://blog.vanamco.com/indexeddb-fundamentals-plus-a-indexeddb-example-tutorial/

Assuming you are still in the past. 假设您仍然在过去。 That is, sending only a push trigger to your browser with no payload it's now time to move on with time. 也就是说,仅向您的浏览器发送推式触发器而没有有效负载,现在该随时间前进。 You can now send payload in your push events. 现在,您可以在推送事件中发送有效负载。 Since you seem familiar with GCM, it's ok to go with that though there is now the Web Push Protocol which is browser vendor independent. 由于您似乎对GCM熟悉,因此可以接受,尽管现在有了Web Push Protocol,它与浏览器供应商无关。

Briefly, to make that work you need to encrypt your payload with specifications found here , in your server. 简而言之,要进行这项工作,您需要在服务器中使用此处找到的规范对有效负载进行加密。

There is a node by google chrome and PHP implementations for that, that I know of. 我知道有一个由谷歌浏览器和PHP实现的node You may check out the PHP Web Push here . 您可以在此处查看PHP Web Push

In the browser you would need to provide the subscription object now with the p256dh and auth on top of the endpoint as before. 在浏览器中,您现在需要像以前一样在端点顶部提供p256dhauth订阅对象。

You may check this out for more details. 您可以检查出更多细节。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM