push notification
snippet in php

web push notifications example

user1786

Please take a look here
https://firebase.google.com/docs/cloud-messaging/js/client

and my github FREE source code push web notification demo
https://github.com/zidane168/WebPushDemo

push notification

user1122

public function push($device_data, $message, $push_params) {
  $ch = curl_init();

  // Set POST variables
  $this->set_credential(true);
  $url = $this->server_feedback_url;

  $headers = array(
    'Connection: keep-alive',
    'Authorization: key=' . $this->server_key,
    'Content-Type: application/json'
  );

  $fields  = array();

  $failed_case = array();
  $succeed_case = array();

  try {
    $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);

    // device_data
    // Array
    // (
    // 	[0] => arrayfSiW2K4tzpY:APA91bGxwkrBAI2FR3Dt5iAMwfhxLeCdSz62HCzF_4xRT4dcWw61bQxUjV7T0JK4hSKYTP2wG--A_pWGAfXlxH9vO68rR_h0crChNnGR0vnDUkgpe2KzGNsZgICEuYX0xCs5KilX3RhJ
    // 	[1] => fSiW2K4tzpY:APA91bGxwkrBAI2FR3Dt5iAMwfhxLeCdSz62HCzF_4xRT4dcWw61bQxUjV7T0JK4hSKYTP2wG--A_pWGAfXlxH9vO68rR_h0crChNnGR0vnDUkgpe2KzGNsZgICEuYX0xCs5KilX3RhJ
    // )

    // $fields = array(
    // 	// 'to' => $device_data[0]['device_token'],
    // 	'registration_ids' => $device_data,	// $device_data[0]['device_token'],
    // 	'notification' 	=> array(
    // 		'body'		=> $message['notification']['body'],
    // 		'title'		=> $message['notification']['title'],
    // 	),
    // 	'priority' => 'high',
    // 	'data' => isset($push_params['custom_data']) ? $push_params['custom_data'] : '',

    //   );

    $fields = array(
      'registration_ids' => $device_data,		// array
      'notification' 	=> array(
        'body'		=> $message['notification']['body'],
        'title'		=> $message['notification']['title'],
      ),
      'priority' => 'high',
      'data' => isset($push_params['custom_data']) ? $push_params['custom_data'] : '',

    );

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

    $result = curl_exec($ch);

    // {"multicast_id":9114866147884203417,"success":1,"failure":3,"canonical_ids":0,
    //	"results":[{"message_id":"0:1576208394681875%cb43adbbcb43adbb"},{"error":"InvalidRegistration"},
    //				{"error":"NotRegistered"},{"error":"NotRegistered"}]}

    $temp = json_decode($result, true);
    if ($temp['failure'] == 0) {		// dont have fail case
      $succeed_case = $device_data;
      $failed_case = array();

    } else {		// exist fail case
      $index = 0;
      foreach ($temp['results'] as $value) {

        if (isset($value['error'])) {
          $failed_case[] = $device_data[$index];

        } else {
          $succeed_case[] = $device_data[$index];
        }

        $index = $index + 1;
      }
    }

    $pushed = array(
      'status' => true,
      'params' => array(
        'result'		=> $result,
        'succeed' 		=> $succeed_case,
        'failed' 		=> $failed_case,
      ),
    );
  } catch (Exception $e) {
    $pushed = array(
      'status' => false,
      'params' => array(),
      'error_messages' => $e->getMessage(),
    );

  } finally {
    curl_close($ch);	 // Close the connection to the server
  }

  return $pushed;
}

public function set_credential($sandbox = true) {
  if ($sandbox === true) {
    $this->server_key 				= Environment::read('push.server_key');
    $this->sender_id	 			= Environment::read('push.sender_id');
    $this->server_feedback_url 		= Environment::read('push.server_feedback_url');
  }
}