<?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; |
This comment has been minimized.
This comment has been minimized.
@TwsiTeDev |
This comment has been minimized.
This comment has been minimized.
its working fine. Thanks. |
This comment has been minimized.
This comment has been minimized.
Dear @sudipdig |
This comment has been minimized.
This comment has been minimized.
Buddy.. you are amazing! Thank you! |
This comment has been minimized.
This comment has been minimized.
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 |
This comment has been minimized.
This comment has been minimized.
Thanks so much! Works perfect |
This comment has been minimized.
This comment has been minimized.
thanks |
This comment has been minimized.
This comment has been minimized.
@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: please help. |
This comment has been minimized.
This comment has been minimized.
I ended up with this : 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 : That is the data is null. Any idea why ? |
This comment has been minimized.
This comment has been minimized.
hello, I have this message: |
This comment has been minimized.
This comment has been minimized.
How can i set push notification time ? |
This comment has been minimized.
This comment has been minimized.
not working for me. please suggest what to do with this error. |
This comment has been minimized.
This comment has been minimized.
It works for me. |
This comment has been minimized.
This comment has been minimized.
For send to multiple devices change key to for registration_ids on $fields. $fields = [
'registration_ids' => $registrationIds,
'data' => $msg
]; This works for my. |
This comment has been minimized.
This comment has been minimized.
@bopoda use remote views |
This comment has been minimized.
This comment has been minimized.
Thanks for all! |
This comment has been minimized.
This comment has been minimized.
Thanks, but I only succeeded when I modified for this: |
This comment has been minimized.
This comment has been minimized.
i tried using postman, and it give me error 500 Internal server error. any suggest ? |
This comment has been minimized.
This comment has been minimized.
The code is working very well. |
This comment has been minimized.
This comment has been minimized.
From where i can get my THE_DEVICE_REGISTRATION_ID ? |
This comment has been minimized.
This comment has been minimized.
Getting the following error: The registration id was picked up from the app. It is correct. FCM API key is also correct. Not sure where the issue is. |
This comment has been minimized.
This comment has been minimized.
THE_DEVICE_REGISTRATION_ID ? |
This comment has been minimized.
This comment has been minimized.
To send multiple devices |
This comment has been minimized.
This comment has been minimized.
Worked as expected. Thanks! |
This comment has been minimized.
This comment has been minimized.
i am getting MismatchSenderId error |
This comment has been minimized.
This comment has been minimized.
it works .thanks for helping beginner |
This comment has been minimized.
This comment has been minimized.
its working fine thanks a lot............! |
This comment has been minimized.
This comment has been minimized.
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? |
This comment has been minimized.
This comment has been minimized.
its working fine thanks WOOOOOOOW |
This comment has been minimized.
This comment has been minimized.
+1, thank you sir! |
This comment has been minimized.
This comment has been minimized.
What is the correct format to send multiple registrationIds |
This comment has been minimized.
This comment has been minimized.
It works fine.. Can I know how to send for iOS device in a similar way? |
This comment has been minimized.
This comment has been minimized.
I am also getting below response with invalidRegistration
May I know please where I am doing wrong? Thanks |
This comment has been minimized.
This comment has been minimized.
How to send multiple topic in a single push? |
This comment has been minimized.
This comment has been minimized.
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 . |
This comment has been minimized.
This comment has been minimized.
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. |
This comment has been minimized.
This comment has been minimized.
Works greatly fine. Thank you, sir. +1 |
This comment has been minimized.
This comment has been minimized.
Hi, Please help me any one. what is $registrationIds . How can i get registrationIds. |
This comment has been minimized.
This comment has been minimized.
Hello Sir, |
This comment has been minimized.
This comment has been minimized.
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() |
This comment has been minimized.
This comment has been minimized.
I am getting error please help me |
This comment has been minimized.
This comment has been minimized.
it's not error."success":1," that means your notification was send |
This comment has been minimized.
This comment has been minimized.
Hi all tech champ, |
This comment has been minimized.
This comment has been minimized.
Help me please |
This comment has been minimized.
This comment has been minimized.
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. |
This comment has been minimized.
This comment has been minimized.
Anyone know how to get the Registration ID for web? I successfully generate this token:
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. |
This comment has been minimized.
This comment has been minimized.
i m getting this message :- |
This comment has been minimized.
This comment has been minimized.
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. |
This comment has been minimized.
This comment has been minimized.
New api uses this format: Also there are changes to auth and payload. More info here: https://firebase.google.com/docs/cloud-messaging/migrate-v1 |
This comment has been minimized.
This comment has been minimized.
$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!!! |
This comment has been minimized.
This comment has been minimized.
{"multicast_id":8910091881405662450,"success":0,"failure":1,"canonical_ids":0,"results":[{"error":"MismatchSenderId"}]} |
This comment has been minimized.
This comment has been minimized.
Im so glad guys this code is helping you . Thank you so much |
This comment has been minimized.
This comment has been minimized.
@ark2018 check your api code |
This comment has been minimized.
This comment has been minimized.
how to redirect www.google.com in notification click. |
This comment has been minimized.
This comment has been minimized.
can you explain this code . |
This comment has been minimized.
This comment has been minimized.
@deepak-gehlot |
This comment has been minimized.
This comment has been minimized.
THANKS! works well! But: |
This comment has been minimized.
This comment has been minimized.
Hi friend, |
This comment has been minimized.
This comment has been minimized.
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? |
This comment has been minimized.
This comment has been minimized.
dear @pasupathinathan hi. |
This comment has been minimized.
This comment has been minimized.
@PanktiPancholi88 hi dear. I thank if you want get the key. You shall go to google fcm key part |
This comment has been minimized.
This comment has been minimized.
please help me to resolve this problem for fcm push notification using php |
This comment has been minimized.
This comment has been minimized.
Thank you. |
This comment has been minimized.
This comment has been minimized.
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. |
This comment has been minimized.
This comment has been minimized.
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 |
This comment has been minimized.
This comment has been minimized.
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? |
This comment has been minimized.
This comment has been minimized.
Your welcome |
This comment has been minimized.
This comment has been minimized.
its simple brother. you just need to define your application and then get the token and put it there. it will work well. |
This comment has been minimized.
This comment has been minimized.
I created a fork available here What's the difference from the original script ? |
This comment has been minimized.
This comment has been minimized.
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. |
This comment has been minimized.
This comment has been minimized.
Your Well Come Dear |
This comment has been minimized.
This comment has been minimized.
Thanks Pal it worked like magic. So for the benefit of those struggling with this error I faced this for nearly a week only to discover that i was parsing a wrong device token. the reason being i was getting the token on device ready event through an alert() method. since i couldn`t copy it directly i had to screenshot the result then use some image to text app to get the token as string which is where i got it all wrong. To fix this i had to use use the whatsapp api to send the token to my account as plain text. then it worked. |
This comment has been minimized.
This comment has been minimized.
hi dude . |
This comment has been minimized.
invalid reg ..... from where do i get reg id?