Skip to content

Instantly share code, notes, and snippets.

@jesobreira
Created February 6, 2018 20:17
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jesobreira/bb7651313d7426c4bea83fecb67530f7 to your computer and use it in GitHub Desktop.
Save jesobreira/bb7651313d7426c4bea83fecb67530f7 to your computer and use it in GitHub Desktop.
Send Push using Google FCM
<?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');
@DelphiWorlds
Copy link

Thanks for this. I had used a couple of other examples that simply didn't work; yours does 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment