javascript - 使用谷歌云消息和服务 worker 的桌面推送通知

标签 javascript php curl push-notification google-cloud-messaging

我想使用谷歌云消息向所有桌面用户发送推送通知

我成功完成了以下步骤

  • 在 service worker 中初始化
  • 在 Google Developers Console 上创建了一个项目
  • 添加 list
  • 使用 php CURL 发送

这是我的 CURL 命令

$url = 'https://android.googleapis.com/gcm/send';
$msg="hi";
$data = array('title'=> $msg,'message'=>'Hello');
$regids= // added regids;
$fields = array('to' => $regids,'data' => $data);
$headers = array( 'Authorization: My auth key','Content-Type: application/json');

$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fields));

print_r(json_encode($fields));
$result=curl_exec($ch);
echo $result; 
if($result==FALSE)
{
    die('Curl Failed');
}
curl_close($ch);

一切正常,我可以使用以下代码显示默认通知

self.addEventListener('push', function(event) {
  console.log('Push message', event);
  var title = 'Push message';
  event.waitUntil(
    self.registration.showNotification(title, {
      body: 'The Message',
      icon: 'images/icon.png',
      tag: 'my-tag'
    }));
});

但我需要的是显示我通过 CURL 命令发送的通知消息(在应用程序中我们可以很容易地做到这一点)

我得到了以下用于接收推送通知的代码 (google)

self.addEventListener('push', function(event) {  
  // Since there is no payload data with the first version  
  // of push messages, we'll grab some data from  
  // an API and use it to populate a notification  
  event.waitUntil(  
    fetch(SOME_API_ENDPOINT).then(function(response) {  
      if (response.status !== 200) {  
        // Either show a message to the user explaining the error  
        // or enter a generic message and handle the
        // onnotificationclick event to direct the user to a web page  
        console.log('Looks like there was a problem. Status Code: ' + response.status);  
        throw new Error();  
      }

      // Examine the text in the response  
      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;  
        var notificationTag = data.notification.tag;

        return self.registration.showNotification(title, {  
          body: message,  
          icon: icon,  
          tag: notificationTag  
        });  
      });  
    }).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 = URL_TO_DEFAULT_ICON;  
      var notificationTag = 'notification-error';  
      return self.registration.showNotification(title, {  
          body: message,  
          icon: icon,  
          tag: notificationTag  
        });  
    })  
  );  
});

一直显示

We were unable to get the information for this push message

该代码中提到的 SOME_API_ENDPOINT 是什么?
我试过 https://android.googleapis.com/gcm/send而不是端点以及服务 worker 中的用户端点但不工作。

非常感谢任何帮助

最佳答案

据我所知,提到的 SOME_API_ENDPOINT 是一个 API 后端,用于简化客户端对来自其他应用程序的数据的访问。

Overview of Cloud Endpoints 中所述,

Google Cloud Endpoints consists of tools, libraries and capabilities that allow you to generate APIs and client libraries from an App Engine application, referred to as an API backend, to simplify client access to data from other applications. Endpoints makes it easier to create a web backend for web clients and mobile clients such as Android or Apple's iOS.

有关端点使用的更多信息在 Best practices for Google Cloud Endpoints 中进行了完整讨论。例如,如何创建 App Engine 后端。

您也可以使用此 SO 帖子 - Google endpoints and Google Cloud Messaging作为您的引用之一,可以提供帮助。

关于javascript - 使用谷歌云消息和服务 worker 的桌面推送通知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38330835/

相关文章:

javascript - 输入键在表单字段之间移动不起作用

javascript - 旋转的 HTML 元素 Angular 的 XY 坐标

javascript - 如何更改水平滚动条上的事件菜单项?

php - sql查询连接三个表并选择数据(如果存在)

php - wordpress csv 文件批量导入帮助

php - Swift - 将字典发送到服务器时格式错误的数据

Node.js 中的 cURL 等效项?

bash - 用curl检查bash中是否存在URL列表

javascript - 使用 jquery 删除未使用的 li 类

php - 创建一个用于数据输入的表,例如html中的access/excel网格,以插入到mysql中