[英]Service Worker only showing first push notification (from cloud messaging) until I reload - message IS received by the worker
[英]Desktop push notification using Google cloud messaging and service worker
我想使用Google云消息向所有桌面用户发送推送通知
我成功完成了以下步骤
这是我的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 Endpoints包含工具,库和功能,允许您从App Engine应用程序(称为API后端)生成API和客户端库,以简化客户端对其他应用程序数据的访问。 端点可以更轻松地为Web客户端和移动客户端(如Android或Apple的iOS)创建Web后端。
有关端点使用的更多信息,请参阅Google Cloud Endpoints的最佳实践,例如,如何创建App Engine后端。
您也可以使用此SO帖子 - Google端点和Google云消息传递作为您可以提供帮助的参考资料之一。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.