How to implement firebase push notification in android and iphone
If you need to send a Firebase notification using PHP, then simply use these tutorials, which can help you send the notification to Android and iPhone devices. Basically there is a GCM (Google Cloud Messaging), which is sending the notification, but now Google has launched Firebase Cloud Messaging (FCM).
Step 1)
Generate the Firebase Server Key. For this you need to follow the below steps.
- Log in to the Firebase Developer Console, and if you haven’t created a project yet, click “Create Project.”
- Enter a project name, accept the Firebase terms, and press “CREATE PROJECT.”
- Does it set up Google Analytics for your project? Select “Not Right Now” and press “CREATE PROJECT.”
- The project will be created, and you will get the message “Your new project is ready.” Press the “Continue” button to redirect to your project dashboard page.
- On the dashboard page, click the gear icon in the top left and select “Project settings.”
- Go to the “Cloud Messaging Section” section, and you will have the server key.
- Done! I hope you successfully generate a Firebase server key.
Step 2)
Create a file named fcm.php and add the below push notification PHP code. Be sure to replace the Your Firebase Server API Key Here with a proper one from the Google API’s Console page.
<?php
class FCM {
function __construct() {
}
/**
* Sending Push Notification
*/
public function send_notification($registatoin_ids, $notification,$device_type) {
$url = 'https://fcm.googleapis.com/fcm/send';
if($device_type == "Android"){
$fields = array(
'to' => $registatoin_ids,
'data' => $notification
);
} else {
$fields = array(
'to' => $registatoin_ids,
'notification' => $notification
);
}
// Firebase API Key
$headers = array('Authorization:key=Your Firebase
Server API Key Here','Content-Type:application/json');
// Open connection
$ch = curl_init();
// Set the url, number of POST vars, POST data
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
// Disabling SSL Certificate support temporarly
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fields));
$result = curl_exec($ch);
if ($result === FALSE) {
die('Curl failed: ' . curl_error($ch));
}
curl_close($ch);
}
}
?>
Step 3)
Finally, create an index.php and add the below code. Before testing, please replace your device token with your real device token.
<?php
$regId ="Your Device Token";
// INCLUDE YOUR FCM FILE
include_once 'fcm.php';
$notification = array();
$arrNotification= array();
$arrData = array();
$arrNotification["body"] ="Test by Ravi Sukhadia.";
$arrNotification["title"] = "TECHIBUCKET";
$arrNotification["sound"] = "default";
$arrNotification["type"] = 1;
$fcm = new FCM();
$result = $fcm->send_notification($regId, $arrNotification,"Android");
?>
Step 4)
Send Push Notification to iOS Using PHP. You just need to change “Android” to “IOS” in your index.php file. like below…
FOR ANDROID
$result=$fcm->send_notification($regId,$arrNotification,"Android");
FOR IPHONE
$result=$fcm->send_notification($regId, $arrNotification,"IOS");
Step 5)
Now run your index.php file, and you can get the notification on your devices.
Thank you for any other informative website. Where else may I am getting that kind of information written in such a perfect means? I’ve a project that I’m just now operating on, and I have been on the glance out for such info.
I wanted to thank you for this great read!! I absolutely enjoyed every little bit of it. I have got you book marked to check out new stuff you
This is a topic that’s close to my heart… Cheers! Where are your contact details though?
Incredible! This blog looks just like my old one! It’s on a completely different subject but it has pretty much the same page layout and design. Great choice of colors!
I’ll immediately grab your rss feed as I can’t to find your email subscription hyperlink or e-newsletter service. Do you’ve any? Please let me recognise so that I could subscribe. Thanks.
Ahaa, its good conversation regarding this piece of writing at this place at this website, I have read all that, so at this time me also commenting at this place.
Hi, I read your new stuff on a regular basis. Your writing style is witty, keep up the good work!
Incredible! This blog looks just like my old one! It’s on a totally different subject but it has pretty much the same layout and design. Great choice of colors!
Hi mates, how is everything, and what you wish for to say on the topic of this article, in my view its actually awesome designed for me.
I will right away take hold of your rss feed as I can not find your e-mail subscription link or e-newsletter service. Do you have any? Please let me recognise in order that I may subscribe. Thanks.
Some genuinely wonderful info, Glad I discovered this.
I am glad to be one of many visitants on this outstanding site (:, regards for posting.
Normally I don’t read post on blogs, but I wish to say that this write-up very pressured me to take a look at and do so! Your writing style has been surprised me. Thanks, very great post.
I am sure this paragraph has touched all the internet viewers, its really really nice article on building up new weblog.
Hello! I’ve been following your site for a while now and finally got the courage to go ahead and give you a shout out from Humble Tx! Just wanted to tell you keep up the fantastic job!
I am sure this article has touched all the internet viewers, its really really nice post on building up new webpage.
This is a topic that is near to my heart… Thank you! Where are your contact details though?
This is a topic that is close to my heart… Best wishes! Where are your contact details though?
Ahaa, its fastidious discussion on the topic of this article at this place at this webpage, I have read all that, so at this time me also commenting here.
I really like what you guys are usually up too. This kind of clever work and reporting! Keep up the superb works guys I’ve added you guys to my own blogroll.
I need to to thank you for this good read!! I certainly loved every bit of it. I’ve got you bookmarked to look at new stuff you
You’ve made some really good points there. I looked on the net for more info about the issue and found most people will go along with your views on this web site.
I like what you guys are up too. This sort of clever work and reporting! Keep up the amazing works guys I’ve added you guys to my blogroll.