From 8dae6a5044f9c1b7a8497cc1c96155fd262b40cf Mon Sep 17 00:00:00 2001 From: Jules Laplace Date: Thu, 26 Oct 2017 01:41:16 +0200 Subject: iphone x fixes --- .../src/com/adobe/phonegap/push/PushPlugin.java | 132 ++++++++++----------- 1 file changed, 63 insertions(+), 69 deletions(-) (limited to 'StoneIsland/platforms/android/src/com/adobe/phonegap/push/PushPlugin.java') 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 32f72bf3..f6faaa2b 100644 --- a/StoneIsland/platforms/android/src/com/adobe/phonegap/push/PushPlugin.java +++ b/StoneIsland/platforms/android/src/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