0

i'm sending notification to who visited my website and accepted for sending desktop notifications to them. I'm taking theirs registration id and making request to gcm api.

BUT

I can not send Notificaiton title,message and icon.

Here is my PHP class taked from GCM with PHP (Google Cloud Messaging)

class GCMPushMessage {
    var $url = 'https://android.googleapis.com/gcm/send';
    var $serverApiKey = "";
    var $devices = array();

    /*
        Constructor
        @param $apiKeyIn the server API key
    */
    function GCMPushMessage($apiKeyIn){
        $this->serverApiKey = $apiKeyIn;
    }
    /*
        Set the devices to send to
        @param $deviceIds array of device tokens to send to
    */
    function setDevices($deviceIds){

        if(is_array($deviceIds)){
            $this->devices = $deviceIds;
        } else {
            $this->devices = array($deviceIds);
        }

    }
    /*
        Send the message to the device
        @param $message The message to send
        @param $data Array of data to accompany the message
    */
    function send($message, $data = false){

        if(!is_array($this->devices) || count($this->devices) == 0){
            $this->error("No devices set");
        }

        if(strlen($this->serverApiKey) < 8){
            $this->error("Server API Key not set");
        }

        $fields = array(
            'registration_ids'  => $this->devices,
            'notification'              => array( "message" => $message ),
        );

        if(is_array($data)){
            foreach ($data as $key => $value) {
                $fields['data'][$key] = $value;
            }
        }
        $headers = array( 
            'Authorization: key=' . $this->serverApiKey,
            'Content-Type: application/json'
        );
        // Open connection
        $ch = curl_init();

        // Set the url, number of POST vars, POST data
        curl_setopt( $ch, CURLOPT_URL, $this->url );

        curl_setopt( $ch, CURLOPT_POST, true );
        curl_setopt( $ch, CURLOPT_HTTPHEADER, $headers);
        curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );

        curl_setopt( $ch, CURLOPT_POSTFIELDS, json_encode( $fields ) );

        // Avoids problem with https certificate
        curl_setopt( $ch, CURLOPT_SSL_VERIFYHOST, false);
        curl_setopt( $ch, CURLOPT_SSL_VERIFYPEER, false);

        // Execute post
        $result = curl_exec($ch);

        // Close connection
        curl_close($ch);

        return $result;
    }

    function error($msg){
        echo "Android send notification failed with error:";
        echo "\t" . $msg;
        exit(1);
    }
}

Here is my notification.php file

$apiKey = "MY_API_KEY";
$devices = array('eBLwIaqdgOs:APA91bG-6Ry1gN1wg2jI3WOpgpQg2bxE1NxgJCBXxzE188GF46JuPO1DkWw-OiKswRr2y0K6xX8k4RKGOHqnub0zgoqgX22I1TDxOAWHrNoTn9XoRMWywARrWAq1zD78fAz3H9VcnqSW');
$message = "The message to send";
$gcpm = new GCMPushMessage($apiKey);
$gcpm->setDevices($devices);
$response = $gcpm->send($message, array('title' => 'Test title'));

echo $response;

And here is my sw.js file (service worker)

'use strict';
console.log('Started', self);

self.addEventListener('install', function(event) {
  self.skipWaiting();
  console.log('Installed', event);
});

self.addEventListener('activate', function(event) {
  console.log('Activated', event);
});

// TODO


function showNotification(title, body, icon, data) {
  console.log('showNotification');

  var notificationOptions = {
    body: body,
    icon: icon ? icon : '/images/touch/chrome-touch-icon-192x192.png',
    tag: 'simple-push-demo-notification',
    data: data
  };
  return self.registration.showNotification(title, notificationOptions);
}

self.addEventListener('push', function(event) {
  console.log('Received a push message', event);
  if (event.data) {
    console.log('message data', event.data);
    console.log('message data', event.data.text);
    var output = event.data.text();
    console.log(output);
  }
  showNotification("My Title","My Message");
});

When i requsted to my notification.php file a notification appears to me or which registration id i wrote with the title My Title and message My Message

As you can see thesee datas are coming from sw.js but i wont to send them via php because they are dynamic variables i'll get them from user input.

Help me with that please. Thank you.

Community
  • 1
  • 1
Nezih
  • 333
  • 3
  • 16
  • Possible duplicate of [Chrome Desktop Notification doesn't display the message](http://stackoverflow.com/questions/35644593/chrome-desktop-notification-doesnt-display-the-message) – Marco Castelluccio Mar 16 '16 at 16:58
  • I'm having trouble following this question. Are you able to get notifications under any circumstances? e.g. if you send them with `curl`? See https://developers.google.com/web/updates/2015/03/push-notifications-on-the-open-web?hl=en – mjs Mar 18 '16 at 16:32
  • Solved by my self. Thanks anyway. If you have same problem or different check out here: http://mobiforge.com/design-development/web-push-notifications – Nezih Mar 19 '16 at 19:53

0 Answers0