使用 Javascript 将数据/有效负载发送到 Google Chrome 推送通知 [英] Sending data / payload to the Google Chrome Push Notification with Javascript

查看:33
本文介绍了使用 Javascript 将数据/有效负载发送到 Google Chrome 推送通知的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在处理 Google Chrome 推送通知,我正在尝试将有效负载发送给 google chrome 工作人员,但是,我不知道我是如何接收此有效负载的.

I'm working on the Google Chrome Push Notification and I'm trying to send the payload to the google chrome worker but, I have no idea how I receive this payload.

我有一个 API 可以在我的数据库中创建和保存通知,我需要通过 https://android.googleapis.com/gcm/send 发送值并在工作人员上接收.js

I have an API to create and save the notifications in my database and I need send the values through the https://android.googleapis.com/gcm/send and receive on the worker.js

这是我的 worker.js

This is my worker.js

    self.addEventListener('push', function(event) {
      var title = 'Yay a message.';
      var body = 'We have received a push message.';
      var icon = '/images/icon-192x192.png';
      var tag = 'simple-push-demo-notification-tag';

      event.waitUntil(
        self.registration.showNotification(title, {
          body: body,
          icon: icon,
          tag: tag
        })
      );
    });

这就是我调用 GCM 的方式

And this is how I'm calling the GCM

curl --header "Authorization: key=AIzaSyDQjYDxeS9MM0LcJm3oR6B7MU7Ad2x2Vqc" --header  "Content-Type: application/json" https://android.googleapis.com/gcm/send -d "{ "data":{"foo":"bar"}, "registration_ids":["APA91bGqJpCmyCnSHLjY6STaBQEumz3eFY9r-2CHTtbsUMzBttq0crU3nEXzzU9TxNpsYeFmjA27urSaszKtA0WWC3yez1hhneLjbwJqlRdc_Yj1EiqLHluVwHB6V4FNdXdKb_gc_-7rbkYkypI3MtHpEaJbWsj6M5Pgs4nKqQ2R-WNho82mnRU"]}"

我试图获取 event.data 但是,这是未定义的.

I tried to get event.data but, this is undefined.

有没有人有任何想法或建议?

Does anyone have any idea or sugestion?

推荐答案

要检索该数据,您需要将event.data.text()"解析为 JSON 对象.我猜自从你试图让它工作以来,有些东西已经更新了,但现在它可以工作了.倒霉!

To retrieve that data, you need to parse "event.data.text()" to a JSON object. I'm guessing something was updated since you tried to get this to work, but it works now. Unlucky!

然而,由于我是在自己寻找解决方案时看到这篇文章的,其他人可能会想要一个有效的答案.这是:

However, since I made it to this post when searching for a solution myself, others would probably like a working answer. Here it is:

// Push message event handler
self.addEventListener('push', function(event) {

  // If true, the event holds data
  if(event.data){

    // Need to parse to JSON format
    // - Consider event.data.text() the "stringify()"
    //   version of the data
    var payload = JSON.parse(event.data.text());
    // For those of you who love logging
    console.log(payload); 

    var title = payload.data.title;
    var body  = payload.data.body;
    var icon  = './assets/icons/icon.ico'
    var tag   = 'notification-tag';

    // Wait until payload is fetched
    event.waitUntil(
      self.registration.showNotification(title, {
        body: body,
        icon: icon,
        tag: tag,
        data: {} // Keeping this here in case I need it later
      })
    );

  } else {
    console.log("Event does not have data...");
  }

}); // End push listener

// Notification Click event
self.addEventListener('notificationclick', function(event) {
  console.log("Notification Clicked");
}); // End click listener

就我个人而言,我将创建一个通用"通知,以防我的数据有问题,并且还将使用 try/catch.我建议也这样做.

Personally, I will be creating a "generic" notification in case my data is funky, and will also be using try/catch. I suggest doing the same.

这篇关于使用 Javascript 将数据/有效负载发送到 Google Chrome 推送通知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆