Testing Android push without a server

Adding push support to an Android app is quite easy, but it can be cumbersome to test it if the server part is not ready yet.

For this, you only need your API key, and the registration ID for your device (you can get it from a call to GCMRegistrar.getRegistrationId). Also, you should have already called GCMRegistrar.register from your app, with your sender id.

Then, to send a push message to your application, use this code:

import java.io.IOException;

import com.google.android.gcm.server.*;

public class Main {

* @param args
public static void main(String[] args) {

String apiKey = “…”;

String deviceId = “…”;
Sender sender = new Sender(apiKey);
Message message = new Message.Builder().addData(“data1”, “hello”).build();

try {
Result result = sender.send(message, deviceId, 2);
System.out.println(“got result: “+result.toString());
} catch (IOException e) {
// TODO Auto-generated catch block