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