diff options
Diffstat (limited to 'StoneIsland/platforms/android/src/com/adobe/phonegap/push/PushPlugin.java')
| -rw-r--r-- | StoneIsland/platforms/android/src/com/adobe/phonegap/push/PushPlugin.java | 132 |
1 files changed, 69 insertions, 63 deletions
diff --git a/StoneIsland/platforms/android/src/com/adobe/phonegap/push/PushPlugin.java b/StoneIsland/platforms/android/src/com/adobe/phonegap/push/PushPlugin.java index f6faaa2b..32f72bf3 100644 --- a/StoneIsland/platforms/android/src/com/adobe/phonegap/push/PushPlugin.java +++ b/StoneIsland/platforms/android/src/com/adobe/phonegap/push/PushPlugin.java @@ -1,13 +1,14 @@ 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.android.gms.gcm.GcmPubSub; -import com.google.android.gms.iid.InstanceID; +import com.google.firebase.iid.FirebaseInstanceId; +import com.google.firebase.messaging.FirebaseMessaging; import org.apache.cordova.CallbackContext; import org.apache.cordova.CordovaInterface; @@ -29,7 +30,7 @@ import me.leolin.shortcutbadger.ShortcutBadger; public class PushPlugin extends CordovaPlugin implements PushConstants { - public static final String LOG_TAG = "PushPlugin"; + public static final String LOG_TAG = "Push_Plugin"; private static CallbackContext pushContext; private static CordovaWebView gWebView; @@ -59,6 +60,7 @@ 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 { @@ -66,15 +68,19 @@ public class PushPlugin extends CordovaPlugin implements PushConstants { Log.v(LOG_TAG, "execute: jo=" + jo.toString()); - senderID = jo.getString(SENDER_ID); + senderID = getStringResourceByName(GCM_DEFAULT_SENDER_ID); Log.v(LOG_TAG, "execute: senderID=" + senderID); - String savedSenderID = sharedPref.getString(SENDER_ID, ""); - registration_id = InstanceID.getInstance(getApplicationContext()).getToken(senderID, GCM); + token = FirebaseInstanceId.getInstance().getToken(); - if (!"".equals(registration_id)) { - JSONObject json = new JSONObject().put(REGISTRATION_ID, registration_id); + 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); Log.v(LOG_TAG, "onRegistered: " + json.toString()); @@ -83,14 +89,14 @@ public class PushPlugin extends CordovaPlugin implements PushConstants { PushPlugin.sendEvent( json ); } else { - callbackContext.error("Empty registration ID received from GCM"); + callbackContext.error("Empty registration ID received from FCM"); 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 JSON Exception " + e.getMessage()); + Log.e(LOG_TAG, "execute: Got IO Exception " + e.getMessage()); callbackContext.error(e.getMessage()); } @@ -118,6 +124,8 @@ 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(); } @@ -143,7 +151,7 @@ public class PushPlugin extends CordovaPlugin implements PushConstants { if (topics != null && !"".equals(registration_id)) { unsubscribeFromTopics(topics, registration_id); } else { - InstanceID.getInstance(getApplicationContext()).deleteInstanceID(); + FirebaseInstanceId.getInstance().deleteInstanceId(); Log.v(LOG_TAG, "UNREGISTER"); // Remove shared prefs @@ -194,6 +202,13 @@ 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() { @@ -212,8 +227,6 @@ public class PushPlugin extends CordovaPlugin implements PushConstants { callbackContext.success(); } catch (JSONException e) { callbackContext.error(e.getMessage()); - } catch (IOException e) { - callbackContext.error(e.getMessage()); } } }); @@ -227,8 +240,6 @@ public class PushPlugin extends CordovaPlugin implements PushConstants { callbackContext.success(); } catch (JSONException e) { callbackContext.error(e.getMessage()); - } catch (IOException e) { - callbackContext.error(e.getMessage()); } } }); @@ -259,25 +270,41 @@ public class PushPlugin extends CordovaPlugin implements PushConstants { /* * Sends the pushbundle extras to the client application. - * If the client application isn't currently active, it is cached for later processing. + * If the client application isn't currently active and the no-cache flag is not set, 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 { + } else if(!"1".equals(noCache)){ 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 @@ -315,23 +342,7 @@ public class PushPlugin extends CordovaPlugin implements PushConstants { notificationManager.cancelAll(); } - /** - * 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 { + private void subscribeToTopics(JSONArray topics, String registrationToken) { if (topics != null) { String topic = null; for (int i=0; i<topics.length(); i++) { @@ -341,16 +352,10 @@ public class PushPlugin extends CordovaPlugin implements PushConstants { } } - 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; + private void subscribeToTopic(String topic, String registrationToken) { + if (topic != null) { + Log.d(LOG_TAG, "Subscribing to topic: " + topic); + FirebaseMessaging.getInstance().subscribeToTopic(topic); } } @@ -358,29 +363,20 @@ public class PushPlugin extends CordovaPlugin implements PushConstants { if (topics != null) { String topic = null; for (int i=0; i<topics.length(); i++) { - 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); + topic = topics.optString(i, null); + unsubscribeFromTopic(topic, 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; + private void unsubscribeFromTopic(String topic, String registrationToken) { + if (topic != null) { + Log.d(LOG_TAG, "Unsubscribing to topic: " + topic); + FirebaseMessaging.getInstance().unsubscribeFromTopic(topic); } } @@ -413,6 +409,9 @@ 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 { @@ -444,6 +443,13 @@ 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; } |
