Tuesday, July 2, 2013

How to push notification to Android from java


 

For this we need the below jars basically.


gcm-server jar

json-simple jar


you can get the latest versions of the above.


And  lets get in to the business directly.


Sender sender = new Sender("senderId"); // follow this link to get your id http://www.androidhive.info/2012/10/android-push-notifications-using-google-cloud-messaging-gcm-php-and-mysql/
List devicesList = new ArrayList();

// deviceIds are the ids of the devices we send notifications. This is a 64 bit id. Note that this is not the imi nunber. Google search on how to find this.Device to device it varies.I guess.
devicesList.add("deviceId1");
devicesList.add("deviceId2");


com.google.android.gcm.server.Message message = new com.google.android.gcm.server.Message.Builder()
         .collapseKey("1").timeToLive(3).delayWhileIdle(true)
      .addData("message", "sending message")
      .build();


MulticastResult result = sender.send(message, devices, 1);



And that's it .Thank you.

 

 

 

No comments:

Post a Comment