Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

Service worker code: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. 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).

like image 473
venugopal Avatar asked Mar 08 '26 10:03

venugopal


1 Answers

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

}
like image 82
bvakiti Avatar answered Mar 10 '26 00:03

bvakiti



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!