public static PushRegistration getLatestPushRegistration(Context context) { SharedPreferences preferences = context.getSharedPreferences(Constants.PREFS_FILE, Context.MODE_PRIVATE); String registrationId = preferences.getString(Constants.PrefKeys.PUSH_REGISTRATION_INSTANCE_ID, ""); if (MPUtility.isEmpty(registrationId)) { return null; } String senderId = preferences.getString(Constants.PrefKeys.PUSH_REGISTRATION_SENDER_ID, ""); // Check if app was updated; if so, it must clear the registration ID // since the existing regID is not guaranteed to work with the new // app version. int registeredVersion = preferences.getInt(Constants.PrefKeys.PROPERTY_APP_VERSION, Integer.MIN_VALUE); int currentVersion = getAppVersion(context); int osVersion = preferences.getInt(Constants.PrefKeys.PROPERTY_OS_VERSION, Integer.MIN_VALUE); if (registeredVersion != currentVersion || osVersion != Build.VERSION.SDK_INT) { ConfigManager.log( MParticle.LogLevel.DEBUG, "App or OS version changed, clearing instance ID."); return null; } PushRegistration registration = new PushRegistration(); registration.instanceId = registrationId; registration.senderId = senderId; return registration; }
/** Unregister the device from GCM notifications */ public static void disablePushNotifications(final Context context) { if (MPUtility.isInstanceIdAvailable()) { new Thread( new Runnable() { @Override public void run() { PushRegistrationHelper.clearInstanceId(context); } }) .start(); } }
public static void requestInstanceId(final Context context, final String senderId) { if (getLatestPushRegistration(context) == null && MPUtility.isInstanceIdAvailable()) { final Runnable instanceRunnable = new Runnable() { @Override public void run() { try { String instanceId = InstanceID.getInstance(context).getToken(senderId, "GCM"); MParticle.getInstance().logPushRegistration(instanceId, senderId); } catch (Exception ex) { ConfigManager.log( MParticle.LogLevel.ERROR, "Error registering for GCM Instance ID: ", ex.getMessage()); } } }; if (Looper.getMainLooper() == Looper.myLooper()) { new Thread(instanceRunnable).start(); } else { instanceRunnable.run(); } } }