简体   繁体   English

使用推送事件的数据推送通知

[英]Push Notifications with data on push Event

I am trying to push notifications using GCM,able to show notifications with hard coded data or message on Push Event.But when I tried to push custom messages using data object as Json,Push event is triggering but ,data object always shoeing as null. 我正在尝试使用GCM推送通知,能够在Push Event上显示带有硬编码数据或消息的通知。但是当我尝试使用数据对象作为Json推送自定义消息时,Push事件正在触发但是,数据对象始终为空。

Service worker code:sw.js 服务工作者代码:sw.js

 self.addEventListener('push', function(event) {
 console.log('Received a push message', event);
 console.log("event data",event.data);//here data is giving as null
var data=event.data.json();
var title= data.title||'No tiltle';
 var body= data.message||'no messgae';

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


  })
  );
  });

am using third party site(requestmaker.com) to check the notifications. 我正在使用第三方网站(requestmaker.com)来检查通知。 format am sending like below 格式我发送如下

 URL:https://android.googleapis.com/gcm/send
 content-type:application/json
 authorization:key=XXXXXXXXXX

below is data format 以下是数据格式

 {
 "registration_ids":["someid"],
    "data":{"title":"hai",
    "body":"hello new message"
   }
 }

How can i get data in Push Event and in which format i have to send data as request sothat i can get that data on push event(event.data). 如何在Push事件中获取数据以及我必须以哪种格式发送数据作为请求,以便我可以在推送事件(event.data)上获取该数据。

You can access the data on notification click trigger event. 您可以访问有关通知单击触发器事件的数据。

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

 // here data you access from event using event.notification.data
  console.log('On notification click: ', event.notification.tag);

}

From the push notification triggered, the payload data wont be sent across to the service worker. 从触发的推送通知中,有效载荷数据不会被发送到服务工作者。 You will have to use browser auth keys while triggering the push notification for the payload to be sent across. 您必须使用浏览器身份验证密钥,同时触发要发送的有效负载的推送通知。

Please refer the documentation below for details. 有关详细信息,请参阅以下文档。 https://developers.google.com/web/updates/2016/03/web-push-encryption?hl=en https://developers.google.com/web/updates/2016/03/web-push-encryption?hl=en

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

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