In this article, We’ll see how to send push notification to android and ios both using fcm.
Firebase Cloud Messaging (FCM)
is a new cloud messaging services
to send push notifications to Android, iOS and Web applications. It is the newer version of GCM (google cloud messaging) with more features.
To send notifications, we basically required two things:
- Authorization key
- Device Token
We can get Authorization key (API_ACCESS_KEY) available in Firebase Console -> Project Settings -> Cloud Messaging -> Server key
, and device token
we can get at the time of registering or logging by the android or ios users through API.
Simple steps to register your app under FCM
- Go to the Firebase console
https://console.firebase.google.com
(Login using Google account) - Create a new project
- Click on “Add Firebase to your iOS app” Or “Add Firebase to your Android app” button and Register Your App.
To send push notifications, use below PHP script.
Create PushNotifications.php
file
class PushNotifications { private static $URL = "https://fcm.googleapis.com/fcm/send"; //API URL of FCM private static $API_ACCESS_KEY = 'AAAApRMIM9I:APA91bHd7N...........Taq'; // YOUR_FIREBASE_API_KEY public function __construct() { } public static function sendPushNotification($token = "", $fields = array()) { $registrationIds = array(); array_push($registrationIds, $token); $msg = array('body' => $fields['body'], 'title' => $fields['title']); $fields = array('registration_ids' => $registrationIds, 'notification' => $msg); $headers = array('Authorization: key=' . self::$API_ACCESS_KEY, 'Content-Type: application/json'); #Send Reponse To FireBase Server $ch = curl_init(); curl_setopt($ch,CURLOPT_URL, self::$URL); 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); return $result; } //include PushNotifications.php file and call sendPushNotification method on your page require_once 'PushNotifications.php'; $device_token = "eIWdeiXnVW4:APA1FMWhLK60.......TdM6IyaYJzdo"; $fields = ["title" => "message title", "body" => "message text"]; $response = PushNotifications::sendPushNotification($device_token, $fields); print_r($response);
That’s it!. Please share your thoughts or suggestions in the comments below.
Organic search website traffic is distinct.
Hey There. I found your blog using msn. This is an extremely well written article.
I will be sure to bookmark it and come back
to read more of your useful info. Thanks for the post.
I’ll definitely return.
Nice thanks
You’re so awesome! I do not suppose I’ve read something like this before.
So wonderful to discover somebody with a few original
thoughts on this issue. Seriously.. many thanks for starting this
up. This website is something that is required on the internet, someone with some originality!
I am really thankful to the holder of this site who has shared
this impressive article at here.
An impressive share! I have just forwarded this onto a co-worker who had been conducting a little homework on this.
And he actually ordered me lunch because I stumbled upon it for him…
lol. So allow me to reword this…. Thank YOU for the meal!!
But yeah, thanks for spending some time to talk about this
matter here on your web site.
Hello! Someone in my Facebook group shared this website with
us so I came to give it a look. I’m definitely enjoying the information. I’m book-marking and
will be tweeting this to my followers! Superb blog and wonderful design and style.