Last active
September 9, 2024 04:24
-
Star
(424)
You must be signed in to star a gist -
Fork
(225)
You must be signed in to fork a gist
-
-
Save prime31/5675017 to your computer and use it in GitHub Desktop.
Simple PHP script showing how to send an Android push notification. Be sure to replace the API_ACCESS_KEY with a proper one from the Google API's Console page. To use the script, just call scriptName.php?id=THE_DEVICE_REGISTRATION_ID
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 | |
// API access key from Google API's Console | |
define( 'API_ACCESS_KEY', 'YOUR-API-ACCESS-KEY-GOES-HERE' ); | |
$registrationIds = array( $_GET['id'] ); | |
// prep the bundle | |
$msg = array | |
( | |
'message' => 'here is a message. message', | |
'title' => 'This is a title. title', | |
'subtitle' => 'This is a subtitle. subtitle', | |
'tickerText' => 'Ticker text here...Ticker text here...Ticker text here', | |
'vibrate' => 1, | |
'sound' => 1, | |
'largeIcon' => 'large_icon', | |
'smallIcon' => 'small_icon' | |
); | |
$fields = array | |
( | |
'registration_ids' => $registrationIds, | |
'data' => $msg | |
); | |
$headers = array | |
( | |
'Authorization: key=' . API_ACCESS_KEY, | |
'Content-Type: application/json' | |
); | |
$ch = curl_init(); | |
curl_setopt( $ch,CURLOPT_URL, 'https://android.googleapis.com/gcm/send' ); | |
curl_setopt( $ch,CURLOPT_POST, true ); | |
curl_setopt( $ch,CURLOPT_HTTPHEADER, $headers ); | |
curl_setopt( $ch,CURLOPT_RETURNTRANSFER, true ); | |
curl_setopt( $ch,CURLOPT_SSL_VERIFYPEER, false ); | |
curl_setopt( $ch,CURLOPT_POSTFIELDS, json_encode( $fields ) ); | |
$result = curl_exec($ch ); | |
curl_close( $ch ); | |
echo $result; |
notification
Thank you man!!! It made worthy of my 2 days work.
Thank You!! It really helped out....
Is it possible to add a sound with the notification?
Yes it is.
…On Thu 17 Sep, 2020, 10:04 PM ImtiazXYZ, ***@***.***> wrote:
***@***.**** commented on this gist.
------------------------------
Is it possible to add a sound with the notification?
—
You are receiving this because you commented.
Reply to this email directly, view it on GitHub
<https://gist.github.com/5675017#gistcomment-3457708>, or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AGAEPD757MJT7ZE52AADVA3SGI3CRANCNFSM4HHJLYCA>
.
I tried to add sound with notification. I wrote this code. But, sound not
working. Notification sent successfully.
$token=$_POST['token'];
$doctor=$_POST['d_name'];
/////////////////////////////
$API_KEY =
"AAAAVXABzCk:APA91bHVimlQYaX_JPMO5lN0ezaiPmZiOzNXcSVq0Q5W7uGU1iZnPEMR5crlIhEFkpjM2NGhF1bZRt5vfYdDyrt7OCqXmaUNKLOPhQ3ZxjW_R";
$url = 'https://fcm.googleapis.com/fcm/send';
// $message = ['order_id' => 'hello'];
$notification = [
'title' => 'Appointment Confirmation',
'body' => $doctor.' is enabled your appointment. Please, open
application.',
];
$fields = array (
"to" =>
'cqwFkMdkS5qqQbFaOtDN5eQTahtC5JgGyrN2QIG2xJwbV6U35gyVXFntSABb50pXmk5iWXYuVYK9j4vEa-9rHlRZ-wV-B4n3A1RCyyZWrlznRehQR7qGtuq8algjFs2Dt-OkFw29Ag',
'notification' => $notification,
'data' => $message,
'vibrate'=>'1',
'sound'=>'default',
);
$fields = json_encode ( $fields );
$headers = array (
'Authorization: key=' . $API_KEY,
'Content-Type: application/json'
);
$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_SSL_VERIFYPEER, false );
curl_setopt ( $ch, CURLOPT_POSTFIELDS, $fields );
$result = curl_exec ( $ch );
curl_close ( $ch );
echo $result;
…On Fri, Sep 18, 2020 at 5:42 PM AzrShk ***@***.***> wrote:
***@***.**** commented on this gist.
------------------------------
Yes it is.
On Thu 17 Sep, 2020, 10:04 PM ImtiazXYZ, ***@***.***> wrote:
> ***@***.**** commented on this gist.
> ------------------------------
>
> Is it possible to add a sound with the notification?
>
> —
> You are receiving this because you commented.
> Reply to this email directly, view it on GitHub
> <https://gist.github.com/5675017#gistcomment-3457708>, or unsubscribe
> <
https://github.com/notifications/unsubscribe-auth/AGAEPD757MJT7ZE52AADVA3SGI3CRANCNFSM4HHJLYCA
>
> .
>
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
<https://gist.github.com/5675017#gistcomment-3458780>, or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AK7355GZAYRCZG4Y6E2EJA3SGNBS3ANCNFSM4HHJLYCA>
.
how to send to multiple devices at once with their tokens
It is not working in 2022
Yes it is.
…
On Thu 17 Sep, 2020, 10:04 PM ImtiazXYZ, @.> wrote: @.* commented on this gist. ------------------------------ Is it possible to add a sound with the notification? — You are receiving this because you commented. Reply to this email directly, view it on GitHub https://gist.github.com/5675017#gistcomment-3457708, or unsubscribe https://github.com/notifications/unsubscribe-auth/AGAEPD757MJT7ZE52AADVA3SGI3CRANCNFSM4HHJLYCA .
yes, but HOWWWWWWWWW
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
You are welcome.