Android Push Notification PHP class using Google Cloud Messaging (GCM)
Google cloud Messaging (GCM) is a service that helps developer to send push notification data to any Android devices. With help of this feature developer can send push message to Android device whenever new data or promotion offers arrives in Android application.
Today lots of Android applications using Google cloud Messaging service to send multiple push messages to multiple Android devices. You can find more at it’s official document at https://developers.google.com/cloud-messaging/
Push Notification flow with PHP Web Server APP and GCM
- Android device send Sender Id and application Id to GCM server.
- GCM server generates unique registration Id and sends them back to Android device.
- Android device sends registration Id to PHP web server application.
- PHP web server stores registration Id to Database for later use.
- When server needs to send Push Message, It fetch previously stored registration Id from Database
- And send them to the GCM server along with the push message data.
- GCM server will send that message to particular Android device using device registration Id
Get Google Cloud messaging API key
First you need to register your application into Google api Console at https://console.developers.google.com/iam-admin/projects
After creating your project find
Google Cloud Messaging
under Google APIs > Mobile APIs and enabled it.
Now go to Credentials and navigate to
Where will you be calling the API from?
dropdown and select option, for server we will choose Web server
from the dropdown.
Click on Create an API key button that will generate an API key, copy this key and paste it at safe place, we will use this key in our Android push notification PHP class.
Android Push Notification PHP Class
Create a php file called it
androidNotification.php
and add following code.
Copy the Google API key which we have just created in above step, and paste it where I mentioned
GOOGLE_API_KEY
. This key is authorizing our Class to send a message to Google Cloud server, and instantly cloud server sent it to Android device as Push messages.
Add following Public function to the class.
This function is responsible to send notification message to Google’s GCM server url with Android device registration id via curl, (Note: we need Android device registration id from Android phone).
Wrapping up whole Class file
Push Notification Call
Include the above Android Notification Class to the file from where you want to call notification.
include_once 'androidNotification.php';
Call the notification class and add your device registration id where I wrote
YOUR_DEVICE_REGISTRATION_ID
.
You can send one message to multiple devices with this class, you just need to pass your device registration ids array like
$result = $sendAndroidPush->sendNotificationAndroid(array($registatoinId, $registatoinId2, $registatoinId3,… and so on, $data);
Wrapping notification call
Download: android-push-notitication-with-php
Conclusion
You can download complete Push Notification PHP class from above Link, remember you still need android device registration id to send your message. Let me know your thoughts in comment below.
Comments
Post a Comment