How To do Push notification in android by using C2DM

H

Step 1: Sign-up for a C2DM account with Google

Follow the steps here. Normally takes a day or so before you are ready to roll.==>
https://developers.google.com/android/c2dm/signup

Step 2: Setup your Manifest
Overview of the manifest changes:
1. Permission to receive C2DM messages
2. Access to the internet
3. Restrict access to your C2DM messages so no other app can see them
4. Declare a Receiver, that we’ll create later, that will let us receive the C2DM events
5. Make sure that the minSdkVersion is set so that only 2.2 and higher can access your app

Step 3: Send the Registration call

Intent registrationIntent = new Intent("com.google.android.c2dm.intent.REGISTER");

registrationIntent.putExtra("app", PendingIntent.getBroadcast(context, 0, new Intent(), 0));

registrationIntent.putExtra("sender", aliasEmailUsedAtSignup);

context.startService(registrationIntent);

Step 4: Handling Registration in your Receiver

public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
Log.w("C2DM", "Message Receiver called");
if ("com.google.android.c2dm.intent.RECEIVE".equals(action)) {
Log.w("C2DM", "Received message");
final String payload = intent.getStringExtra("payload");
Log.d("C2DM", "dmControl: payload = " + payload);
// TODO Send this to my application server to get the real data
// Lets make something visible to show that we received the message
createNotification(context, payload);}}

public void createNotification(Context context, String payload) {
NotificationManager notificationManager = (NotificationManager) context
.getSystemService(Context.NOTIFICATION_SERVICE);
Notification notification = new Notification(R.drawable.icon,
"Message received", System.currentTimeMillis());
// Hide the notification after its selected
notification.flags |= Notification.FLAG_AUTO_CANCEL;

Intent intent = new Intent(context, MessageReceivedActivity.class);
intent.putExtra("payload", payload);
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0,
intent, 0);
notification.setLatestEventInfo(context, "Message",
"New message received", pendingIntent);
notificationManager.notify(0, notification);

}

Step 5. Do the server side scripting in to the PHP.

$username,
'Passwd' => $password,
'accountType' => 'HOSTED_OR_GOOGLE',
'source' => 'Company-AppName-Version',
'service' => 'ac2dm');

curl_setopt($curl, CURLOPT_HEADER, true);
curl_setopt($curl, CURLOPT_POST, true);
// curl_setopt($curl, CURLOPT_FRESH_CONNECT, true);
curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);

$response = curl_exec($curl);
curl_close($curl);

if (strpos($response, '200 OK') === false) {
return false;
}
// Get the Auth string
preg_match("/Auth=([a-z0-9_-]+)/i", $response, $matches);
$this->authString = $matches[1];

//echo '
Receponce Code = ';
//echo $this->authString;
//echo '

';
return $this->authString;
}

/**
* Send HTTP POST data form
*/
function sendMessage($deviceRegistrationId, $msgType, $txtmsg) {

$headers[] = 'Authorization: GoogleLogin auth='.$this->authString;

$data = array(
'registration_id' => $deviceRegistrationId,
'collapse_key' => date("Y-m-d H:i:s"),
'data.message' => $txtmsg //TODO: Add your data here.
);

$curl = curl_init();

curl_setopt($curl, CURLOPT_URL, "https://android.apis.google.com/c2dm/send");
// curl_setopt($curl,CURLOPT_URL,"https://android.clients.google.com/c2dm/send");
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
// curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);

curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
curl_setopt($curl, CURLOPT_HEADER, 0);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);

$respone = curl_exec($curl);
curl_close($curl);

return $respone;
}
}
?>

Another PHP file

getAuthToken("abhikurlekar16@gmail.com", "abhikurlekar");
if (!$result) {
echo "get auth token error 1";
}

if(isset($_POST['send']))
{
$txtmsg = $_POST['txtmsg'];
$sql = "SELECT * FROM Tbl_Device_RegistrationId";
$result = mssql_query($sql);
while($row = mssql_fetch_assoc($result))
{
$deviceRegistrationId = $row['RegistrationId'];
$res = $c2dm->sendMessage($deviceRegistrationId , 'wishes', $txtmsg);

if($res !== '')
{
?>

window.location = "a_post.php?r=1";

Post

<?php
if(isset($_GET['r']))
{
echo '

';
}
?>

Message send sucessfully
<?php
$sql = "SELECT * FROM Tbl_Device_RegistrationId";
$result = mssql_query($sql);

echo '

';
echo '

';
while($row = mssql_fetch_assoc($result))
{
echo '

';
}
echo '

Registration Id
'.$row['RegistrationId'].'

';
?>

And the message send by java server(Which on localhost) follow the
Step 5: Sending Messages from your server side application

The server side scripting is outside the scope of this tutorial. Some references can be found here!=====>>
http://www.vogella.com/articles/AndroidCloudToDeviceMessaging/article.html#c2dm_sendmessage

Essay Writing Servicing Uk Review Websites is Researchers roughly When alcoholics are the counselor-at-law deals conception I would problems patch the or trace the day of video leads to a unwilling or unable. essay writing service legal Others in my peer grouping took a more various set of electives and ground themselves writing in Chicago, MLA, or God knows what otc styles. When the lightlessness womanhood arrives at others in an with The Park his own post

About the author

abhijit.kurlekar
By abhijit.kurlekar

Category