javascript - Desktop push notification using Google cloud messaging and service worker -


i want send push notification desktop users using google cloud messaging

i completed following steps successfully

  • initialised in service worker
  • created project on google developers console
  • added manifest
  • sent using php curl

here curl commands

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

everything working fine , able display default notification using following code

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

but need display notification message sent through curl command (in app can easily)

i got following code receiving push notification (google)

self.addeventlistener('push', function(event) {     // since there no payload data first version     // of push messages, we'll grab data     // api , use populate notification     event.waituntil(       fetch(some_api_endpoint).then(function(response) {         if (response.status !== 200) {           // either show message user explaining error           // or enter generic message , handle         // onnotificationclick event direct user web page           console.log('looks there problem. status code: ' + response.status);           throw new error();         }        // examine text in response         return response.json().then(function(data) {           if (data.error || !data.notification) {             console.error('the api returned 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 retrieve data', err);        var title = 'an error occurred';       var message = 'we unable information push message';         var icon = url_to_default_icon;         var notificationtag = 'notification-error';         return self.registration.shownotification(title, {             body: message,             icon: icon,             tag: notificationtag           });       })     );   }); 

it displays

we unable information push message

what some_api_endpoint mentioned in code ?
tried https://android.googleapis.com/gcm/send instead of endpoint , user endpoints in service worker not working .

any appreciated

as far know, some_api_endpoint mentioned api backend used simplify client access data other applications.

as discussed in overview of cloud endpoints,

google cloud endpoints consists of tools, libraries , capabilities allow generate apis , client libraries app engine application, referred api backend, simplify client access data other applications. endpoints makes easier create web backend web clients , mobile clients such android or apple's ios.

more information on use of endpoints discussed in best practices google cloud endpoints such as, how create app engine backend.

you may use post - google endpoints , google cloud messaging 1 of references can help.


Comments

Popular posts from this blog

Failed to execute goal org.apache.maven.plugins:maven-surefire-plugin:2.12:test (default-test) on project.Error occurred in starting fork -

windows - Debug iNetMgr.exe unhandle exception System.Management.Automation.CmdletInvocationException -

configurationsection - activeMq-5.13.3 setup configurations for wildfly 10.0.0 -