Skip to content

Instantly share code, notes, and snippets.

@prime31
Last active September 9, 2024 04:24
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;
@mountainclimbing
Copy link

mountainclimbing commented May 16, 2019

i am developing an ionic application for android, i have a problem with notifications, if i send message with token from console of firebase, the message is received in my app, also if use the this script , the message not received the response is {"multicast_id":9211726170113352103,"success":1,"failure":0,"canonical_ids":0,"results":[{"message_id":"0:1485467751893255%e573a045f9fd7ecd"}]}"

@DavidAlejandroM please share code for your index.html, do we need to activate/call any function? I keep getting "InvalidRegistrationID"

@AzrShk
Copy link

AzrShk commented Jun 17, 2019

great function,
i dont recive notification when the app is closed.. do you know what i need to do????

you should use custom json(data) to send fcm

@mountainclimbing
Copy link

Hello,

The code was working very well, then from nowhere am receiving this error: "Error=DeprecatedEndpoint". And the notification is not being sent. Is there a fix for this?

@giovannidilob
Copy link

Same problem for me

@acaliebe
Copy link

acaliebe commented Aug 7, 2019

'https://android.googleapis.com/gcm/send' is outdated, use 'https://fcm.googleapis.com/fcm/send' instead. (worked for me this morning)

Sample to check with new URL:
curl "https://fcm.googleapis.com/fcm/send" -X POST -H "Authorization: key=MY_AUTH_KEY" -H "Content-Type: application/json" -d '{"registration_ids":["MY_DEVICE_REGISTRATION_ID"],"priority": "high","data": {"vibrate":"false", "changes":"state:off", "source":"gcmsend_fhem", "type":"notify", "deviceName":"TestLampe", "gcmDeviceName":"andFHEMUpdater"}}'

Answer must be anything like this:
{"multicast_id":123,"success":1,"failure":0,"canonical_ids":0,"results":[{"message_id":"0:123"}]}

@mountainclimbing
Copy link

Thank you @acaliebe!!! Its back on.

@RuturajSarnobat
Copy link

RuturajSarnobat commented Sep 26, 2019

Hello @acaliebe,
It works fine for me, It successfully gives a Success message but actual notification is not getting. Dont know why this happened. Can you please help me, I am eagerly waiting for your reply. Thanks.

@AzrShk
Copy link

AzrShk commented Sep 26, 2019 via email

@erigobeli
Copy link

Thank you @acaliebe

@imguccho
Copy link

imguccho commented Dec 1, 2019

{"multicast_id":4314312645771955637,"success":0,"failure":1,"canonical_ids":0,"results":[{"error":"InvalidRegistration"}]}

why no one is giving the solution of this error? when you are sharing your code you should make sure that it will execute successfully

@AzrShk
Copy link

AzrShk commented Dec 1, 2019 via email

@wirakw
Copy link

wirakw commented Dec 14, 2019

How to find THE_DEVICE_REGISTRATION_ID ? please, help me.

@mazud21
Copy link

mazud21 commented Dec 23, 2019

@wirakw

How to find THE_DEVICE_REGISTRATION_ID ? please, help me.

@wirakw it got from client(android) get regId(), just like generated string

myabe like this :
https://gist.github.com/prime31/5675017#gistcomment-1785186

@WolfgangFentler
Copy link

hi everybody,
i was trying to insert a picture to the message, but failed in any way, here is my code:

$msg = array
(
'body' => "Wir wünschen all unseren Kunden, Freunden und Familien ein frohes Osterfest und freuen uns Euch bald wieder in einem unserer Studios begrüßen zu dürfen!",
'title' => "Nachricht aus dem MoveCenter",
'vibrate' => 1,
'sound' => 1,
'image' => 'https://www.fentler.com/move/app1170/imagesApp/osterhase.jpg',
);

$fields = array
(
'registration_ids' => $registrationIds,
'notification' => $msg
);

$headers = array
(
'Authorization: key=' . 'MY KEY HERE',
'Content-Type: application/json'
);

all works fine, but no image is displayed on adroid

@manishram
Copy link

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

@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 ?

@manishram
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

@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