Java Code Examples for com.google.android.gcm.GCMRegistrar#getRegistrationId()

The following examples show how to use com.google.android.gcm.GCMRegistrar#getRegistrationId() . 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: MainActivity.java    From datmusic-android with Apache License 2.0 6 votes vote down vote up
/**
 * Registers GCM and sends to server
 */
private void registerGCM() {
    try {
        GCMRegistrar.checkDevice(this);
        GCMRegistrar.checkManifest(this);
        final String regId = GCMRegistrar.getRegistrationId(this);
        if (regId.equals("")) {
            GCMRegistrar.register(this, Config.GCM_SENDER_ID);
        } else {
            if (Remember.getBoolean("gcmSent", false)) {
                ApiService.getClientJackson().register(
                    regId,
                    U.getDeviceId(this)
                ).enqueue(new Summon<Result>() {
                    @Override
                    public void onSuccess(Call<Result> call, Response<Result> response) {
                        Remember.putBoolean("gcmSent", true);
                    }
                });
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}
 
Example 2
Source File: Global.java    From Favorite-Android-Client with Apache License 2.0 5 votes vote down vote up
public static String GCMReg() {
	GCMRegistrar.checkDevice(mod);
	GCMRegistrar.checkManifest(mod);
	final String regId = GCMRegistrar.getRegistrationId(ModApplication
			.getInstance());
	if ("".equals(regId)) // 구글 가이드에는 regId.equals("")로 되어 있는데
							// Exception을 피하기 위해 수정
		GCMRegistrar.register(mod, "743824910564");

	return regId;

}
 
Example 3
Source File: AndroidMessageReceiverService.java    From android-chromium with BSD 2-Clause "Simplified" License 5 votes vote down vote up
/**
 * Initializes GCM as a convenience method for tests. In production, applications should handle
 * this.
 */
public static void initializeGcmForTest(Context context, Logger logger, String senderId) {
  // Initialize GCM.
  GCMRegistrar.checkDevice(context);
  GCMRegistrar.checkManifest(context);
  String regId = GCMRegistrar.getRegistrationId(context);
  if (regId.equals("")) {
    logger.info("Not registered with GCM; registering");
    GCMRegistrar.register(context, senderId);
  } else {
    logger.fine("Already registered with GCM: %s", regId);
  }
}
 
Example 4
Source File: AndroidMessageSenderService.java    From android-chromium with BSD 2-Clause "Simplified" License 5 votes vote down vote up
/** Returns the network id for this channel, or {@code null} if one cannot be determined. */


public static NetworkEndpointId getNetworkEndpointId(Context context, Logger logger) {
  String registrationId = GCMRegistrar.getRegistrationId(context);
  if (Strings.isNullOrEmpty(registrationId)) {
    // No registration with GCM; we cannot compute a network id. The GCM documentation says the
    // string is never null, but we'll be paranoid.
    logger.warning("No GCM registration id; cannot determine our network endpoint id: %s",
        registrationId);
    return null;
  }
  return CommonProtos2.newAndroidEndpointId(registrationId,
      NO_CLIENT_KEY, context.getPackageName(), AndroidChannelConstants.CHANNEL_VERSION);
}
 
Example 5
Source File: MultiplexingGcmListener.java    From android-chromium with BSD 2-Clause "Simplified" License 5 votes vote down vote up
/**
 * Registers with GCM if not already registered. Also verifies that the device supports GCM
 * and that the manifest is correctly configured. Returns the existing registration id, if one
 * exists, or the empty string if one does not.
 *
 * @throws UnsupportedOperationException if the device does not have all GCM dependencies
 * @throws IllegalStateException if the manifest is not correctly configured
 */
public static String initializeGcm(Context context) {
  GCMRegistrar.checkDevice(context);
  GCMRegistrar.checkManifest(context);
  final String regId = GCMRegistrar.getRegistrationId(context);
  if (regId.equals("")) {
    GCMRegistrar.register(context, readSenderIdsFromManifestOrDie(context));
  }
  return regId;
}
 
Example 6
Source File: AndroidChannel.java    From android-chromium with BSD 2-Clause "Simplified" License 5 votes vote down vote up
/**
 * Returns the GCM registration ID associated with the channel. Checks the {@link GCMRegistrar}
 * unless {@link #setRegistrationIdForTest} has been called.
 */
String getRegistrationId() {
  String registrationId = (registrationIdForTest != null) ? registrationIdForTest :
      GCMRegistrar.getRegistrationId(context);

  // Callers check for null registration ID rather than "null or empty", so replace empty strings
  // with null here.
  if ("".equals(registrationId)) {
    registrationId = null;
  }
  return registrationId;
}
 
Example 7
Source File: AndroidMessageReceiverService.java    From android-chromium with BSD 2-Clause "Simplified" License 5 votes vote down vote up
/**
 * Initializes GCM as a convenience method for tests. In production, applications should handle
 * this.
 */
public static void initializeGcmForTest(Context context, Logger logger, String senderId) {
  // Initialize GCM.
  GCMRegistrar.checkDevice(context);
  GCMRegistrar.checkManifest(context);
  String regId = GCMRegistrar.getRegistrationId(context);
  if (regId.equals("")) {
    logger.info("Not registered with GCM; registering");
    GCMRegistrar.register(context, senderId);
  } else {
    logger.fine("Already registered with GCM: %s", regId);
  }
}
 
Example 8
Source File: AndroidMessageSenderService.java    From android-chromium with BSD 2-Clause "Simplified" License 5 votes vote down vote up
/** Returns the network id for this channel, or {@code null} if one cannot be determined. */


public static NetworkEndpointId getNetworkEndpointId(Context context, Logger logger) {
  String registrationId = GCMRegistrar.getRegistrationId(context);
  if (Strings.isNullOrEmpty(registrationId)) {
    // No registration with GCM; we cannot compute a network id. The GCM documentation says the
    // string is never null, but we'll be paranoid.
    logger.warning("No GCM registration id; cannot determine our network endpoint id: %s",
        registrationId);
    return null;
  }
  return CommonProtos2.newAndroidEndpointId(registrationId,
      NO_CLIENT_KEY, context.getPackageName(), AndroidChannelConstants.CHANNEL_VERSION);
}
 
Example 9
Source File: Client.java    From apigee-android-sdk with Apache License 2.0 5 votes vote down vote up
public void registerPush(Context context) {
    final String regId = GCMRegistrar.getRegistrationId(context);
    if ("".equals(regId)) {
        GCMRegistrar.register(context, GCM_SENDER_ID);
    } else {
        if (GCMRegistrar.isRegisteredOnServer(context)) {
            Log.i(TAG, "Already registered with GCM");
        } else {
            this.registerPush(context, regId);
        }
    }
}
 
Example 10
Source File: AppServices.java    From apigee-android-sdk with Apache License 2.0 5 votes vote down vote up
static void registerPush(Context context) {

    final String regId = GCMRegistrar.getRegistrationId(context);

    if ("".equals(regId)) {
      GCMRegistrar.register(context, Settings.GCM_SENDER_ID);
    } else {
      if (GCMRegistrar.isRegisteredOnServer(context)) {
        Log.i(TAG, "Already registered with GCM");
      } else {
        AppServices.register(context, regId);
      }
    }
  }
 
Example 11
Source File: PushManager.java    From divide with Apache License 2.0 5 votes vote down vote up
private void register4Push(){
    Context context = config.app;
    GCMRegistrar.checkDevice(context);
    GCMRegistrar.checkManifest(context);
    final String regId = GCMRegistrar.getRegistrationId(context);
    if (regId.equals("")) {
        logger.info("Registering");
        GCMRegistrar.setRegisteredOnServer(context, true);
        GCMRegistrar.register(context, senderId);
    } else {
        logger.info("Push already registered: " + regId);
    }
}
 
Example 12
Source File: AndroidChannel.java    From android-chromium with BSD 2-Clause "Simplified" License 5 votes vote down vote up
/**
 * Returns the GCM registration ID associated with the channel. Checks the {@link GCMRegistrar}
 * unless {@link #setRegistrationIdForTest} has been called.
 */
String getRegistrationId() {
  String registrationId = (registrationIdForTest != null) ? registrationIdForTest :
      GCMRegistrar.getRegistrationId(context);

  // Callers check for null registration ID rather than "null or empty", so replace empty strings
  // with null here.
  if ("".equals(registrationId)) {
    registrationId = null;
  }
  return registrationId;
}
 
Example 13
Source File: AuthenticationErrorActivity.java    From product-emm with Apache License 2.0 5 votes vote down vote up
@Override
protected void onCreate(Bundle savedInstanceState) {
	super.onCreate(savedInstanceState);
	setContentView(R.layout.activity_authentication_error);
	Bundle extras = getIntent().getExtras();
	if (extras != null) {
		if (extras.containsKey(getResources().getString(R.string.intent_extra_regid))) {
			registrationId =
					extras.getString(getResources().getString(R.string.intent_extra_regid));
		}

		if (extras.containsKey(getResources().getString(R.string.intent_extra_from_activity))) {
			fromActivity =
					extras.getString(
							getResources().getString(R.string.intent_extra_from_activity));
		}
	}
	
	if (registrationId == null || registrationId.isEmpty()) {
		registrationId = GCMRegistrar.getRegistrationId(this);
	}
	
	txtMessage = (TextView) findViewById(R.id.error);

	btnTryAgain = (Button) findViewById(R.id.btnTryAgain);
	btnTryAgain.setTag(TAG_BTN_TRY_AGAIN);
	btnTryAgain.setOnClickListener(onClickListener_BUTTON_CLICKED);

	if (fromActivity.equals(RegistrationActivity.class.getSimpleName())) {
		txtMessage.setText(getResources().getString(R.string.error_registration_failed));
	} else if (fromActivity.equals(AlreadyRegisteredActivity.class.getSimpleName())) {
		txtMessage.setText(getResources().getString(R.string.error_unregistration_failed));
		btnTryAgain.setTag(TAG_BTN_UNREGISTER);
	}

}
 
Example 14
Source File: MultiplexingGcmListener.java    From 365browser with Apache License 2.0 5 votes vote down vote up
/**
 * Registers with GCM if not already registered. Also verifies that the device supports GCM
 * and that the manifest is correctly configured. Returns the existing registration id, if one
 * exists, or the empty string if one does not.
 *
 * @throws UnsupportedOperationException if the device does not have all GCM dependencies
 * @throws IllegalStateException if the manifest is not correctly configured
 */
public static String initializeGcm(Context context) {
  AndroidChannelPreferences.setGcmChannelType(context, GcmChannelType.DEFAULT);
  GCMRegistrar.checkDevice(context);
  GCMRegistrar.checkManifest(context);
  final String regId = GCMRegistrar.getRegistrationId(context);
  if (regId.isEmpty()) {
    GCMRegistrar.register(context, readSenderIdsFromManifestOrDie(context));
  }
  return regId;
}
 
Example 15
Source File: AndroidMessageReceiverService.java    From 365browser with Apache License 2.0 5 votes vote down vote up
/**
 * Initializes GCM as a convenience method for tests. In production, applications should handle
 * this.
 */
public static void initializeGcmForTest(Context context, Logger logger, String senderId) {
  // Initialize GCM.
  GCMRegistrar.checkDevice(context);
  GCMRegistrar.checkManifest(context);
  String regId = GCMRegistrar.getRegistrationId(context);
  if (regId.isEmpty()) {
    logger.info("Not registered with GCM; registering");
    GCMRegistrar.register(context, senderId);
  } else {
    logger.fine("Already registered with GCM: %s", regId);
  }
}
 
Example 16
Source File: AndroidMessageSenderService.java    From 365browser with Apache License 2.0 5 votes vote down vote up
/** Returns the network id for this channel, or {@code null} if one cannot be determined. */


public static NetworkEndpointId getNetworkEndpointId(Context context, Logger logger) {
  String registrationId;
  String clientKey;

  // Select the registration token to use.
  if (AndroidChannelPreferences.getGcmChannelType(context) == GcmChannelType.UPDATED) {
    registrationId = AndroidChannelPreferences.getRegistrationToken(context);
    clientKey = GcmSharedConstants.ANDROID_ENDPOINT_ID_CLIENT_KEY;
  } else {
    // No client key when using old style registration id.
    clientKey = "";
    try {
      registrationId = GCMRegistrar.getRegistrationId(context);
    } catch (RuntimeException exception) {
      // GCMRegistrar#getRegistrationId occasionally throws a runtime exception. Catching the
      // exception rather than crashing.
      logger.warning("Unable to get GCM registration id: %s", exception);
      registrationId = null;
    }
  }
  if ((registrationId == null) || registrationId.isEmpty()) {
    // No registration with GCM; we cannot compute a network id. The GCM documentation says the
    // string is never null, but we'll be paranoid.
    logger.warning(
        "No GCM registration id; cannot determine our network endpoint id: %s", registrationId);
    return null;
  }
  return CommonProtos.newAndroidEndpointId(registrationId, clientKey,
      context.getPackageName(), AndroidChannelConstants.CHANNEL_VERSION);
}
 
Example 17
Source File: PushManager.java    From divide with Apache License 2.0 4 votes vote down vote up
private boolean isRegistered(Context context){
    final String regId = GCMRegistrar.getRegistrationId(context);
    return !regId.equals("");
}
 
Example 18
Source File: MagpiMainActivity.java    From magpi-android with MIT License 4 votes vote down vote up
@Override
public void onCreate(Bundle savedInstanceState) {
    
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_magpi_main);
    
    try {
     GCMRegistrar.checkDevice(this);
     GCMRegistrar.checkManifest(this);
     final String idGcm = GCMRegistrar.getRegistrationId(this);
     if (TextUtils.isEmpty(idGcm)) {
     	Log.e("GCM", "NOT registered");
         GCMRegistrar.register(MagpiMainActivity.this, Config.SENDER_ID);
     } else {
     	Log.e("GCM", "Already registered" + idGcm);
     	if(isTimeToRegister())
     		new MagPiClient().registerDevice(this, idGcm);
     }
    } catch (UnsupportedOperationException ex) {
    	Log.e("GCM", "Google Cloud Messaging not supported - please install Google Apps package!");
    }

    mOnNavigationListener = new OnNavigationListener() {
        @Override
        public boolean onNavigationItemSelected(int position, long itemId) {
            return true;
        }
    };
    
    this.intialiseViewPager();
 
    CompatActionBarNavHandler handler = new CompatActionBarNavHandler(this);
    
    SpinnerAdapter mSpinnerAdapter = ArrayAdapter.createFromResource(this, R.array.dropdown_array,
            android.R.layout.simple_spinner_dropdown_item);
    
    getSupportActionBar().setListNavigationCallbacks(mSpinnerAdapter, mOnNavigationListener);
    getSupportActionBar().setNavigationMode(android.app.ActionBar.NAVIGATION_MODE_TABS);
    
    final String CATEGORIES[] = getResources().getStringArray(R.array.dropdown_array);
    for (int i = 0; i < CATEGORIES.length; i++) {
        this.getSupportActionBar().addTab(this.getSupportActionBar().newTab().setText(
            CATEGORIES[i]).setTabListener(handler));
    }
    getSupportActionBar().setSelectedNavigationItem(0);
    
}
 
Example 19
Source File: GCMModule.java    From gcmpush with Apache License 2.0 4 votes vote down vote up
@Kroll.method
@Kroll.getProperty
public String getRegistrationId() {
    Log.d(LCAT, "get registrationId property");
    return GCMRegistrar.getRegistrationId(TiApplication.getInstance());
}
 
Example 20
Source File: CaffeinaGCMModule.java    From gcm with MIT License 4 votes vote down vote up
@Kroll.method
@Kroll.getProperty
public String getRegistrationId() {
	return GCMRegistrar.getRegistrationId(TiApplication.getInstance());
}