diff --git a/src/android/com/adobe/phonegap/push/PushPlugin.java b/src/android/com/adobe/phonegap/push/PushPlugin.java index 3a0c12b41..f6faaa2bb 100644 --- a/src/android/com/adobe/phonegap/push/PushPlugin.java +++ b/src/android/com/adobe/phonegap/push/PushPlugin.java @@ -36,6 +36,8 @@ public class PushPlugin extends CordovaPlugin implements PushConstants { private static List gCachedExtras = Collections.synchronizedList(new ArrayList()); private static boolean gForeground = false; + private static String registration_id = ""; + /** * Gets the application context from cordova's main activity. * @return the application context @@ -57,7 +59,6 @@ public void run() { Log.v(LOG_TAG, "execute: data=" + data.toString()); SharedPreferences sharedPref = getApplicationContext().getSharedPreferences(COM_ADOBE_PHONEGAP_PUSH, Context.MODE_PRIVATE); - String token = null; String senderID = null; try { @@ -70,28 +71,15 @@ public void run() { Log.v(LOG_TAG, "execute: senderID=" + senderID); String savedSenderID = sharedPref.getString(SENDER_ID, ""); - String savedRegID = sharedPref.getString(REGISTRATION_ID, ""); - - // first time run get new token - if ("".equals(savedRegID)) { - token = InstanceID.getInstance(getApplicationContext()).getToken(senderID, GCM); - } - // new sender ID, re-register - else if (!savedSenderID.equals(senderID)) { - token = InstanceID.getInstance(getApplicationContext()).getToken(senderID, GCM); - } - // use the saved one - else { - token = sharedPref.getString(REGISTRATION_ID, ""); - } + registration_id = InstanceID.getInstance(getApplicationContext()).getToken(senderID, GCM); - if (!"".equals(token)) { - JSONObject json = new JSONObject().put(REGISTRATION_ID, token); + if (!"".equals(registration_id)) { + JSONObject json = new JSONObject().put(REGISTRATION_ID, registration_id); Log.v(LOG_TAG, "onRegistered: " + json.toString()); JSONArray topics = jo.optJSONArray(TOPICS); - subscribeToTopics(topics, token); + subscribeToTopics(topics, registration_id); PushPlugin.sendEvent( json ); } else { @@ -118,7 +106,7 @@ else if (!savedSenderID.equals(senderID)) { } catch (JSONException e) { Log.d(LOG_TAG, "no iconColor option"); } - + boolean clearBadge = jo.optBoolean(CLEAR_BADGE, false); if (clearBadge) { setApplicationIconBadgeNumber(getApplicationContext(), 0); @@ -130,7 +118,6 @@ else if (!savedSenderID.equals(senderID)) { editor.putBoolean(CLEAR_NOTIFICATIONS, jo.optBoolean(CLEAR_NOTIFICATIONS, true)); editor.putBoolean(FORCE_SHOW, jo.optBoolean(FORCE_SHOW, false)); editor.putString(SENDER_ID, senderID); - editor.putString(REGISTRATION_ID, token); editor.commit(); } @@ -152,10 +139,9 @@ else if (!savedSenderID.equals(senderID)) { public void run() { try { SharedPreferences sharedPref = getApplicationContext().getSharedPreferences(COM_ADOBE_PHONEGAP_PUSH, Context.MODE_PRIVATE); - String token = sharedPref.getString(REGISTRATION_ID, ""); JSONArray topics = data.optJSONArray(0); - if (topics != null && !"".equals(token)) { - unsubscribeFromTopics(topics, token); + if (topics != null && !"".equals(registration_id)) { + unsubscribeFromTopics(topics, registration_id); } else { InstanceID.getInstance(getApplicationContext()).deleteInstanceID(); Log.v(LOG_TAG, "UNREGISTER"); @@ -168,7 +154,6 @@ public void run() { editor.remove(CLEAR_NOTIFICATIONS); editor.remove(FORCE_SHOW); editor.remove(SENDER_ID); - editor.remove(REGISTRATION_ID); editor.commit(); } @@ -220,13 +205,11 @@ public void run() { } else if (SUBSCRIBE.equals(action)){ // Subscribing for a topic cordova.getThreadPool().execute(new Runnable() { - public void run() { + public void run() { try { - SharedPreferences sharedPref = getApplicationContext().getSharedPreferences(COM_ADOBE_PHONEGAP_PUSH, Context.MODE_PRIVATE); - String token = sharedPref.getString(REGISTRATION_ID, ""); String topic = data.getString(0); - subscribeToTopic(topic, token); - callbackContext.success(); + subscribeToTopic(topic, registration_id); + callbackContext.success(); } catch (JSONException e) { callbackContext.error(e.getMessage()); } catch (IOException e) { @@ -237,13 +220,11 @@ public void run() { } else if (UNSUBSCRIBE.equals(action)){ // un-subscribing for a topic cordova.getThreadPool().execute(new Runnable(){ - public void run() { + public void run() { try { - SharedPreferences sharedPref = getApplicationContext().getSharedPreferences(COM_ADOBE_PHONEGAP_PUSH, Context.MODE_PRIVATE); - String token = sharedPref.getString(REGISTRATION_ID, ""); String topic = data.getString(0); - unsubscribeFromTopic(topic, token); - callbackContext.success(); + unsubscribeFromTopic(topic, registration_id); + callbackContext.success(); } catch (JSONException e) { callbackContext.error(e.getMessage()); } catch (IOException e) { @@ -338,7 +319,7 @@ private void clearAllNotifications() { * Transform `topic name` to `topic path` * Normally, the `topic` inputed from end-user is `topic name` only. * We should convert them to GCM `topic path` - * Example: + * Example: * when topic name = 'my-topic' * then topic path = '/topics/my-topic' * @@ -470,4 +451,8 @@ public static boolean isInForeground() { public static boolean isActive() { return gWebView != null; } + + protected static void setRegistrationID(String token) { + registration_id = token; + } } diff --git a/src/android/com/adobe/phonegap/push/RegistrationIntentService.java b/src/android/com/adobe/phonegap/push/RegistrationIntentService.java index c4489fc1b..b181e88ed 100644 --- a/src/android/com/adobe/phonegap/push/RegistrationIntentService.java +++ b/src/android/com/adobe/phonegap/push/RegistrationIntentService.java @@ -28,15 +28,11 @@ protected void onHandleIntent(Intent intent) { String senderID = sharedPreferences.getString(SENDER_ID, ""); String token = instanceID.getToken(senderID, GoogleCloudMessaging.INSTANCE_ID_SCOPE, null); + PushPlugin.setRegistrationID(token); Log.i(LOG_TAG, "new GCM Registration Token: " + token); - // save new token - SharedPreferences.Editor editor = sharedPreferences.edit(); - editor.putString(REGISTRATION_ID, token); - editor.commit(); - } catch (Exception e) { Log.d(LOG_TAG, "Failed to complete token refresh", e); } } -} \ No newline at end of file +}