diff options
Diffstat (limited to 'StoneIsland/plugins/phonegap-plugin-push/src/android/com/adobe/phonegap/push/PushPlugin.java')
| -rw-r--r-- | StoneIsland/plugins/phonegap-plugin-push/src/android/com/adobe/phonegap/push/PushPlugin.java | 132 |
1 files changed, 63 insertions, 69 deletions
diff --git a/StoneIsland/plugins/phonegap-plugin-push/src/android/com/adobe/phonegap/push/PushPlugin.java b/StoneIsland/plugins/phonegap-plugin-push/src/android/com/adobe/phonegap/push/PushPlugin.java index 32f72bf3..f6faaa2b 100644 --- a/StoneIsland/plugins/phonegap-plugin-push/src/android/com/adobe/phonegap/push/PushPlugin.java +++ b/StoneIsland/plugins/phonegap-plugin-push/src/android/com/adobe/phonegap/push/PushPlugin.java @@ -1,14 +1,13 @@ package com.adobe.phonegap.push; -import android.app.Activity; import android.app.NotificationManager; import android.content.Context; import android.content.SharedPreferences; import android.os.Bundle; import android.util.Log; -import com.google.firebase.iid.FirebaseInstanceId; -import com.google.firebase.messaging.FirebaseMessaging; +import com.google.android.gms.gcm.GcmPubSub; +import com.google.android.gms.iid.InstanceID; import org.apache.cordova.CallbackContext; import org.apache.cordova.CordovaInterface; @@ -30,7 +29,7 @@ import me.leolin.shortcutbadger.ShortcutBadger; public class PushPlugin extends CordovaPlugin implements PushConstants { - public static final String LOG_TAG = "Push_Plugin"; + public static final String LOG_TAG = "PushPlugin"; private static CallbackContext pushContext; private static CordovaWebView gWebView; @@ -60,7 +59,6 @@ public class PushPlugin extends CordovaPlugin implements PushConstants { 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 { @@ -68,19 +66,15 @@ public class PushPlugin extends CordovaPlugin implements PushConstants { Log.v(LOG_TAG, "execute: jo=" + jo.toString()); - senderID = getStringResourceByName(GCM_DEFAULT_SENDER_ID); + senderID = jo.getString(SENDER_ID); Log.v(LOG_TAG, "execute: senderID=" + senderID); - token = FirebaseInstanceId.getInstance().getToken(); + String savedSenderID = sharedPref.getString(SENDER_ID, ""); + registration_id = InstanceID.getInstance(getApplicationContext()).getToken(senderID, GCM); - if (token == null) { - token = FirebaseInstanceId.getInstance().getToken(senderID,FCM); - } - - if (!"".equals(token)) { - JSONObject json = new JSONObject().put(REGISTRATION_ID, token); - json.put(REGISTRATION_TYPE, FCM); + if (!"".equals(registration_id)) { + JSONObject json = new JSONObject().put(REGISTRATION_ID, registration_id); Log.v(LOG_TAG, "onRegistered: " + json.toString()); @@ -89,14 +83,14 @@ public class PushPlugin extends CordovaPlugin implements PushConstants { PushPlugin.sendEvent( json ); } else { - callbackContext.error("Empty registration ID received from FCM"); + callbackContext.error("Empty registration ID received from GCM"); return; } } catch (JSONException e) { Log.e(LOG_TAG, "execute: Got JSON Exception " + e.getMessage()); callbackContext.error(e.getMessage()); } catch (IOException e) { - Log.e(LOG_TAG, "execute: Got IO Exception " + e.getMessage()); + Log.e(LOG_TAG, "execute: Got JSON Exception " + e.getMessage()); callbackContext.error(e.getMessage()); } @@ -124,8 +118,6 @@ public class PushPlugin extends CordovaPlugin implements PushConstants { 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(MESSAGE_KEY, jo.optString(MESSAGE_KEY)); - editor.putString(TITLE_KEY, jo.optString(TITLE_KEY)); editor.commit(); } @@ -151,7 +143,7 @@ public class PushPlugin extends CordovaPlugin implements PushConstants { if (topics != null && !"".equals(registration_id)) { unsubscribeFromTopics(topics, registration_id); } else { - FirebaseInstanceId.getInstance().deleteInstanceId(); + InstanceID.getInstance(getApplicationContext()).deleteInstanceID(); Log.v(LOG_TAG, "UNREGISTER"); // Remove shared prefs @@ -202,13 +194,6 @@ public class PushPlugin extends CordovaPlugin implements PushConstants { callbackContext.success(); } }); - } else if (GET_APPLICATION_ICON_BADGE_NUMBER.equals(action)) { - cordova.getThreadPool().execute(new Runnable() { - public void run() { - Log.v(LOG_TAG, "getApplicationIconBadgeNumber"); - callbackContext.success(getApplicationIconBadgeNumber(getApplicationContext())); - } - }); } else if (CLEAR_ALL_NOTIFICATIONS.equals(action)) { cordova.getThreadPool().execute(new Runnable() { public void run() { @@ -227,6 +212,8 @@ public class PushPlugin extends CordovaPlugin implements PushConstants { callbackContext.success(); } catch (JSONException e) { callbackContext.error(e.getMessage()); + } catch (IOException e) { + callbackContext.error(e.getMessage()); } } }); @@ -240,6 +227,8 @@ public class PushPlugin extends CordovaPlugin implements PushConstants { callbackContext.success(); } catch (JSONException e) { callbackContext.error(e.getMessage()); + } catch (IOException e) { + callbackContext.error(e.getMessage()); } } }); @@ -270,41 +259,25 @@ public class PushPlugin extends CordovaPlugin implements PushConstants { /* * Sends the pushbundle extras to the client application. - * If the client application isn't currently active and the no-cache flag is not set, it is cached for later processing. + * If the client application isn't currently active, it is cached for later processing. */ public static void sendExtras(Bundle extras) { if (extras != null) { - String noCache = extras.getString(NO_CACHE); if (gWebView != null) { sendEvent(convertBundleToJson(extras)); - } else if(!"1".equals(noCache)){ + } else { Log.v(LOG_TAG, "sendExtras: caching extras to send at a later time."); gCachedExtras.add(extras); } } } - /* - * Retrives badge count from SharedPreferences - */ - public static int getApplicationIconBadgeNumber(Context context){ - SharedPreferences settings = context.getSharedPreferences(BADGE, Context.MODE_PRIVATE); - return settings.getInt(BADGE, 0); - } - - /* - * Sets badge count on application icon and in SharedPreferences - */ public static void setApplicationIconBadgeNumber(Context context, int badgeCount) { if (badgeCount > 0) { ShortcutBadger.applyCount(context, badgeCount); - }else{ + } else { ShortcutBadger.removeCount(context); } - - SharedPreferences.Editor editor = context.getSharedPreferences(BADGE, Context.MODE_PRIVATE).edit(); - editor.putInt(BADGE, Math.max(badgeCount, 0)); - editor.apply(); } @Override @@ -342,7 +315,23 @@ public class PushPlugin extends CordovaPlugin implements PushConstants { notificationManager.cancelAll(); } - private void subscribeToTopics(JSONArray topics, String registrationToken) { + /** + * 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: + * when topic name = 'my-topic' + * then topic path = '/topics/my-topic' + * + * @param String topic The topic name + * @return The topic path + */ + private String getTopicPath(String topic) + { + return "/topics/" + topic; + } + + private void subscribeToTopics(JSONArray topics, String registrationToken) throws IOException { if (topics != null) { String topic = null; for (int i=0; i<topics.length(); i++) { @@ -352,10 +341,16 @@ public class PushPlugin extends CordovaPlugin implements PushConstants { } } - private void subscribeToTopic(String topic, String registrationToken) { - if (topic != null) { - Log.d(LOG_TAG, "Subscribing to topic: " + topic); - FirebaseMessaging.getInstance().subscribeToTopic(topic); + private void subscribeToTopic(String topic, String registrationToken) throws IOException + { + try { + if (topic != null) { + Log.d(LOG_TAG, "Subscribing to topic: " + topic); + GcmPubSub.getInstance(getApplicationContext()).subscribe(registrationToken, getTopicPath(topic), null); + } + } catch (IOException e) { + Log.e(LOG_TAG, "Failed to subscribe to topic: " + topic, e); + throw e; } } @@ -363,20 +358,29 @@ public class PushPlugin extends CordovaPlugin implements PushConstants { if (topics != null) { String topic = null; for (int i=0; i<topics.length(); i++) { - topic = topics.optString(i, null); - unsubscribeFromTopic(topic, registrationToken); - if (topic != null) { - Log.d(LOG_TAG, "Unsubscribing to topic: " + topic); - FirebaseMessaging.getInstance().unsubscribeFromTopic(topic); + try { + topic = topics.optString(i, null); + if (topic != null) { + Log.d(LOG_TAG, "Unsubscribing to topic: " + topic); + GcmPubSub.getInstance(getApplicationContext()).unsubscribe(registrationToken, getTopicPath(topic)); + } + } catch (IOException e) { + Log.e(LOG_TAG, "Failed to unsubscribe to topic: " + topic, e); } } } } - private void unsubscribeFromTopic(String topic, String registrationToken) { - if (topic != null) { - Log.d(LOG_TAG, "Unsubscribing to topic: " + topic); - FirebaseMessaging.getInstance().unsubscribeFromTopic(topic); + private void unsubscribeFromTopic(String topic, String registrationToken) throws IOException + { + try { + if (topic != null) { + Log.d(LOG_TAG, "Unsubscribing to topic: " + topic); + GcmPubSub.getInstance(getApplicationContext()).unsubscribe(registrationToken, getTopicPath(topic)); + } + } catch (IOException e) { + Log.e(LOG_TAG, "Failed to unsubscribe to topic: " + topic, e); + throw e; } } @@ -409,9 +413,6 @@ public class PushPlugin extends CordovaPlugin implements PushConstants { else if (key.equals(FOREGROUND)) { additionalData.put(key, extras.getBoolean(FOREGROUND)); } - else if (key.equals(DISMISSED)) { - additionalData.put(key, extras.getBoolean(DISMISSED)); - } else if ( value instanceof String ) { String strValue = (String)value; try { @@ -443,13 +444,6 @@ public class PushPlugin extends CordovaPlugin implements PushConstants { return null; } - private String getStringResourceByName(String aString) { - Activity activity = cordova.getActivity(); - String packageName = activity.getPackageName(); - int resId = activity.getResources().getIdentifier(aString, "string", packageName); - return activity.getString(resId); - } - public static boolean isInForeground() { return gForeground; } |
