1Please take a look here
2https://firebase.google.com/docs/cloud-messaging/js/client
3
4and my github FREE source code push web notification demo
5https://github.com/zidane168/WebPushDemo
1public function push($device_data, $message, $push_params) {
2 $ch = curl_init();
3
4 // Set POST variables
5 $this->set_credential(true);
6 $url = $this->server_feedback_url;
7
8 $headers = array(
9 'Connection: keep-alive',
10 'Authorization: key=' . $this->server_key,
11 'Content-Type: application/json'
12 );
13
14 $fields = array();
15
16 $failed_case = array();
17 $succeed_case = array();
18
19 try {
20 $ch = curl_init();
21 curl_setopt($ch, CURLOPT_URL, $url);
22 curl_setopt($ch, CURLOPT_POST, true);
23 curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
24 curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
25 curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
26
27 // device_data
28 // Array
29 // (
30 // [0] => arrayfSiW2K4tzpY:APA91bGxwkrBAI2FR3Dt5iAMwfhxLeCdSz62HCzF_4xRT4dcWw61bQxUjV7T0JK4hSKYTP2wG--A_pWGAfXlxH9vO68rR_h0crChNnGR0vnDUkgpe2KzGNsZgICEuYX0xCs5KilX3RhJ
31 // [1] => fSiW2K4tzpY:APA91bGxwkrBAI2FR3Dt5iAMwfhxLeCdSz62HCzF_4xRT4dcWw61bQxUjV7T0JK4hSKYTP2wG--A_pWGAfXlxH9vO68rR_h0crChNnGR0vnDUkgpe2KzGNsZgICEuYX0xCs5KilX3RhJ
32 // )
33
34 // $fields = array(
35 // // 'to' => $device_data[0]['device_token'],
36 // 'registration_ids' => $device_data, // $device_data[0]['device_token'],
37 // 'notification' => array(
38 // 'body' => $message['notification']['body'],
39 // 'title' => $message['notification']['title'],
40 // ),
41 // 'priority' => 'high',
42 // 'data' => isset($push_params['custom_data']) ? $push_params['custom_data'] : '',
43
44 // );
45
46 $fields = array(
47 'registration_ids' => $device_data, // array
48 'notification' => array(
49 'body' => $message['notification']['body'],
50 'title' => $message['notification']['title'],
51 ),
52 'priority' => 'high',
53 'data' => isset($push_params['custom_data']) ? $push_params['custom_data'] : '',
54
55 );
56
57 curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fields));
58
59 $result = curl_exec($ch);
60
61 // {"multicast_id":9114866147884203417,"success":1,"failure":3,"canonical_ids":0,
62 // "results":[{"message_id":"0:1576208394681875%cb43adbbcb43adbb"},{"error":"InvalidRegistration"},
63 // {"error":"NotRegistered"},{"error":"NotRegistered"}]}
64
65 $temp = json_decode($result, true);
66 if ($temp['failure'] == 0) { // dont have fail case
67 $succeed_case = $device_data;
68 $failed_case = array();
69
70 } else { // exist fail case
71 $index = 0;
72 foreach ($temp['results'] as $value) {
73
74 if (isset($value['error'])) {
75 $failed_case[] = $device_data[$index];
76
77 } else {
78 $succeed_case[] = $device_data[$index];
79 }
80
81 $index = $index + 1;
82 }
83 }
84
85 $pushed = array(
86 'status' => true,
87 'params' => array(
88 'result' => $result,
89 'succeed' => $succeed_case,
90 'failed' => $failed_case,
91 ),
92 );
93 } catch (Exception $e) {
94 $pushed = array(
95 'status' => false,
96 'params' => array(),
97 'error_messages' => $e->getMessage(),
98 );
99
100 } finally {
101 curl_close($ch); // Close the connection to the server
102 }
103
104 return $pushed;
105}
106
107public function set_credential($sandbox = true) {
108 if ($sandbox === true) {
109 $this->server_key = Environment::read('push.server_key');
110 $this->sender_id = Environment::read('push.sender_id');
111 $this->server_feedback_url = Environment::read('push.server_feedback_url');
112 }
113}
114