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