PushBots-to-PHP: send the Device RegistrationID using HttpPostRequest

1 Star2 Stars3 Stars4 Stars5 Stars (No Ratings Yet)
Loading...

Sometimes you will need the Android client device RegistrationID  to send individual Push Notification to the client device. Using the PushBots Library it will be easy to get the RegistrationID and send it using the  HttpPost Request. 

There are many Tutorials, showing how to use the PushBots Library to send push notification to your Android Application.

In our case, we have a registration or login Android Activity, that require to send also the device RegistrationID  to your Server. In this demo we will use a PHP Server application. It allow to receive the Post variables and save it in your e.g MySQL database.

Let say you have an account in PushBots and the standard push Notification example is working.

In your Activity just add the device RegistrationID to your HttpPost Request using following code:

import com.pushbots.push.Pushbots;
...
 // add your data
 List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
 nameValuePairs.add(new BasicNameValuePair("username", username));
 nameValuePairs.add(new BasicNameValuePair("deviceRegistrationID", Pushbots.getInstance().getSharedPrefs().getRegistrationID()));

httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
// execute HTTP post request
HttpResponse response = httpClient.execute(httpPost);

Sending your HttpPost Request to the Server, in our Exapmle a PHP Server, You can get the data as following:

<?php
if($_POST){
 echo '{"user": [{"username": '.$_POST['username'].', "deviceRegistrationID": '.$_POST['deviceRegistrationID'].'}]}';
}
?>

Now you will be able to save the “deviceRegistrationID” in the Database. If you need to send push Notification to this device, you will need first to download the PushBots Library . Just create a test.php script and add this data:

<?php 	 
// Push The notification with parameters 
require_once('PushBots.class.php'); 
$pb = new PushBots();  
$appID = 'XXXXXXXXXXXXXXXXXXXXXXXXX';//Application ID : from your PushBots dashboard infos. 
appSecret = 'XXXXXXXXXXXXXXXXXXXXXXXXX'; //Application Secret : from your PushBots dashboard infos. 
//Add the device RegistrationID here ($_POST['deviceRegistrationID']) 
$MyDevice ="YYYYYYYYYYYYYYYYYYYYYYYYYY"; 
$pb->App($appID, $appSecret);
// Notification Settings
$pb->AlertOne("Your Message here");
$pb->PlatformOne("1"); //1 :for Android
$pb->TokenOne($MyDevice);

//Push to Single Device
$pb->PushOne();
?>

Here can you find the Application ID and the Application Secret from the PushBots Application Dashboard:
PushBots_dashboard