· 5 years ago · Jun 17, 2020, 08:16 AM
1public function notif()
2 {
3
4 $device_id = '27xxxxxx327';
5 $message = array('message' => 'ciok', 'title' => 'vdxvxvd');
6
7 //API URL of FCM
8 $url = 'https://fcm.googleapis.com/fcm/send';
9
10 /*api_key available in:
11 Firebase Console -> Project Settings -> CLOUD MESSAGING -> Server key*/
12 $api_key = 'AAAAP5zwem8:APA91bHb........................jBQ_4';
13
14 $fields = array (
15 'registration_ids' => array (
16 $device_id
17 ),
18 'data' => array (
19 "notification" => $message
20 )
21 );
22
23
24
25 //header includes Content type and api key
26 $headers = array(
27 'Content-Type:application/json',
28 'Authorization:key='.$api_key
29 );
30
31 $ch = curl_init();
32 curl_setopt($ch, CURLOPT_URL, $url);
33 curl_setopt($ch, CURLOPT_POST, true);
34 curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
35 curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
36 curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
37 curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
38 curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fields));
39 $result = curl_exec($ch);
40 if ($result === FALSE) {
41 die('FCM Send Error: ' . curl_error($ch));
42 }
43 curl_close($ch);
44 return $result;
45
46 }