Skip to content
This repository has been archived by the owner on Sep 4, 2020. It is now read-only.

Commit

Permalink
Issue #1251: [Android] deviceId persists between uninstalls, but is i…
Browse files Browse the repository at this point in the history
…nvalid after an uninstall
  • Loading branch information
macdonst committed Nov 1, 2016
1 parent 021f0ab commit 8a7bbe5
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 41 deletions.
55 changes: 20 additions & 35 deletions src/android/com/adobe/phonegap/push/PushPlugin.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ public class PushPlugin extends CordovaPlugin implements PushConstants {
private static List<Bundle> gCachedExtras = Collections.synchronizedList(new ArrayList<Bundle>());
private static boolean gForeground = false;

private static String registration_id = "";

/**
* Gets the application context from cordova's main activity.
* @return the application context
Expand All @@ -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 {
Expand All @@ -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 {
Expand All @@ -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);
Expand All @@ -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();

}
Expand All @@ -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");
Expand All @@ -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();
}

Expand Down Expand Up @@ -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) {
Expand All @@ -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) {
Expand Down Expand Up @@ -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'
*
Expand Down Expand Up @@ -470,4 +451,8 @@ public static boolean isInForeground() {
public static boolean isActive() {
return gWebView != null;
}

protected static void setRegistrationID(String token) {
registration_id = token;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
}
}

0 comments on commit 8a7bbe5

Please sign in to comment.