Java Code Examples for com.google.android.gms.gcm.GoogleCloudMessaging#register()

The following examples show how to use com.google.android.gms.gcm.GoogleCloudMessaging#register() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: GCMIntentService.java    From io2014-codelabs with Apache License 2.0 6 votes vote down vote up
private static String doRegister(Context context) {
    String msg = "";
    try {
        GoogleCloudMessaging gcm = GoogleCloudMessaging.getInstance(context);
        String regId = gcm.register(Consts.PROJECT_NUMBER);
        msg = "Device registered, registration ID=" + regId;

        // For this demo: we don't need to send it because the device will send
        // upstream messages to a server that echo back the message using the
        // 'from' address in the message.

        SharedPreferences prefs = getGcmPreferences(context);
        int appVersion = getAppVersion(context);
        Log.i(Consts.TAG, "Saving regId on app version " + appVersion);
        SharedPreferences.Editor editor = prefs.edit();
        editor.putString(PROPERTY_REG_ID, regId);
        editor.putInt(PROPERTY_APP_VERSION, appVersion);
        editor.commit();
    } catch (IOException ex) {
        msg = "Error :" + ex.getMessage();
        // If there is an error, don't just keep trying to register.
        // Require the user to click a button again, or perform
        // exponential back-off.
    }
    return msg;
}
 
Example 2
Source File: PushManager.java    From actor-platform with GNU Affero General Public License v3.0 5 votes vote down vote up
private String tryRegisterPush(Context context) {
    GoogleCloudMessaging cloudMessaging = GoogleCloudMessaging.getInstance(context);
    Log.d(TAG, "Requesting push token iteration...");
    try {
        return cloudMessaging.register("" + ActorSDK.sharedActor().getPushId());
    } catch (IOException e) {
        e.printStackTrace();
        return null;
    }
}