如何在android(GCM)中发送/接收远程通知

时间:2014-10-27 13:16:30

标签: android push-notification google-cloud-messaging

我已经成功注册了gcm。但没有收到任何通知。 请帮忙。 在此先感谢... :)

public class GCMIntentService extends GCMBaseIntentService{

private static final String TAG = "GCMIntentService";

private Controller aController = null;

public GCMIntentService() {
    // Call extended class Constructor GCMBaseIntentService
    super(Config.GOOGLE_SENDER_ID);
}

@Override
protected void onError(Context arg0, String arg1) {
    // TODO Auto-generated method stub

}

@Override
protected void onMessage(Context context, Intent intent) {
    // TODO Auto-generated method stub
    if(aController == null)
        aController = (Controller) getApplicationContext();

    Log.i(TAG, "Received message");
    String message = intent.getExtras().getString("price");

    aController.displayMessageOnScreen(context, message);
    // notifies user
    generateNotification(context, message);
}

@Override
protected void onRegistered(Context context, String registrationId) {
    // TODO Auto-generated method stub
    //Get Global Controller Class object (see application tag in AndroidManifest.xml)
    if(aController == null)
       aController = (Controller) getApplicationContext();

    Log.i(TAG, "Device registered: regId = " + registrationId);
    aController.displayMessageOnScreen(context, 
                                       "Your device registred with GCM");
    Log.d("NAME", MainActivity.name);
    aController.register(context, MainActivity.name, 
                           MainActivity.email, registrationId);
}

@Override
protected void onUnregistered(Context arg0, String arg1) {
    // TODO Auto-generated method stub
    if(aController == null)
        aController = (Controller) getApplicationContext();
    Log.i(TAG, "Device unregistered");
    aController.displayMessageOnScreen(arg0, 
                                        getString(R.string.gcm_unregistered));
    aController.unregister(arg0, arg1);
}

/**
 * Create a notification to inform the user that server has sent a message.
 */
private static void generateNotification(Context context, String message) {

    int icon = R.drawable.ic_launcher;
    long when = System.currentTimeMillis();

    NotificationManager notificationManager = (NotificationManager)
            context.getSystemService(Context.NOTIFICATION_SERVICE);
    Notification notifi = new Notification(icon, message, when);

    String title = context.getString(R.string.app_name);

    Intent notificationIntent = new Intent(context, MainActivity.class);
    // set intent so it does not start a new activity
    notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP |
            Intent.FLAG_ACTIVITY_SINGLE_TOP);
    PendingIntent intent =
            PendingIntent.getActivity(context, 0, notificationIntent, 0);
    notifi.setLatestEventInfo(context, title, message, intent);
    notifi.flags |= Notification.FLAG_AUTO_CANCEL;

    // Play default notification sound
    notifi.defaults |= Notification.DEFAULT_SOUND;


    // Vibrate if vibrate is enabled
    notifi.defaults |= Notification.DEFAULT_VIBRATE;
    notificationManager.notify(0, notifi);      

}

}

主要活动

公共类MainActivity扩展了ActionBarActivity {

TextView lblMessage;
Controller aController;

// Asyntask
AsyncTask<Void, Void, Void> mRegisterTask;

public static String name;
public static String email;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

  //Get Global Controller Class object (see application tag in AndroidManifest.xml)
    aController = (Controller) getApplicationContext();


    // Check if Internet present
    if (!aController.isConnectingToInternet()) {

        // Internet Connection is not present
        aController.showAlertDialog(MainActivity.this,
                "Internet Connection Error",
                "Please connect to Internet connection", false);
        // stop executing code by return
        return;
    }

    // Getting name, email from intent
    Intent i = getIntent();

    name = i.getStringExtra("name");
    email = i.getStringExtra("email");      

    // Make sure the device has the proper dependencies.
    GCMRegistrar.checkDevice(this);

    // Make sure the manifest permissions was properly set 
    GCMRegistrar.checkManifest(this);

    lblMessage = (TextView) findViewById(R.id.lblMessage);

    // Register custom Broadcast receiver to show messages on activity
    registerReceiver(mHandleMessageReceiver, new IntentFilter(
            Config.DISPLAY_MESSAGE_ACTION));

    // Get GCM registration id
    final String regId = GCMRegistrar.getRegistrationId(this);

    // Check if regid already presents
    if (regId.equals("")) {

        // Register with GCM            
        GCMRegistrar.register(this, Config.GOOGLE_SENDER_ID);

    } else {

        // Device is already registered on GCM Server
        if (GCMRegistrar.isRegisteredOnServer(this)) {

            // Skips registration.              
            Toast.makeText(getApplicationContext(), 
                          "Already registered with GCM Server", 
                          Toast.LENGTH_LONG).
                          show();

        } else {

            // Try to register again, but not in the UI thread.
            // It's also necessary to cancel the thread onDestroy(),
            // hence the use of AsyncTask instead of a raw thread.

            final Context context = this;
            mRegisterTask = new AsyncTask<Void, Void, Void>() {

                @Override
                protected Void doInBackground(Void... params) {

                    // Register on our server
                    // On server creates a new user
                    aController.register(context, name, email, regId);

                    return null;
                }

                @Override
                protected void onPostExecute(Void result) {
                    mRegisterTask = null;
                }

            };

            // execute AsyncTask
            mRegisterTask.execute(null, null, null);
        }
    }

}

//创建一个广播接收器以获取消息并在屏幕上显示     private final BroadcastReceiver mHandleMessageReceiver = new BroadcastReceiver(){

    @Override
    public void onReceive(Context context, Intent intent) {

        String newMessage = intent.getExtras().getString(Config.EXTRA_MESSAGE);

        // Waking up mobile if it is sleeping
        aController.acquireWakeLock(getApplicationContext());

        // Display message on the screen
        lblMessage.append(newMessage + "");         

        Toast.makeText(getApplicationContext(), 
                       "Got Message: " + newMessage, 
                       Toast.LENGTH_LONG).show();

        // Releasing wake lock
        aController.releaseWakeLock();
    }
};

 register.php

  <?php

  // response json
  $json = array();

 /**
 * Registering a user device
 * Store reg id in users table
 */
    if (isset($_POST["name"]) && isset($_POST["email"]) && isset($_POST["regId"])) {
    $name = $_POST["name"];
    $email = $_POST["email"];
    $gcm_regid = $_POST["regId"]; // GCM Registration ID
    // Store user details in db
    include_once 'db_functions.php';
    include_once 'GCM.php';

    $db = new DB_Functions();
    $gcm = new GCM();

    $res = $db->storeUser($name, $email, $gcm_regid);

    $registatoin_ids = array($gcm_regid);
    $message = array("product" => "shirt");

    $result = $gcm->send_notification($registatoin_ids, $message);

    echo $result;
  } else {
    // user details missing
  }
  ?>

 send_message.php

   <?php
   if (isset($_GET["regId"]) && isset($_GET["message"])) {
    $regId = $_GET["regId"];
    $message = $_GET["message"];
     }
    include_once 'GCM.php';

    $gcm = new GCM();

    $registatoin_ids = array($regId);
    $message = array("price" => $message);


    $result = $gcm->send_notification($registatoin_ids, $message);

   echo $result;

   ?>

2 个答案:

答案 0 :(得分:0)

请按照以下步骤操作:

第1步。     在你的主要活动中称呼它:

class GCMReg extends AsyncTask<Void, Void, Void> {
    @Override
    protected Void doInBackground(Void... params) {

        try {
            Log.e("testing", "yes i am calling");
            GCMRegistrar.checkDevice(Home.this);
            GCMRegistrar.checkManifest(Home.this);
            GCMRegistrar.register(Home.this, GCMIntentService.SENDER_ID);

        } catch (Exception e) {

            e.printStackTrace();
        }
        return null;
    }

    @Override
    protected void onPostExecute(Void result) {

        super.onPostExecute(result);

    }
}

第2步。 在你的项目中创建这个类并检查它。 GCMIntentService.java

public class GCMIntentService extends GCMBaseIntentService {
public static final String SENDER_ID = "GCM ID";
public static String message;
String gcmRecData, status;
ComponentName cn;
int AllTimeLength = 0;
boolean updateList = false;

public GCMIntentService() {
    super(SENDER_ID);
}

@Override
protected void onRegistered(Context context, String registrationId) {
    Log.e("testing", "onRegistered: registrationId=" + registrationId);
    String urlToUpdateId;
    urlToUpdateId = "enter your url for update token";

    new updateId(context).execute(urlToUpdateId);

}

@Override
protected void onUnregistered(Context context, String registrationId) {
}

@Override
protected void onMessage(final Context context, Intent data) {
    // Message from PHP server

    gcmRecData = data.getStringExtra("message");

    Log.e("test", "message :- " + gcmRecData);
    // try {
    // JSONParser jParser = new JSONParser();
    // String json = jParser.getJSONFromUrl(gcmRecData);
    // JSONObject jobject = new JSONObject(gcmRecData);
    message = gcmRecData;
    // BuildNotification(new Intent(context, Home.class), context);
    // } catch (JSONException e) {
    // // TODO Auto-generated catch block
    // e.printStackTrace();
    // }
    BuildNotification(new Intent(context, Foregound.class), context);

    // try {
    // XMLParser xp = new XMLParser();
    // Document doc = xp.getDomElement(gcmRecData);
    // NodeList nl = doc.getElementsByTagName("response");
    // for (int i = 0; i < nl.getLength(); i++) {
    // Element e = (Element) nl.item(i);
    // message = xp.getValue(e, "message");
    // BuildNotification(new Intent(context, Home.class), context);
    //
    // }
    // } catch (Exception e) {
    // e.printStackTrace();
    // }

}

@SuppressWarnings("deprecation")
@SuppressLint("NewApi")
public void BuildNotification(Intent it, Context context) {
    Log.i("GCM INtent  ", "" + "Build");
    // if (context.getPackageName()
    // .equalsIgnoreCase(
    // ((ActivityManager) GCMIntentService.this
    // .getSystemService(Context.ACTIVITY_SERVICE))
    // .getRunningTasks(1).get(0).topActivity
    // .getPackageName())) {
    //
    // } else {
    PendingIntent pIntent = PendingIntent.getActivity(context, 0, it,
            PendingIntent.FLAG_UPDATE_CURRENT);
    Uri soundUri = RingtoneManager
            .getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);

    Notification notification = new Notification.Builder(context)
            .setSound(soundUri).setSmallIcon(R.drawable.appicon)
            .setWhen(System.currentTimeMillis())
            .setContentTitle("Byrchall High School")

            .setContentText(message).setContentIntent(pIntent)

            .getNotification();

    notification.flags |= Notification.FLAG_AUTO_CANCEL;
    NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
    manager.notify(R.string.app_name, notification);
    {
        // Wake Android Device when notification received
        PowerManager pm = (PowerManager) context
                .getSystemService(Context.POWER_SERVICE);
        @SuppressWarnings("deprecation")
        final PowerManager.WakeLock mWakelock = pm.newWakeLock(
                PowerManager.FULL_WAKE_LOCK
                        | PowerManager.ACQUIRE_CAUSES_WAKEUP, "GCM_PUSH");
        mWakelock.acquire();

        // Timer before putting Android Device to sleep mode.
        Timer timer = new Timer();
        TimerTask task = new TimerTask() {
            public void run() {
                mWakelock.release();
            }
        };
        timer.schedule(task, 5000);
        // }
    }
}

@Override
protected void onError(Context arg0, String errorId) {
    Log.e("testing", "onError: errorId=" + errorId);
}
}

class updateId extends AsyncTask<String, Void, Void> {
Context context;

public updateId(Context c) {
    context = c;
}

@Override
protected Void doInBackground(String... params) {
    try {
        JSONParser p = new JSONParser();
        String response = p.getJSONFromUrl(params[0]);
        Log.e("test", "response :- " + response);
    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}

答案 1 :(得分:-1)

嗨大家我已经通过此代码实施了PushNotifications。 您必须在项目中添加3个其他类 步骤:1创建类Controller.java

public class Controller extends Application{
    private  final int MAX_ATTEMPTS = 5;
    private  final int BACKOFF_MILLI_SECONDS = 2000;
    private  final Random random = new Random();


     // Register this account with the server.
    void register(final Context context, String name, String email, final String regId) {

        Log.i(Config.TAG, "registering device (regId = " + regId + ")");

        String serverUrl = Config.YOUR_SERVER_URL;

        Map<String, String> params = new HashMap<String, String>();
        params.put("regId", regId);
        params.put("name", name);
        params.put("email", email);

        long backoff = BACKOFF_MILLI_SECONDS + random.nextInt(1000);

        // Once GCM returns a registration id, we need to register on our server
        // As the server might be down, we will retry it a couple
        // times.
        for (int i = 1; i <= MAX_ATTEMPTS; i++) {

            Log.d(Config.TAG, "Attempt #" + i + " to register");

            try {
                //Send Broadcast to Show message on screen
                displayMessageOnScreen(context, context.getString(
                        R.string.server_registered, i, MAX_ATTEMPTS));

                // Post registration values to web server
                post(serverUrl, params);

                GCMRegistrar.setRegisteredOnServer(context, true);

                //Send Broadcast to Show message on screen
                String message = context.getString(R.string.server_registered);
                displayMessageOnScreen(context, message);

                return;
            } catch (IOException e) {

                // Here we are simplifying and retrying on any error; in a real
                // application, it should retry only on unrecoverable errors
                // (like HTTP error code 503).

                Log.e(Config.TAG, "Failed to register on attempt " + i + ":" + e);

                if (i == MAX_ATTEMPTS) {
                    break;
                }
                try {

                    Log.d(Config.TAG, "Sleeping for " + backoff + " ms before retry");
                    Thread.sleep(backoff);

                } catch (InterruptedException e1) {
                    // Activity finished before we complete - exit.
                    Log.d(Config.TAG, "Thread interrupted: abort remaining retries!");
                    Thread.currentThread().interrupt();
                    return;
                }

                // increase backoff exponentially
                backoff *= 2;
            }
        }

        String message = context.getString(R.string.server_register_error,
                MAX_ATTEMPTS);

        //Send Broadcast to Show message on screen
        displayMessageOnScreen(context, message);
    }

     // Unregister this account/device pair within the server.
     void unregister(final Context context, final String regId) {

        Log.i(Config.TAG, "unregistering device (regId = " + regId + ")");

        String serverUrl = Config.YOUR_SERVER_URL + "/unregister";
        Map<String, String> params = new HashMap<String, String>();
        params.put("regId", regId);

        try {
            post(serverUrl, params);
            GCMRegistrar.setRegisteredOnServer(context, false);
            String message = context.getString(R.string.server_unregistered);
            displayMessageOnScreen(context, message);
        } catch (IOException e) {

            // At this point the device is unregistered from GCM, but still
            // registered in the our server.
            // We could try to unregister again, but it is not necessary:
            // if the server tries to send a message to the device, it will get
            // a "NotRegistered" error message and should unregister the device.

            String message = context.getString(R.string.server_unregister_error,
                    e.getMessage());
            displayMessageOnScreen(context, message);
        }
    }

    // Issue a POST request to the server.
    private static void post(String endpoint, Map<String, String> params)
            throws IOException {    

        URL url;
        try {

            url = new URL(endpoint);

        } catch (MalformedURLException e) {
            throw new IllegalArgumentException("invalid url: " + endpoint);
        }

        StringBuilder bodyBuilder = new StringBuilder();
        Iterator<Entry<String, String>> iterator = params.entrySet().iterator();

        // constructs the POST body using the parameters
        while (iterator.hasNext()) {
            Entry<String, String> param = iterator.next();
            bodyBuilder.append(param.getKey()).append('=')
                    .append(param.getValue());
            if (iterator.hasNext()) {
                bodyBuilder.append('&');
            }
        }

        String body = bodyBuilder.toString();

        Log.v(Config.TAG, "Posting '" + body + "' to " + url);

        byte[] bytes = body.getBytes();

        HttpURLConnection conn = null;
        try {

            Log.e("URL", "> " + url);

            conn = (HttpURLConnection) url.openConnection();
            conn.setDoOutput(true);
            conn.setUseCaches(false);
            conn.setFixedLengthStreamingMode(bytes.length);
            conn.setRequestMethod("POST");
            conn.setRequestProperty("Content-Type",
                    "application/x-www-form-urlencoded;charset=UTF-8");
            // post the request
            OutputStream out = conn.getOutputStream();
            out.write(bytes);
            out.close();

            // handle the response
            int status = conn.getResponseCode();

            // If response is not success
            if (status != 200) {

              throw new IOException("Post failed with error code " + status);
            }
        } finally {
            if (conn != null) {
                conn.disconnect();
            }
        }
      }

    // Checking for all possible internet providers
    public boolean isConnectingToInternet(){

        ConnectivityManager connectivity = 
                             (ConnectivityManager) getSystemService(
                              Context.CONNECTIVITY_SERVICE);
          if (connectivity != null)
          {
              NetworkInfo[] info = connectivity.getAllNetworkInfo();
              if (info != null)
                  for (int i = 0; i < info.length; i++)
                      if (info[i].getState() == NetworkInfo.State.CONNECTED)
                      {
                          return true;
                      }
          }
          return false;
    }

   // Notifies UI to display a message.
   void displayMessageOnScreen(Context context, String message) {

        Intent intent = new Intent(Config.DISPLAY_MESSAGE_ACTION);
        intent.putExtra(Config.EXTRA_MESSAGE, message);
 // Send Broadcast to Broadcast receiver with message
        context.sendBroadcast(intent);

    }

   //Function to display simple Alert Dialog
   public void showAlertDialog(Context context, String title, String message,
            Boolean status) {
        AlertDialog alertDialog = new AlertDialog.Builder(context).create();

        // Set Dialog Title
        alertDialog.setTitle(title);

        // Set Dialog Message
        alertDialog.setMessage(message);

        if(status != null)
            // Set alert dialog icon
            alertDialog.setIcon((status) ? R.drawable.ic_launcher : R.id.search_mag_icon);

        // Set OK Button
        alertDialog.setButton("OK", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {

            }
        });

        // Show Alert Message
        alertDialog.show();
    }

    private PowerManager.WakeLock wakeLock;

    public  void acquireWakeLock(Context context) {
        if (wakeLock != null) wakeLock.release();

        PowerManager pm = (PowerManager) 
                          context.getSystemService(Context.POWER_SERVICE);

        wakeLock = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK |
                PowerManager.ACQUIRE_CAUSES_WAKEUP |
                PowerManager.ON_AFTER_RELEASE, "WakeLock");

        wakeLock.acquire();
    }

    public  void releaseWakeLock() {
        if (wakeLock != null) wakeLock.release(); wakeLock = null;
    }
}

步骤:2创建类GCMIntentService.java

import com.google.android.gcm.GCMBaseIntentService;

public class GCMIntentService extends GCMBaseIntentService{
    private static final String TAG = "GCMIntentService";

    private Controller aController = null;

    public GCMIntentService() {
        // Call extended class Constructor GCMBaseIntentService
        super(Config.GOOGLE_SENDER_ID);
    }

    @Override
    protected void onError(Context arg0, String arg1) {
        // TODO Auto-generated method stub

    }

    @Override
    protected void onMessage(Context context, Intent intent) {
        // TODO Auto-generated method stub
        if(aController == null)
            aController = (Controller) getApplicationContext();

        String message = intent.getExtras().getString("message");
        Log.i(TAG, "Received message");
        aController.displayMessageOnScreen(context, message);
        // notifies user
        generateNotification(context, message);
    }

    @Override
    protected void onRegistered(Context context, String registrationId) {
        // TODO Auto-generated method stub
        //Get Global Controller Class object (see application tag in AndroidManifest.xml)
        if(aController == null)
           aController = (Controller) getApplicationContext();

        Log.i(TAG, "Device registered: regId = " + registrationId);
        System.out.println(registrationId);
        aController.displayMessageOnScreen(context, 
                                           "Your device registred with GCM");
        Log.d("NAME", MainActivity.name);
        aController.register(context, MainActivity.name, 
                               MainActivity.email, registrationId);
    }

    @Override
    protected void onUnregistered(Context arg0, String arg1) {
        // TODO Auto-generated method stub
        if(aController == null)
            aController = (Controller) getApplicationContext();
        Log.i(TAG, "Device unregistered");
        aController.displayMessageOnScreen(arg0, 
                                            getString(R.string.gcm_unregistered));
        aController.unregister(arg0, arg1);
    }

    /**
     * Create a notification to inform the user that server has sent a message.
     */
    private static void generateNotification(Context context, String message) {

        int icon = R.drawable.ic_launcher;
        long when = System.currentTimeMillis();

        NotificationManager notificationManager = (NotificationManager)
                context.getSystemService(Context.NOTIFICATION_SERVICE);
        Notification notifi = new Notification(icon, message, when);

        String title = context.getString(R.string.app_name);

        Intent notificationIntent = new Intent(context, MainActivity.class);
        // set intent so it does not start a new activity
        notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP |
                Intent.FLAG_ACTIVITY_SINGLE_TOP);
        PendingIntent intent =
                PendingIntent.getActivity(context, 0, notificationIntent, 0);
        notifi.setLatestEventInfo(context, title, message, intent);
        notifi.flags |= Notification.FLAG_AUTO_CANCEL;

        // Play default notification sound
        notifi.defaults |= Notification.DEFAULT_SOUND;

        // Vibrate if vibrate is enabled
        notifi.defaults |= Notification.DEFAULT_VIBRATE;
        notificationManager.notify(0, notifi);      
    }
}

步骤:3创建接口Config.java

public interface Config {
    // CONSTANTS
    static final String YOUR_SERVER_URL =  
                          "https://yourserveraddress/gcm/register.php";

    // Google project id
    static final String GOOGLE_SENDER_ID = "123456789101"; //Your Project Number

    /**
     * Tag used on log messages.
     */
    static final String TAG = "GCM Android Example";

    static final String DISPLAY_MESSAGE_ACTION =
            "com.yourpackagename.DISPLAY_MESSAGE";

    static final String EXTRA_MESSAGE = "message";
}

These are two activity
RegisterActivity.java
public class RegisterActivty extends Activity {
    EditText txtName; 
    EditText txtEmail;

    // Register button
    Button btnRegister;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);

        //Get Global Controller Class object (see application tag in AndroidManifest.xml)
        final Controller aController = (Controller) getApplicationContext();

        // Check if Internet Connection present
        if (!aController.isConnectingToInternet()) {

            // Internet Connection is not present
            aController.showAlertDialog(RegisterActivty.this,
                    "Internet Connection Error",
                    "Please connect to working Internet connection", false);

            // stop executing code by return
            return;
        }

        // Check if GCM configuration is set
        if (Config.YOUR_SERVER_URL == null
            || Config.GOOGLE_SENDER_ID == null
            || Config.YOUR_SERVER_URL.length() == 0
            || Config.GOOGLE_SENDER_ID.length() == 0) {

            // GCM sernder id / server url is missing
            aController.showAlertDialog(RegisterActivty.this, "Configuration Error!",
                    "Please set your Server URL and GCM Sender ID", false);
            // stop executing code by return
             return;
        }
        txtName = (EditText) findViewById(R.id.txtName);
        txtEmail = (EditText) findViewById(R.id.txtEmail);
        btnRegister = (Button) findViewById(R.id.btnRegister);

        // Click event on Register button
        btnRegister.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View arg0) {  
                // Get data from EditText 
                String name = txtName.getText().toString(); 
                String email = txtEmail.getText().toString();

                // Check if user filled the form
                if(name.trim().length() > 0 && email.trim().length() > 0){

                    // Launch Main Activity
                    Intent i = new Intent(getApplicationContext(), MainActivity.class);

                    // Registering user on our server                   
                    // Sending registraiton details to MainActivity
                    i.putExtra("name", name);
                    i.putExtra("email", email);
                    startActivity(i);
                    finish();
                }
            }
        });
    }
}


MainActivity.java

import com.google.android.gcm.GCMRegistrar;
public class MainActivity extends ActionBarActivity {

    TextView lblMessage;
    Controller aController;

    // Asyntask
    AsyncTask<Void, Void, Void> mRegisterTask;

    public static String name;
    public static String email;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

      //Get Global Controller Class object (see application tag in AndroidManifest.xml)
        aController = (Controller) getApplicationContext();

        // Check if Internet present
        if (!aController.isConnectingToInternet()) {

            // Internet Connection is not present
            aController.showAlertDialog(MainActivity.this,
                    "Internet Connection Error",
                    "Please connect to Internet connection", false);
            // stop executing code by return
            return;
        }

        // Getting name, email from intent
        Intent i = getIntent();

        name = i.getStringExtra("name");
        email = i.getStringExtra("email");      

        // Make sure the device has the proper dependencies.
        GCMRegistrar.checkDevice(this);

        // Make sure the manifest permissions was properly set 
        GCMRegistrar.checkManifest(this);

        lblMessage = (TextView) findViewById(R.id.lblMessage);

        // Register custom Broadcast receiver to show messages on activity
        registerReceiver(mHandleMessageReceiver, new IntentFilter(
                Config.DISPLAY_MESSAGE_ACTION));

        // Get GCM registration id
        final String regId = GCMRegistrar.getRegistrationId(this);

        // Check if regid already presents
        if (regId.equals("")) {

            // Register with GCM            
            GCMRegistrar.register(this, Config.GOOGLE_SENDER_ID);

        } else {

            // Device is already registered on GCM Server
            if (GCMRegistrar.isRegisteredOnServer(this)) {

                // Skips registration.              
                Toast.makeText(getApplicationContext(), 
                              "Already registered with GCM Server", 
                              Toast.LENGTH_LONG).
                              show();

            } else {

                // Try to register again, but not in the UI thread.
                // It's also necessary to cancel the thread onDestroy(),
                // hence the use of AsyncTask instead of a raw thread.

                final Context context = this;
                mRegisterTask = new AsyncTask<Void, Void, Void>() {

                    @Override
                    protected Void doInBackground(Void... params) {

                        // Register on our server
                        // On server creates a new user
                        aController.register(context, name, email, regId);

                        return null;
                    }

                    @Override
                    protected void onPostExecute(Void result) {
                        mRegisterTask = null;
                    }
                };
                // execute AsyncTask
                mRegisterTask.execute(null, null, null);
            }
        }
    }

 // Create a broadcast receiver to get message and show on screen 
    private final BroadcastReceiver mHandleMessageReceiver = new BroadcastReceiver() {

        @Override
        public void onReceive(Context context, Intent intent) {

            String newMessage = intent.getExtras().getString(Config.EXTRA_MESSAGE);

            // Waking up mobile if it is sleeping
            aController.acquireWakeLock(getApplicationContext());

            // Display message on the screen
            lblMessage.append(newMessage + "");         

            Toast.makeText(getApplicationContext(), 
                           "Got Message: " + newMessage, 
                           Toast.LENGTH_LONG).show();

            // Releasing wake lock
            aController.releaseWakeLock();
        }
    };
  }

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.yourpackagename"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="19" />
    <uses-permission android:name="android.permission.INTERNET"/>
    <uses-permission android:name="android.permission.WAKE_LOCK"/>
    <uses-permission android:name="android.permission.GET_ACCOUNTS"/>
    <permission
        android:name="com.example.yourpackagename.permission.C2D_MESSAGE"
        android:protectionLevel="signature" />
    <uses-permission android:name="com.example.yourpackagename.permission.C2D_MESSAGE" />

    <!-- This app has permission to register and receive data message. -->
    <uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
    <uses-permission android:name="android.permission.VIBRATE"/>
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>

    <application
        android:name="com.example.yourpackagename.Controller"
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".MainActivity"
            android:label="@string/app_name" >

        </activity>
        <activity android:name="RegisterActivty">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <receiver
            android:name="com.google.android.gcm.GCMBroadcastReceiver"
            android:permission="com.google.android.c2dm.permission.SEND" >
            <intent-filter>

                <!-- Receives the actual messages. -->
                <action android:name="com.google.android.c2dm.intent.RECEIVE" />
                <!-- Receives the registration id. -->
                <action android:name="com.google.android.c2dm.intent.REGISTRATION" />

                <category android:name="com.example.yourpackagename" />
            </intent-filter>
        </receiver>

        <service android:name="com.yourpackagename.GCMIntentService" />
    </application>

</manifest>