Skip to content

Instantly share code, notes, and snippets.

@prime31
Last active April 2, 2024 03:55
Show Gist options
  • Save prime31/5675017 to your computer and use it in GitHub Desktop.
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
<?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;
@Aloha276
Copy link

Replace 'data' in line number 25 with 'notification' to get the notification messages displayed on the notification.

Please how to get the registration ID ?

Follow this video from FCM:
https://youtu.be/BsCBCudx58g

Thanks!! it helped me alot!!

@manishram
Copy link

manishram commented Jun 16, 2020

Thanks!! it helped me alot!!

You are welcome.

@sanjeevkse
Copy link

notification

Thank you man!!! It made worthy of my 2 days work.

@premalASIT
Copy link

Thank You!! It really helped out....

@ImtiazXYZ
Copy link

Is it possible to add a sound with the notification?

@AzrShk
Copy link

AzrShk commented Sep 18, 2020 via email

@ImtiazXYZ
Copy link

ImtiazXYZ commented Sep 18, 2020 via email

@ikhlaqmalik13
Copy link

how to send to multiple devices at once with their tokens

@dzungpv
Copy link

dzungpv commented Jan 17, 2022

It is not working in 2022

@Faz23
Copy link

Faz23 commented Nov 11, 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