· 6 years ago · Dec 05, 2019, 07:40 PM
1function sendToFireBase($title, $body, $type = '', $id = -1)
2{
3 // API access key from Google API's Console
4 define('API_ACCESS_KEY', 'AAAAuxEVVQY:APA91bHbeWrYEH5AXkmqDKWi5XS6WlmpzzNh21Qc-1UXnsSZ78cGsr0CWGHeLheVTNSNWqTyHj8lEy4hKeCx2q97Ra2k_IYOt9j_eeAyz8hDJR5nLPOIiCmHBFstyrpL27ynpaXevCP5');
5
6 $msg = array(
7 'body' => $body,
8 'title' => $title,
9 'vibrate' => 1,
10 'sound' => 1,
11 );
12
13 $data = ['type' => $type, 'id' => $id];
14
15 $fields = array(
16 'to' => '/topics/porscheMessages',
17 'notification' => $msg,
18 'data' => $data
19 );
20
21 $headers = array(
22 'Authorization: key=' . API_ACCESS_KEY,
23 'Content-Type: application/json'
24 );
25
26 $ch = curl_init();
27 curl_setopt($ch, CURLOPT_URL, 'https://fcm.googleapis.com/fcm/send');
28 curl_setopt($ch, CURLOPT_POST, true);
29 curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
30 curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
31 curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
32 curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fields));
33 $result = curl_exec($ch);
34 $error = curl_error($ch);
35 curl_close($ch);
36 if($error){
37 error_log($error);
38 }
39 else{
40 return true;
41 }
42
43}