Created
February 6, 2018 20:17
-
-
Save jesobreira/bb7651313d7426c4bea83fecb67530f7 to your computer and use it in GitHub Desktop.
Send Push using Google FCM
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
<?php | |
// Firebase Cloud Messaging Authorization Key | |
define('FCM_AUTH_KEY', 'your key here'); | |
function sendPush($to, $title, $body, $icon, $url) { | |
$postdata = json_encode( | |
[ | |
'notification' => | |
[ | |
'title' => $title, | |
'body' => $body, | |
'icon' => $icon, | |
'click_action' => $url | |
] | |
, | |
'to' => $to | |
] | |
); | |
$opts = array('http' => | |
array( | |
'method' => 'POST', | |
'header' => 'Content-type: application/json'."\r\n" | |
.'Authorization: key='.FCM_AUTH_KEY."\r\n", | |
'content' => $postdata | |
) | |
); | |
$context = stream_context_create($opts); | |
$result = file_get_contents('https://fcm.googleapis.com/fcm/send', false, $context); | |
if($result) { | |
return json_decode($result); | |
} else return false; | |
} | |
sendPush('your-client-push-authorization-key', 'This is the title!', 'And this is the text.', 'https://anysite.com/some_image.png', 'https://openthissiteonclick.com'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks for this. I had used a couple of other examples that simply didn't work; yours does 👍