· 6 years ago · Mar 23, 2020, 09:08 AM
1function _send_push_notification($notification,$device_tokens){
2 if(!empty($device_tokens)){
3 #API access key from Google API's Console
4 //$api_access_key = 'AIzaSyBVP5p7WgxkiHtrlvd8YWD_qtwQwWM9DKU';
5 $api_access_key = 'AIzaSyA5VOM06vPNPizfcXctO92pnizV8oZBbL0';
6
7 // $singleID = $notification['device_token'];
8 //$singleID = 'fXZRWsNz_FI:APA91bEDT1soTOEW3KszjNUgOT-tmngjkW2KQSycCob9dw3yvBrm_4-zcStiOlwrVSNYt2vZSiYEWHGCa34SLj9KlOxFW3jmXnLQfdf7u2e0OOnk9Gqg9JTtXHfrJtNTKCYylssTNG1tEtPXiP7CBSDmMqtjvfblBw';
9 // $registrationIds = $notification['device_token'];
10
11 #prep the bundle
12 $msg = array(
13 'body' => $notification['content'],
14 'title' => $notification['title'],
15 'icon' => 'https://web.ku.edu.np/images/icon-ku.png',/*Default Icon*/
16 'sound' => 'mySound',/*Default sound*/
17 );
18
19 $fields = array(
20 'registration_ids' => $device_tokens,
21 // 'to' => $singleID,
22 'priority' => 'high',
23 'notification' => $msg,
24 'data' => [
25 "user_id"=>$notification['user_id'],
26 "date"=>date('Y-m-d'),
27 'screen'=> $notification['screen'],
28 'type_id' => $notification['notification_type_id'],
29 'page'=>$notification['notification_type']
30 ]
31 );
32
33 $headers = array(
34 'Authorization: key=' . $api_access_key,
35 'Content-Type: application/json'
36 );
37
38 #Send Reponse To FireBase Server
39 $ch = curl_init();
40 curl_setopt( $ch,CURLOPT_URL, 'https://fcm.googleapis.com/fcm/send' );
41 curl_setopt( $ch,CURLOPT_POST, true );
42 curl_setopt( $ch,CURLOPT_HTTPHEADER, $headers );
43 curl_setopt( $ch,CURLOPT_RETURNTRANSFER, true );
44 curl_setopt( $ch,CURLOPT_SSL_VERIFYPEER, false );
45 curl_setopt( $ch,CURLOPT_POSTFIELDS, json_encode( $fields ) );
46 $result = curl_exec($ch );
47 if ($result === FALSE)
48 {
49 die('FCM Send Error: ' . curl_error($ch));
50 }
51 curl_close( $ch );
52 #Echo Result Of FireBase Server
53 // echo $result;
54 //$result = json_decode($result);
55 //print_r($result);
56 }
57 }
58
59 function _send_push_notification_website($notification,$device_tokens)
60 {
61 if(!empty($device_tokens)){
62
63 // $api_access_key = 'AIzaSyBdBtpNSB71y4C58SBuME7m1ra4VrZzSnw';
64 $api_access_key = 'AIzaSyDs6EjA0Cjyb_UUDZaGRnup54CuFpb8AQI';
65
66 #prep the bundle
67 $msg = array(
68 'body' => $notification['content'],
69 'title' => $notification['title'],
70 'icon' => 'https://web.ku.edu.np/images/icon-ku.png',/*Default Icon*/
71 'sound' => 'mySound',/*Default sound*/
72 'click_action' => $notification['screen'],
73 );
74
75 $fields = array(
76 'registration_ids' => $device_tokens,
77 // 'to' => $singleID,
78 'priority' => 'high',
79 'notification' => $msg,
80 'data' => [
81 "user_id"=>$notification['user_id'],
82 "date"=>date('Y-m-d'),
83 'screen'=> $notification['screen'],
84 'type_id' => $notification['notification_type_id'],
85 'page'=>$notification['notification_type']
86 ]
87 );
88
89 $headers = array(
90 'Authorization: key=' . $api_access_key,
91 'Content-Type: application/json'
92 );
93
94 #Send Reponse To FireBase Server
95 $ch = curl_init();
96 curl_setopt( $ch,CURLOPT_URL, 'https://fcm.googleapis.com/fcm/send' );
97 curl_setopt( $ch,CURLOPT_POST, true );
98 curl_setopt( $ch,CURLOPT_HTTPHEADER, $headers );
99 curl_setopt( $ch,CURLOPT_RETURNTRANSFER, true );
100 curl_setopt( $ch,CURLOPT_SSL_VERIFYPEER, false );
101 curl_setopt( $ch,CURLOPT_POSTFIELDS, json_encode( $fields ) );
102 $result = curl_exec($ch );
103 if ($result === FALSE)
104 {
105 die('FCM Send Error: ' . curl_error($ch));
106 }
107 curl_close( $ch );
108 #Echo Result Of FireBase Server
109 // echo $result;
110 //$result = json_decode($result);
111 //print_r($result);
112 }
113 }