Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
if you like this code follow me and star it . i will follow you and use your codes and share them . Simple PHP FireBase (FCM) script showing how to send an Android push notification. Be sure to replace the SERVER_API_ACCESS_KEY with a proper one from the Google API's Console page. To use the script, just call http://sample.com/PhpFireBaseNotific…
<?php
#API access key from Google API's Console
define( 'API_ACCESS_KEY', 'YOUR-SERVER-API-ACCESS-KEY-GOES-HERE' );
$registrationIds = $_GET['id'];
#prep the bundle
$msg = array
(
'body' => 'Body Of Notification',
'title' => 'Title Of Notification',
'icon' => 'myicon',/*Default Icon*/
'sound' => 'mySound'/*Default sound*/
);
$fields = array
(
'to' => $registrationIds,
'notification' => $msg
);
$headers = array
(
'Authorization: key=' . API_ACCESS_KEY,
'Content-Type: application/json'
);
#Send Reponse To FireBase Server
$ch = curl_init();
curl_setopt( $ch,CURLOPT_URL, 'https://fcm.googleapis.com/fcm/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 Of FireBase Server
echo $result;
@TwsiTeDev

This comment has been minimized.

Copy link

commented Nov 29, 2016

invalid reg ..... from where do i get reg id?

@MohammadaliMirhamed

This comment has been minimized.

Copy link
Owner Author

commented Dec 9, 2016

@TwsiTeDev
hi you can get registration Ids From an Java example in Firebase docs for android
you can get this id just when you install App on your phone
goodluck

@sudipdig

This comment has been minimized.

Copy link

commented Dec 29, 2016

its working fine. Thanks.

@MohammadaliMirhamed

This comment has been minimized.

Copy link
Owner Author

commented Jan 9, 2017

Dear @sudipdig
Your welcome buddy

@traveltje

This comment has been minimized.

Copy link

commented Feb 21, 2017

Buddy.. you are amazing!
I tried many scripts but this is the only one that worked for me :)

Thank you!

@gomathyfollowon

This comment has been minimized.

Copy link

commented Mar 7, 2017

Can anyone send full code with explanation step by step how to send push notification in android? pls! Tons of thanks.. if get response ASP

@josemiguel66

This comment has been minimized.

Copy link

commented Apr 21, 2017

Thanks so much! Works perfect

@apurav2007

This comment has been minimized.

Copy link

commented May 24, 2017

thanks

@aprilsacil

This comment has been minimized.

Copy link

commented May 30, 2017

@MohammadaliMirhamed YOUR-SERVER-API-ACCESS-KEY-GOES-HERE is the server key right and THE_DEVICE_REGISTRATION_ID is the device token. i am using the right details but i get:
{"multicast_id":4627545918120818556,"success":0,"failure":1,"canonical_ids":0,"results":[{"error":"MismatchSenderId"}]}

please help.

@sudiptokc

This comment has been minimized.

Copy link

commented May 30, 2017

I ended up with this :
{"multicast_id":5480925760579273559,"success":1,"failure":0,"canonical_ids":0,"results":[{"message_id":"0:1496150197311340%e609af1ce609af1c"}]}

But the issue is : in the service worker push handler , i am not getting the contents set in $msg. In the console I see this :
PushEvent bubbles : false cancelBubble : false cancelable : false composed : false currentTarget : ServiceWorkerGlobalScope data : null defaultPrevented : false eventPhase : 0 path : Array(0) returnValue : true srcElement : ServiceWorkerGlobalScope target : ServiceWorkerGlobalScope timeStamp : 0 type : "push" __proto__ : PushEvent

That is the data is null. Any idea why ?

@alcarcel

This comment has been minimized.

Copy link

commented May 31, 2017

hello, I have this message:
append .json to your request URI to use the REST API.
if you can give me any idea i really apreciate....

@MaheshSheliya

This comment has been minimized.

Copy link

commented Jun 2, 2017

How can i set push notification time ?

@pradeeprelwani

This comment has been minimized.

Copy link

commented Jun 10, 2017

not working for me. please suggest what to do with this error.
{"multicast_id":7086393246851971856,"success":0,"failure":1,"canonical_ids":0,"results":[{"error":"NotRegistered"}]}

@bopoda

This comment has been minimized.

Copy link

commented Jun 27, 2017

It works for me.
But how can i put image into push notification (not icon but full screen width image) ?
I tried different approaches but never see my image (android v4, v5). Is it need some client changes for images?
Thx.

@iparrabb

This comment has been minimized.

Copy link

commented Jun 29, 2017

For send to multiple devices change key to for registration_ids on $fields.

$fields = [
    'registration_ids' => $registrationIds,
    'data'	    => $msg
];

This works for my.

@pluzmedia

This comment has been minimized.

Copy link

commented Aug 5, 2017

@bopoda use remote views

@darkjhonas

This comment has been minimized.

Copy link

commented Sep 15, 2017

Thanks for all!

@betobaracho

This comment has been minimized.

Copy link

commented Sep 16, 2017

Thanks, but I only succeeded when I modified for this:
$fields = array
(
'registration_ids' => $registrationIds,
'notification' => $msg
);

@gatshu

This comment has been minimized.

Copy link

commented Oct 1, 2017

i tried using postman, and it give me error 500 Internal server error. any suggest ?
api_access_key and registrationIds already filled

@mannigit

This comment has been minimized.

Copy link

commented Oct 8, 2017

The code is working very well.
please let me know how can i send it to the multiple devices. and how can i detect the unregistered device token after sending.
Thanks.

@syedahrabia

This comment has been minimized.

Copy link

commented Oct 11, 2017

From where i can get my THE_DEVICE_REGISTRATION_ID ?

@shubhodeep

This comment has been minimized.

Copy link

commented Oct 25, 2017

@MohammadaliMirhamed

Getting the following error:
{"multicast_id":5298140393388678667,"success":0,"failure":1,"canonical_ids":0,"results":[{"error":"InvalidRegistration"}]}

The registration id was picked up from the app. It is correct. FCM API key is also correct. Not sure where the issue is.

@kamhawy4

This comment has been minimized.

Copy link

commented Oct 26, 2017

THE_DEVICE_REGISTRATION_ID ?
????????????????????????????????????

@joginder89

This comment has been minimized.

Copy link

commented Oct 31, 2017

To send multiple devices
'registration_ids' => array($registrationIds)

@shahinzadeh

This comment has been minimized.

Copy link

commented Nov 6, 2017

Worked as expected. Thanks!

@parthsureliya

This comment has been minimized.

Copy link

commented Nov 9, 2017

i am getting MismatchSenderId error
{"multicast_id":5133840611575562782,"success":0,"failure":1,"canonical_ids":0,"results":[{"error":"# MismatchSenderId"}]}

@rk215

This comment has been minimized.

Copy link

commented Nov 9, 2017

it works .thanks for helping beginner

@SharadPanpatil

This comment has been minimized.

Copy link

commented Nov 14, 2017

its working fine thanks a lot............!

@maleavle

This comment has been minimized.

Copy link

commented Nov 19, 2017

It works, but can i do vobrate de device? how? and can i redirect to specific url in chrome when user click in the notification?

@batkhuu1

This comment has been minimized.

Copy link

commented Dec 7, 2017

its working fine thanks WOOOOOOOW

@signalpoint

This comment has been minimized.

Copy link

commented Dec 10, 2017

+1, thank you sir!

@kadriavesh47

This comment has been minimized.

Copy link

commented Dec 11, 2017

What is the correct format to send multiple registrationIds
I am getting error for sending multiple ids
{"multicast_id":6120534523968825086,"success":0,"failure":1,"canonical_ids":0,"results":[{"error":"InvalidRegistration"}]}

@shrutiupari

This comment has been minimized.

Copy link

commented Dec 15, 2017

It works fine.. Can I know how to send for iOS device in a similar way?
Because I'm trying to run few of the iOS push notifications but couldn't. Any help would be very useful to me.

@mtkumar82

This comment has been minimized.

Copy link

commented Jan 10, 2018

I am also getting below response with invalidRegistration
{"multicast_id":5862143825007794641,"success":0,"failure":1,"canonical_ids":0,"results":[{"error":"InvalidRegistration"}]}
I follow the below Steps.

  1. Copy your code in my php file.
  2. Create project on firebase console, It returns me Server Accesss key and Legacy key under Project Overview-> project settings -> Cloud Message.
  3. I copy and paste the key in my php code\
  4. Then Under Authentication section, I created users, And get the UID of one of the user and paste in my registration code.
  5. When I run the script, I am getting InvalidRegistration error.

May I know please where I am doing wrong?

Thanks

@LeoFeitosa

This comment has been minimized.

Copy link

commented Jan 11, 2018

How to send multiple topic in a single push?

@Rishi121

This comment has been minimized.

Copy link

commented Jan 19, 2018

It's working fine , but when I am running this code , notification goes by service worker and title , body , icon all goes from service worker .
I want to know how I can customize this . And what will happen when I click the notification , Can I redirect on a dynamic url when notification is clicked .

@kkmishra7

This comment has been minimized.

Copy link

commented Jan 30, 2018

I have 100000 registration Ids in my DB. How i can send notification in chunk because if i am trying to send notification 100000 device i will hangs.
FCM allow to only 1000 id in one message how we can send on 500000 devices in php

@Lucaro-P

This comment has been minimized.

Copy link

commented Feb 9, 2018

Works greatly fine. Thank you, sir. +1

@kjareena

This comment has been minimized.

Copy link

commented Feb 26, 2018

Hi,

Please help me any one. what is $registrationIds .

How can i get registrationIds.

@8108905324

This comment has been minimized.

Copy link

commented Mar 9, 2018

Hello Sir,
Its worked ...
Thank You So Much

@syedsouban

This comment has been minimized.

Copy link

commented Apr 6, 2018

For all those who don't know what $registrationIds is. It is a FCM Token generated. You can find the token from the FirebaseInstanceIdService's onTokenRefresh() method by calling FirebaseInstanceId.getInstance().getToken()

@Kumar4321

This comment has been minimized.

Copy link

commented Apr 26, 2018

I am getting error
{"multicast_id":8885871081448522914,"success":1,"failure":0,"canonical_ids":0,"results":[{"message_id":"0:1524723818481580%0000000000000000"}]}

please help me

@kaveh-94

This comment has been minimized.

Copy link

commented May 5, 2018

it's not error."success":1," that means your notification was send

@ils-rahula

This comment has been minimized.

Copy link

commented May 30, 2018

Hi all tech champ,
I'm getting InvalidRegistration error in FCM code.
{"multicast_id":6204887613133668009,"success":0,"failure":1,"canonical_ids":0,"results":[{"error":"InvalidRegistration"}]}
Can some one please help me?

@kamran03223

This comment has been minimized.

Copy link

commented Jun 8, 2018

Help me please
{"multicast_id":5675976306928296499,"success":0,"failure":1,"canonical_ids":0,"results":[{"error":"InvalidRegistration"}]}
i have inserted token in table and using token in web service which is responding "Invalid Registration" What can be issue?
Thanks in advance for help

@domixr

This comment has been minimized.

Copy link

commented Jun 20, 2018

Great code, it worked for me but only a clarification for the guys that will use it: use as API_ACCESS_KEY the server key that is possible to find, from the Firebase console, in project overview -> setting's project -> cloud messaging.

@hrsetyono

This comment has been minimized.

Copy link

commented Jun 29, 2018

Anyone know how to get the Registration ID for web? I successfully generate this token:

{
  "endpoint":"https://fcm.googleapis.com/fcm/send/1111111:2222222222222222",
  "expirationTime":null,
  "keys":{"p256dh":"aaaaaaaaaaaa","auth":"bbbbbbbbbbbb"}
}

Is it correct that the 11111:2222222 is the Registration ID? I still get MismatchSenderID error just like the people above.

I tried other value but it gives me InvalidRegistration error.

@kamal-nyaupane

This comment has been minimized.

Copy link

commented Jul 9, 2018

i m getting this message :-
{"multicast_id":7145009148233962961,"success":1,"failure":0,"canonical_ids":0,"results":[{"message_id":"0:1531121690342192%0000000000000000"}]}
but i m not getting notification on server neither any other message please help me.

@yickmo

This comment has been minimized.

Copy link

commented Jul 12, 2018

@kamal-nyaupane

If you are talking about Android, then I think we have the same issue. Go to Settings -> Developer Options and look for "Show notification channel warnings". I think this option is only available on Android 8. Enable it and test it again. If you see something like "failed to post notification on channel 'null', then you do have the same issue as what I have.

Android 8 have this new feature for notification which can be assigned to a channel ID.

I Google this for the entire day and tried several recommendation, but unfortunately I am still trying to find the solution to this.

@arshad115

This comment has been minimized.

Copy link

commented Jul 19, 2018

New api uses this format:
https://fcm.googleapis.com/v1/projects/myproject-b5ae1/messages:send
You have to include project name and version of api.

Also there are changes to auth and payload. More info here: https://firebase.google.com/docs/cloud-messaging/migrate-v1

@cristo1985

This comment has been minimized.

Copy link

commented Jul 24, 2018

$registrationIds = $_GET['id']; => WHERE TO GET THE LIST OF REGISTRATION IDS? IS THERE A SCRIPT? I WANT SEND TO ANY DEVICE HAD INSTALLED MY APP!!!

@ark2018

This comment has been minimized.

Copy link

commented Jul 27, 2018

{"multicast_id":8910091881405662450,"success":0,"failure":1,"canonical_ids":0,"results":[{"error":"MismatchSenderId"}]}
Please help

@MohammadaliMirhamed

This comment has been minimized.

Copy link
Owner Author

commented Aug 5, 2018

Im so glad guys this code is helping you . Thank you so much

@MohammadaliMirhamed

This comment has been minimized.

Copy link
Owner Author

commented Aug 5, 2018

@ark2018 check your api code

@JayeshBreezeway

This comment has been minimized.

Copy link

commented Aug 7, 2018

how to redirect www.google.com in notification click.

@deepak-gehlot

This comment has been minimized.

Copy link

commented Aug 23, 2018

can you explain this code .

@ghost

This comment has been minimized.

Copy link

commented Aug 23, 2018

@deepak-gehlot
Hi friend. Which part do you want to i explaine for you?

@bastimm

This comment has been minimized.

Copy link

commented Aug 27, 2018

THANKS! works well! But:
Would be nice if somebody could explain what changes are needed for the new v1 mentioned by @arshad115

@pasupathinathan

This comment has been minimized.

Copy link

commented Sep 25, 2018

Hi friend,
Your code is working successfully, I got a registrationId, But how to store this value in another variable... pls help me... what can i do?

@PMistry123

This comment has been minimized.

Copy link

commented Sep 25, 2018

I have used the same script for GCM, what should I do to get new keys for my android app for fcm with this php script

Also, my app has added gcm.senderid, do I need to change this as well?

@ghost

This comment has been minimized.

Copy link

commented Sep 25, 2018

dear @pasupathinathan hi.
Ask you question more clear.

@ghost

This comment has been minimized.

Copy link

commented Sep 25, 2018

@PanktiPancholi88 hi dear. I thank if you want get the key. You shall go to google fcm key part

@jiyaan

This comment has been minimized.

Copy link

commented Jan 6, 2019

please help me to resolve this problem for fcm push notification using php
{
"multicast_id": 8064599321131175636,
"success": 0,
"failure": 1,
"canonical_ids": 0,
"results": [
{
"error": "MismatchSenderId"
}
]
}

@mosesyashua

This comment has been minimized.

Copy link

commented Mar 30, 2019

Thank you.

@mosesyashua

This comment has been minimized.

Copy link

commented Mar 30, 2019

After trying all sorts of complicated plugins that required complicated installations and giving me so much errors and without success, I am amazed that this simple script works. Wow! Thank you.

@mountainclimbing

This comment has been minimized.

Copy link

commented May 16, 2019

Hello team,

Any examples for cordova/phonegap? Been looking around for an example on how to setup the index.html to receive notifications but no luck

@MohammadaliMirhamed

This comment has been minimized.

Copy link
Owner Author

commented Jul 12, 2019

Hello team,

Any examples for cordova/phonegap? Been looking around for an example on how to setup the index.html to receive notifications but no luck

hi dear. yes, there is a way. make a route with the top script and in your web application then send the request by your Cordova/PhoneGap?

@MohammadaliMirhamed

This comment has been minimized.

Copy link
Owner Author

commented Jul 12, 2019

Thank you.

Your welcome

@MohammadaliMirhamed

This comment has been minimized.

Copy link
Owner Author

commented Jul 12, 2019

please help me to resolve this problem for fcm push notification using php
{
"multicast_id": 8064599321131175636,
"success": 0,
"failure": 1,
"canonical_ids": 0,
"results": [
{
"error": "MismatchSenderId"
}
]
}

its simple brother. you just need to define your application and then get the token and put it there. it will work well.

@rhafiko

This comment has been minimized.

Copy link

commented Sep 11, 2019

I created a fork available here

What's the difference from the original script ?
-You can send message to a specific device by informing a device ID to the parameter 'id'
-or send a broadcast to a specific topic to the parameter 'topic'.
-it is necessary to set a specific topic previously on your Firebase Console and
also the device must subscribe to it.
-You will also receive a error message if you do not pass any value to those two needed parameters.

@rhafiko

This comment has been minimized.

Copy link

commented Sep 11, 2019

please help me to resolve this problem for fcm push notification using php
{
"multicast_id": 8064599321131175636,
"success": 0,
"failure": 1,
"canonical_ids": 0,
"results": [
{
"error": "MismatchSenderId"
}
]
}

Create project on firebase console, It returns me Server Accesss key and Legacy key under Project Overview-> project settings -> Cloud Message.

Copy the legacy server key and put it on your script API_ACCESS_KEY;

Will work.

@MohammadaliMirhamed

This comment has been minimized.

Copy link
Owner Author

commented Sep 12, 2019

please help me to resolve this problem for fcm push notification using php
{
"multicast_id": 8064599321131175636,
"success": 0,
"failure": 1,
"canonical_ids": 0,
"results": [
{
"error": "MismatchSenderId"
}
]
}

Create project on firebase console, It returns me Server Accesss key and Legacy key under Project Overview-> project settings -> Cloud Message.

Copy the legacy server key and put it on your script API_ACCESS_KEY;

Will work.

Your Well Come Dear

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.