0

I created a Push messages program, it use firebase.google.com. If a send push message via javascript the message is perfect, but if I send push message with Php curl the message do not same what I wrote in php file. If I send via Php curl, the message will be what I wrote in service-worker.js. Why is it?

Php code

function send_push_message($subscription){
if (empty($subscription)) return FALSE;
$ch = curl_init();
  switch ($subscription["browser"]){
      case "firefox":
          curl_setopt($ch, CURLOPT_URL, "https://updates.push.services.mozilla.com/push/".$subscription["id"] );
          curl_setopt($ch, CURLOPT_PUT, TRUE);
          curl_setopt($ch, CURLOPT_HTTPHEADER, array( "TTL: 86400" ) );
          curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
          curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
      break;

      case "chrome":

      /**
        * https://console.firebase.google.com
        * http://stackoverflow.com/questions/37427709/firebase-messaging-where-to-get-server-key
        */
          // API access key from Google API's Console
          define( 'API_ACCESS_KEY', '<API_SZERVER_KULCSOM>' );

            $registrationIds = array($_GET["id"]);
          // prep the bundle
          $msg = array
          (
              'message'   => 'My text',
              'title'     => 'This is a title. title',
              'subtitle'   => 'This is a subtitle. subtitle',
              'tickerText'   => 'Ticker text here...Ticker text here...Ticker text here',
              'vibrate'   => 1,
              'sound'     => 1
          );
          $fields = array
          (
              'registration_ids'   => $registrationIds,
              'data'       => $msg
          );

          $headers = array
          (
              'Authorization: key=' . API_ACCESS_KEY,
              'Content-Type: application/json'
          );

          $ch = curl_init();
          curl_setopt($ch, CURLOPT_URL, "https://gcm-http.googleapis.com/gcm/send" );
          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));
          $response = curl_exec($ch);
          echo $response;
          if($response === false){
              echo 'Hiba: '.curl_error($ch);
          }else{
              echo 'Siker';
          }
          curl_close($ch);
      break;  
  }
}
$tomb["browser"] = "chrome";
$tomb["id"] = $_GET["id"];
if(isset($_GET["id"])){
  send_push_message($tomb);
}

service-worker.js

self.addEventListener('push', function(event) {
var title = 'xxxxxx';
var body = 'Szerbusz YYY';
var icon = '/images/icon-192x192.png';
var tag = 'simple-push-demo-notification-tag';
event.waitUntil(
  self.registration.showNotification(title, {
    body: body,
    icon: icon,
    tag: tag
  })
);
});

Thank You

Mezőfi
  • 11
  • 2
  • _"the message will be what I wrote in service-worker.js"_ because your value's are static. JS is not going to automatically replace those values for you. You have to write the code to set body, icon etc from the event data payload – Patrick Evans Mar 04 '17 at 18:10

1 Answers1

0

Thank You, I do not send message when visitor sign up, via javascript. I send message for example 5 days later via Php Curl. Send message will be that what I wrote javascript. When I send message, I use only Php curl 5 days later!

Why is $msg value in Php curl, if don't use that?

Thanks

Mezőfi
  • 11
  • 2