· 6 years ago · Dec 09, 2019, 08:16 AM
1<?php
2
3$json_data = array(
4 'to' => 'dRbCRkw774Y:APA91bEXSfFnm-1bJzgECCGEK9ImhS40ZjG7wzyTA2e4pUKxtCx_lW5B2oriTCwqQfB1-TJaGWs3lN-OpCuAErrUW3szyFLgasCZfAvGCJ_y_BEWDWcw4PfvRLv9sHdk8qPk2Oa3Mht8',
5 'notification' => array(
6 'body' => 'something',
7 'title' => 'something',
8 ),
9 'data' => null,
10);
11
12// print(json_encode($json_data));
13
14$data = json_encode($json_data);
15//FCM API end-point
16$url = 'https://fcm.googleapis.com/fcm/send';
17
18//api_key in Firebase Console -> Project Settings -> CLOUD MESSAGING -> Server key
19$server_key = 'AAAAQGz59Vw:APA91bE-arA2TUKPFw3Lsk2_PaesNhoWAZfpmFxVfijUrpS3d6OmuBKBZZfV-JhGDFQNEjN2_4VxG8qbgQXBBD8HviFEqyh4FUTWp-0EiqI3uhh7unk4jISYMSSq77ndG8uB3bAPxO0c';
20//header with content_type api key
21$headers = array(
22 'Content-Type:application/json',
23 'Authorization:key=' . $server_key
24);
25//CURL request to route notification to FCM connection server (provided by Google)
26$ch = curl_init();
27curl_setopt($ch, CURLOPT_URL, $url);
28curl_setopt($ch, CURLOPT_POST, true);
29curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
30curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
31curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
32curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
33curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
34$result = curl_exec($ch);
35if ($result === FALSE) {
36 die('Oops! FCM Send Error: ' . curl_error($ch));
37}
38curl_close($ch);