Last active
January 7, 2022 11:34
-
-
Save aditagusta/1439eb6c136490b1dd1bd52a3976fcb3 to your computer and use it in GitHub Desktop.
Notifikasi FCM Google
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// FCM API Url | |
$url = 'https://fcm.googleapis.com/fcm/send'; | |
// Put your Server Response Key here | |
$apiKey = "AAAAVX7ftjo:APA91bEtmlrwt7oeUvIJU6G0_cef2vLXnMpZvqBhnb65dTjgfiSv-jlTvFnXPkHbUHlgMKby3EdOLNquBgkIu2xG_686VIb_Ni2seWeVpPg7g5x_w4boZSoXw5Y48RnFS_qCeWT0VGYZ"; | |
// Compile headers in one variable | |
$headers = array ( | |
'Authorization:key=' . $apiKey, | |
'Content-Type:application/json' | |
); | |
// Add notification content to a variable for easy reference | |
$notifData = [ | |
'title' => $title, | |
'body' => $message, | |
'tipes' => $type | |
]; | |
// Create the api body | |
$apiBody = [ | |
'notification' => $notifData, | |
'data' => $notifData,// Optional | |
'to' => $token | |
]; | |
// Initialize curl with the prepared headers and body | |
$ch = curl_init(); | |
curl_setopt ($ch, CURLOPT_URL, $url ); | |
curl_setopt ($ch, CURLOPT_POST, true ); | |
curl_setopt ($ch, CURLOPT_HTTPHEADER, $headers); | |
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, true ); | |
curl_setopt ($ch, CURLOPT_POSTFIELDS, json_encode($apiBody)); | |
// Execute call and save result | |
$result = curl_exec ( $ch ); | |
// Close curl after call | |
curl_close ( $ch ); | |
// return $result; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment