使用 Google Cloud 消息传递和服务辅助角色的桌面推送通知


Desktop push notification using Google cloud messaging and service worker

我想使用 google cloud 消息向所有桌面用户发送推送通知

我成功完成了以下步骤

  • 在服务工作线程中初始化
  • 在 Google 开发者控制台上创建了一个项目
  • 添加了清单
  • 使用 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命令发送的通知消息(在应用程序中我们可以轻松做到这一点)

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

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  
        });  
    })  
  );  
});

它始终显示

我们无法获取此推送消息的信息

该代码中提到的SOME_API_ENDPOINT是什么?
我尝试使用 https://android.googleapis.com/gcm/send 而不是端点,也尝试使用服务工作者中的用户端点,但不起作用.

任何帮助都非常感谢

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

如云终结点概述中所述,

Google Cloud Endpoint 由工具、库和功能组成,可让您通过 App Engine 应用(称为 API 后端)生成 API 和客户端库,从而简化客户端对其他应用数据的访问权限。端点可以更轻松地为 Web 客户端和移动客户端(如 Android 或 Apple 的 iOS)创建 Web 后端。

有关端点使用的更多信息,请参阅

Google Cloud 端点的最佳做法,例如如何创建 App Engine 后端。

您也可以使用此SO帖子 - Google端点和Google云消息传递作为您的参考之一,可以提供帮助。