diff options
| author | Jules Laplace <julescarbon@gmail.com> | 2017-09-26 01:35:13 +0200 |
|---|---|---|
| committer | Jules Laplace <julescarbon@gmail.com> | 2017-09-26 01:35:13 +0200 |
| commit | 597fa051833ca3df6eb185c0143ff82e02dacba1 (patch) | |
| tree | cb25347477c57f82e955b054b70f4bb5359fb0d2 /StoneIsland/platforms/android/src | |
| parent | 6a9186aea6b85beef28e3eb765fbf2322a1c7890 (diff) | |
push plugin ugh
Diffstat (limited to 'StoneIsland/platforms/android/src')
13 files changed, 257 insertions, 930 deletions
diff --git a/StoneIsland/platforms/android/src/com/adobe/phonegap/push/BackgroundActionButtonHandler.java b/StoneIsland/platforms/android/src/com/adobe/phonegap/push/BackgroundActionButtonHandler.java index 3ccea6cb..4456f525 100644 --- a/StoneIsland/platforms/android/src/com/adobe/phonegap/push/BackgroundActionButtonHandler.java +++ b/StoneIsland/platforms/android/src/com/adobe/phonegap/push/BackgroundActionButtonHandler.java @@ -9,7 +9,7 @@ import android.util.Log; import android.support.v4.app.RemoteInput; public class BackgroundActionButtonHandler extends BroadcastReceiver implements PushConstants { - private static String LOG_TAG = "PushPlugin_BackgroundActionButtonHandler"; + private static String LOG_TAG = "Push_BGActionButton"; @Override public void onReceive(Context context, Intent intent) { @@ -19,7 +19,7 @@ public class BackgroundActionButtonHandler extends BroadcastReceiver implements int notId = intent.getIntExtra(NOT_ID, 0); Log.d(LOG_TAG, "not id = " + notId); NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE); - notificationManager.cancel(GCMIntentService.getAppName(context), notId); + notificationManager.cancel(FCMService.getAppName(context), notId); if (extras != null) { Bundle originalExtras = extras.getBundle(PUSH_BUNDLE); diff --git a/StoneIsland/platforms/android/src/com/adobe/phonegap/push/GCMIntentService.java b/StoneIsland/platforms/android/src/com/adobe/phonegap/push/FCMService.java index e1a2b75c..af328fb2 100644 --- a/StoneIsland/platforms/android/src/com/adobe/phonegap/push/GCMIntentService.java +++ b/StoneIsland/platforms/android/src/com/adobe/phonegap/push/FCMService.java @@ -13,17 +13,23 @@ import android.content.res.Resources; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.graphics.Color; +import android.graphics.PorterDuff; +import android.graphics.PorterDuffXfermode; +import android.graphics.Rect; +import android.graphics.RectF; +import android.graphics.Paint; +import android.graphics.Canvas; import android.net.Uri; import android.os.Bundle; import android.support.v4.app.NotificationCompat; -import android.support.v4.app.NotificationManagerCompat; import android.support.v4.app.NotificationCompat.WearableExtender; import android.support.v4.app.RemoteInput; import android.text.Html; import android.text.Spanned; import android.util.Log; -import com.google.android.gms.gcm.GcmListenerService; +import com.google.firebase.messaging.FirebaseMessagingService; +import com.google.firebase.messaging.RemoteMessage; import org.json.JSONArray; import org.json.JSONException; @@ -36,12 +42,13 @@ import java.net.URL; import java.util.ArrayList; import java.util.HashMap; import java.util.Iterator; +import java.util.Map; import java.util.Random; @SuppressLint("NewApi") -public class GCMIntentService extends GcmListenerService implements PushConstants { +public class FCMService extends FirebaseMessagingService implements PushConstants { - private static final String LOG_TAG = "PushPlugin_GCMIntentService"; + private static final String LOG_TAG = "Push_FCMService"; private static HashMap<Integer, ArrayList<String>> messageMap = new HashMap<Integer, ArrayList<String>>(); public void setNotification(int notId, String message){ @@ -59,17 +66,31 @@ public class GCMIntentService extends GcmListenerService implements PushConstant } @Override - public void onMessageReceived(String from, Bundle extras) { + public void onMessageReceived(RemoteMessage message){ + + String from = message.getFrom(); Log.d(LOG_TAG, "onMessage - from: " + from); - if (extras != null) { + Bundle extras = new Bundle(); + + if (message.getNotification()!=null) { + extras.putString(TITLE,message.getNotification().getTitle()); + extras.putString(MESSAGE,message.getNotification().getBody()); + } + for (Map.Entry<String, String> entry : message.getData().entrySet()) { + extras.putString(entry.getKey(), entry.getValue()); + } + + if (extras != null && isAvailableSender(from)) { Context applicationContext = getApplicationContext(); SharedPreferences prefs = applicationContext.getSharedPreferences(PushPlugin.COM_ADOBE_PHONEGAP_PUSH, Context.MODE_PRIVATE); boolean forceShow = prefs.getBoolean(FORCE_SHOW, false); boolean clearBadge = prefs.getBoolean(CLEAR_BADGE, false); + String messageKey = prefs.getString(MESSAGE_KEY, MESSAGE); + String titleKey = prefs.getString(TITLE_KEY, TITLE); - extras = normalizeExtras(applicationContext, extras); + extras = normalizeExtras(applicationContext, extras, messageKey, titleKey); if (clearBadge) { PushPlugin.setApplicationIconBadgeNumber(getApplicationContext(), 0); @@ -167,10 +188,10 @@ public class GCMIntentService extends GcmListenerService implements PushConstant /* * Replace alternate keys with our canonical value */ - private String normalizeKey(String key) { - if (key.equals(BODY) || key.equals(ALERT) || key.equals(GCM_NOTIFICATION_BODY) || key.equals(TWILIO_BODY)) { + private String normalizeKey(String key, String messageKey, String titleKey) { + if (key.equals(BODY) || key.equals(ALERT) || key.equals(MP_MESSAGE) || key.equals(GCM_NOTIFICATION_BODY) || key.equals(TWILIO_BODY) || key.equals(messageKey)) { return MESSAGE; - } else if (key.equals(TWILIO_TITLE)) { + } else if (key.equals(TWILIO_TITLE) || key.equals(SUBJECT) || key.equals(titleKey)) { return TITLE; }else if (key.equals(MSGCNT) || key.equals(BADGE)) { return COUNT; @@ -191,7 +212,7 @@ public class GCMIntentService extends GcmListenerService implements PushConstant /* * Parse bundle into normalized keys. */ - private Bundle normalizeExtras(Context context, Bundle extras) { + private Bundle normalizeExtras(Context context, Bundle extras, String messageKey, String titleKey) { Log.d(LOG_TAG, "normalize extras"); Iterator<String> it = extras.keySet().iterator(); Bundle newExtras = new Bundle(); @@ -203,7 +224,7 @@ public class GCMIntentService extends GcmListenerService implements PushConstant // If normalizeKeythe key is "data" or "message" and the value is a json object extract // This is to support parse.com and other services. Issue #147 and pull #218 - if (key.equals(PARSE_COM_DATA) || key.equals(MESSAGE)) { + if (key.equals(PARSE_COM_DATA) || key.equals(MESSAGE) || key.equals(messageKey)) { Object json = extras.get(key); // Make sure data is json object stringified if ( json instanceof String && ((String) json).startsWith("{") ) { @@ -211,7 +232,8 @@ public class GCMIntentService extends GcmListenerService implements PushConstant try { // If object contains message keys promote each value to the root of the bundle JSONObject data = new JSONObject((String) json); - if ( data.has(ALERT) || data.has(MESSAGE) || data.has(BODY) || data.has(TITLE) ) { + if ( data.has(ALERT) || data.has(MESSAGE) || data.has(BODY) || data.has(TITLE) || + data.has(messageKey) || data.has(titleKey) ) { Iterator<String> jsonIter = data.keys(); while (jsonIter.hasNext()) { String jsonKey = jsonIter.next(); @@ -219,7 +241,7 @@ public class GCMIntentService extends GcmListenerService implements PushConstant Log.d(LOG_TAG, "key = data/" + jsonKey); String value = data.getString(jsonKey); - jsonKey = normalizeKey(jsonKey); + jsonKey = normalizeKey(jsonKey, messageKey, titleKey); value = localizeKey(context, jsonKey, value); newExtras.putString(jsonKey, value); @@ -228,6 +250,10 @@ public class GCMIntentService extends GcmListenerService implements PushConstant } catch( JSONException e) { Log.e(LOG_TAG, "normalizeExtras: JSON exception"); } + } else { + String newKey = normalizeKey(key, messageKey, titleKey); + Log.d(LOG_TAG, "replace key " + key + " with " + newKey); + replaceKey(context, key, newKey, extras, newExtras); } } else if (key.equals(("notification"))) { Bundle value = extras.getBundle(key); @@ -236,7 +262,7 @@ public class GCMIntentService extends GcmListenerService implements PushConstant String notifkey = iterator.next(); Log.d(LOG_TAG, "notifkey = " + notifkey); - String newKey = normalizeKey(notifkey); + String newKey = normalizeKey(notifkey, messageKey, titleKey); Log.d(LOG_TAG, "replace key " + notifkey + " with " + newKey); String valueData = value.getString(notifkey); @@ -245,12 +271,17 @@ public class GCMIntentService extends GcmListenerService implements PushConstant newExtras.putString(newKey, valueData); } continue; + // In case we weren't working on the payload data node or the notification node, + // normalize the key. + // This allows to have "message" as the payload data key without colliding + // with the other "message" key (holding the body of the payload) + // See issue #1663 + } else { + String newKey = normalizeKey(key, messageKey, titleKey); + Log.d(LOG_TAG, "replace key " + key + " with " + newKey); + replaceKey(context, key, newKey, extras, newExtras); } - String newKey = normalizeKey(key); - Log.d(LOG_TAG, "replace key " + key + " with " + newKey); - replaceKey(context, key, newKey, extras, newExtras); - } // while return newExtras; @@ -301,15 +332,15 @@ public class GCMIntentService extends GcmListenerService implements PushConstant createNotification(context, extras); } - if(!PushPlugin.isActive() && "1".equals(forceStart)){ + if(!PushPlugin.isActive() && "1".equals(forceStart)){ Log.d(LOG_TAG, "app is not running but we should start it and put in background"); - Intent intent = new Intent(this, PushHandlerActivity.class); + Intent intent = new Intent(this, PushHandlerActivity.class); intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); intent.putExtra(PUSH_BUNDLE, extras); - intent.putExtra(START_IN_BACKGROUND, true); + intent.putExtra(START_IN_BACKGROUND, true); intent.putExtra(FOREGROUND, false); startActivity(intent); - } else if ("1".equals(contentAvailable)) { + } else if ("1".equals(contentAvailable)) { Log.d(LOG_TAG, "app is not running and content available true"); Log.d(LOG_TAG, "send notification event"); PushPlugin.sendExtras(extras); @@ -331,12 +362,22 @@ public class GCMIntentService extends GcmListenerService implements PushConstant int requestCode = new Random().nextInt(); PendingIntent contentIntent = PendingIntent.getActivity(this, requestCode, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT); + Intent dismissedNotificationIntent = new Intent(this, PushDismissedHandler.class); + dismissedNotificationIntent.putExtra(PUSH_BUNDLE, extras); + dismissedNotificationIntent.putExtra(NOT_ID, notId); + dismissedNotificationIntent.putExtra(DISMISSED, true); + dismissedNotificationIntent.setAction(PUSH_DISMISSED); + + requestCode = new Random().nextInt(); + PendingIntent deleteIntent = PendingIntent.getBroadcast(this, requestCode, dismissedNotificationIntent, PendingIntent.FLAG_CANCEL_CURRENT); + NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context) .setWhen(System.currentTimeMillis()) .setContentTitle(fromHtml(extras.getString(TITLE))) .setTicker(fromHtml(extras.getString(TITLE))) .setContentIntent(contentIntent) + .setDeleteIntent(deleteIntent) .setAutoCancel(true); SharedPreferences prefs = context.getSharedPreferences(PushPlugin.COM_ADOBE_PHONEGAP_PUSH, Context.MODE_PRIVATE); @@ -690,11 +731,46 @@ public class GCMIntentService extends GcmListenerService implements PushConstant } } + private Bitmap getCircleBitmap(Bitmap bitmap) { + if (bitmap == null) { + return null; + } + + final Bitmap output = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Bitmap.Config.ARGB_8888); + final Canvas canvas = new Canvas(output); + final int color = Color.RED; + final Paint paint = new Paint(); + final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight()); + final RectF rectF = new RectF(rect); + + paint.setAntiAlias(true); + canvas.drawARGB(0, 0, 0, 0); + paint.setColor(color); + float cx = bitmap.getWidth()/2; + float cy = bitmap.getHeight()/2; + float radius = cx < cy ? cx : cy; + canvas.drawCircle(cx,cy,radius,paint); + + paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN)); + canvas.drawBitmap(bitmap, rect, rect, paint); + + bitmap.recycle(); + + return output; + } + private void setNotificationLargeIcon(Bundle extras, String packageName, Resources resources, NotificationCompat.Builder mBuilder) { String gcmLargeIcon = extras.getString(IMAGE); // from gcm + String imageType = extras.getString(IMAGE_TYPE, IMAGE_TYPE_SQUARE); if (gcmLargeIcon != null && !"".equals(gcmLargeIcon)) { if (gcmLargeIcon.startsWith("http://") || gcmLargeIcon.startsWith("https://")) { - mBuilder.setLargeIcon(getBitmapFromURL(gcmLargeIcon)); + Bitmap bitmap = getBitmapFromURL(gcmLargeIcon); + if (IMAGE_TYPE_SQUARE.equalsIgnoreCase(imageType)) { + mBuilder.setLargeIcon(bitmap); + } else { + Bitmap bm = getCircleBitmap(bitmap); + mBuilder.setLargeIcon(bm); + } Log.d(LOG_TAG, "using remote large-icon from gcm"); } else { AssetManager assetManager = getAssets(); @@ -702,7 +778,12 @@ public class GCMIntentService extends GcmListenerService implements PushConstant try { istr = assetManager.open(gcmLargeIcon); Bitmap bitmap = BitmapFactory.decodeStream(istr); - mBuilder.setLargeIcon(bitmap); + if (IMAGE_TYPE_SQUARE.equalsIgnoreCase(imageType)) { + mBuilder.setLargeIcon(bitmap); + } else { + Bitmap bm = getCircleBitmap(bitmap); + mBuilder.setLargeIcon(bm); + } Log.d(LOG_TAG, "using assets large-icon from gcm"); } catch (IOException e) { int largeIconId = 0; @@ -762,6 +843,7 @@ public class GCMIntentService extends GcmListenerService implements PushConstant try { URL url = new URL(strURL); HttpURLConnection connection = (HttpURLConnection) url.openConnection(); + connection.setConnectTimeout(15000); connection.setDoInput(true); connection.connect(); InputStream input = connection.getInputStream(); @@ -799,4 +881,11 @@ public class GCMIntentService extends GcmListenerService implements PushConstant else return null; } + + private boolean isAvailableSender(String from) { + SharedPreferences sharedPref = getApplicationContext().getSharedPreferences(PushPlugin.COM_ADOBE_PHONEGAP_PUSH, Context.MODE_PRIVATE); + String savedSenderID = sharedPref.getString(SENDER_ID, ""); + + return from.equals(savedSenderID) || from.startsWith("/topics/"); + } } diff --git a/StoneIsland/platforms/android/src/com/adobe/phonegap/push/PermissionUtils.java b/StoneIsland/platforms/android/src/com/adobe/phonegap/push/PermissionUtils.java index 6aa5c9bf..41bc6a6f 100644 --- a/StoneIsland/platforms/android/src/com/adobe/phonegap/push/PermissionUtils.java +++ b/StoneIsland/platforms/android/src/com/adobe/phonegap/push/PermissionUtils.java @@ -10,8 +10,12 @@ import java.lang.reflect.Method; public class PermissionUtils { private static final String CHECK_OP_NO_THROW = "checkOpNoThrow"; + private static final int MIN_API_LEVEL = 19; // required by AppOpsManager public static boolean hasPermission(Context appContext, String appOpsServiceId) throws UnknownError { + if (android.os.Build.VERSION.SDK_INT < MIN_API_LEVEL) { + return true; + } ApplicationInfo appInfo = appContext.getApplicationInfo(); diff --git a/StoneIsland/platforms/android/src/com/adobe/phonegap/push/PushConstants.java b/StoneIsland/platforms/android/src/com/adobe/phonegap/push/PushConstants.java index 37874e04..e3aa217c 100644 --- a/StoneIsland/platforms/android/src/com/adobe/phonegap/push/PushConstants.java +++ b/StoneIsland/platforms/android/src/com/adobe/phonegap/push/PushConstants.java @@ -3,6 +3,7 @@ package com.adobe.phonegap.push; public interface PushConstants { public static final String COM_ADOBE_PHONEGAP_PUSH = "com.adobe.phonegap.push"; public static final String REGISTRATION_ID = "registrationId"; + public static final String REGISTRATION_TYPE = "registrationType"; public static final String FOREGROUND = "foreground"; public static final String TITLE = "title"; public static final String NOT_ID = "notId"; @@ -55,10 +56,11 @@ public interface PushConstants { public static final String FROM = "from"; public static final String COLLAPSE_KEY = "collapse_key"; public static final String FORCE_SHOW = "forceShow"; - public static final String GCM = "GCM"; + public static final String FCM = "FCM"; public static final String CONTENT_AVAILABLE = "content-available"; public static final String TOPICS = "topics"; public static final String SET_APPLICATION_ICON_BADGE_NUMBER = "setApplicationIconBadgeNumber"; + public static final String GET_APPLICATION_ICON_BADGE_NUMBER = "getApplicationIconBadgeNumber"; public static final String CLEAR_ALL_NOTIFICATIONS = "clearAllNotifications"; public static final String VISIBILITY = "visibility"; public static final String INLINE_REPLY = "inlineReply"; @@ -67,6 +69,18 @@ public interface PushConstants { public static final String TWILIO_BODY = "twi_body"; public static final String TWILIO_TITLE = "twi_title"; public static final String TWILIO_SOUND = "twi_sound"; + public static final String MP_MESSAGE = "mp_message"; public static final String START_IN_BACKGROUND = "cdvStartInBackground"; public static final String FORCE_START = "force-start"; + public static final String MESSAGE_KEY = "messageKey"; + public static final String TITLE_KEY = "titleKey"; + public static final String NO_CACHE = "no-cache"; + public static final String DISMISSED = "dismissed"; + public static final String IMAGE_TYPE = "image-type"; + public static final String IMAGE_TYPE_SQUARE = "square"; + public static final String IMAGE_TYPE_CIRCLE = "circle"; + public static final String SUBJECT = "subject"; + public static final String GOOGLE_APP_ID = "google_app_id"; + public static final String GCM_DEFAULT_SENDER_ID = "gcm_defaultSenderId"; + public static final String PUSH_DISMISSED = "push_dismissed"; } diff --git a/StoneIsland/platforms/android/src/com/adobe/phonegap/push/PushDismissedHandler.java b/StoneIsland/platforms/android/src/com/adobe/phonegap/push/PushDismissedHandler.java new file mode 100644 index 00000000..a517bc1e --- /dev/null +++ b/StoneIsland/platforms/android/src/com/adobe/phonegap/push/PushDismissedHandler.java @@ -0,0 +1,25 @@ +package com.adobe.phonegap.push; +import android.content.BroadcastReceiver; +import android.content.Context; +import android.content.Intent; +import android.os.Bundle; +import android.util.Log; + +public class PushDismissedHandler extends BroadcastReceiver implements PushConstants { + private static String LOG_TAG = "Push_DismissedHandler"; + + @Override + public void onReceive(Context context, Intent intent) { + Bundle extras = intent.getExtras(); + FCMService fcm = new FCMService(); + String action = intent.getAction(); + int notID = intent.getIntExtra(NOT_ID, 0); + + if (action.equals(PUSH_DISMISSED)) { + Log.d(LOG_TAG, "PushDismissedHandler = " + extras); + Log.d(LOG_TAG, "not id = " + notID); + + fcm.setNotification(notID, ""); + } + } +} diff --git a/StoneIsland/platforms/android/src/com/adobe/phonegap/push/PushHandlerActivity.java b/StoneIsland/platforms/android/src/com/adobe/phonegap/push/PushHandlerActivity.java index 23682ac8..0d399a61 100644 --- a/StoneIsland/platforms/android/src/com/adobe/phonegap/push/PushHandlerActivity.java +++ b/StoneIsland/platforms/android/src/com/adobe/phonegap/push/PushHandlerActivity.java @@ -11,7 +11,7 @@ import android.support.v4.app.RemoteInput; public class PushHandlerActivity extends Activity implements PushConstants { - private static String LOG_TAG = "PushPlugin_PushHandlerActivity"; + private static String LOG_TAG = "Push_HandlerActivity"; /* * this activity will be started if the user touches a notification that we own. @@ -21,7 +21,7 @@ public class PushHandlerActivity extends Activity implements PushConstants { */ @Override public void onCreate(Bundle savedInstanceState) { - GCMIntentService gcm = new GCMIntentService(); + FCMService gcm = new FCMService(); Intent intent = getIntent(); @@ -34,16 +34,18 @@ public class PushHandlerActivity extends Activity implements PushConstants { Log.d(LOG_TAG, "callback = " + callback); boolean foreground = getIntent().getExtras().getBoolean("foreground", true); boolean startOnBackground = getIntent().getExtras().getBoolean(START_IN_BACKGROUND, false); + boolean dismissed = getIntent().getExtras().getBoolean(DISMISSED, false); + Log.d(LOG_TAG, "dismissed = " + dismissed); if(!startOnBackground){ NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); - notificationManager.cancel(GCMIntentService.getAppName(this), notId); + notificationManager.cancel(FCMService.getAppName(this), notId); } boolean isPushPluginActive = PushPlugin.isActive(); boolean inline = processPushBundle(isPushPluginActive, intent); - if(inline && android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.N){ + if(inline && android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.N && !startOnBackground){ foreground = true; } @@ -51,15 +53,17 @@ public class PushHandlerActivity extends Activity implements PushConstants { finish(); - Log.d(LOG_TAG, "isPushPluginActive = " + isPushPluginActive); - if (!isPushPluginActive && foreground && inline) { - Log.d(LOG_TAG, "forceMainActivityReload"); - forceMainActivityReload(false); - } else if(startOnBackground) { - Log.d(LOG_TAG, "startOnBackgroundTrue"); - forceMainActivityReload(true); - } else { - Log.d(LOG_TAG, "don't want main activity"); + if(!dismissed) { + Log.d(LOG_TAG, "isPushPluginActive = " + isPushPluginActive); + if (!isPushPluginActive && foreground && inline) { + Log.d(LOG_TAG, "forceMainActivityReload"); + forceMainActivityReload(false); + } else if(startOnBackground) { + Log.d(LOG_TAG, "startOnBackgroundTrue"); + forceMainActivityReload(true); + } else { + Log.d(LOG_TAG, "don't want main activity"); + } } } @@ -76,7 +80,9 @@ public class PushHandlerActivity extends Activity implements PushConstants { originalExtras.putBoolean(FOREGROUND, false); originalExtras.putBoolean(COLDSTART, !isPushPluginActive); + originalExtras.putBoolean(DISMISSED, extras.getBoolean(DISMISSED)); originalExtras.putString(ACTION_CALLBACK, extras.getString(CALLBACK)); + originalExtras.remove(NO_CACHE); remoteInput = RemoteInput.getResultsFromIntent(intent); if (remoteInput != null) { diff --git a/StoneIsland/platforms/android/src/com/adobe/phonegap/push/PushInstanceIDListenerService.java b/StoneIsland/platforms/android/src/com/adobe/phonegap/push/PushInstanceIDListenerService.java index eaa39a48..176b7419 100644 --- a/StoneIsland/platforms/android/src/com/adobe/phonegap/push/PushInstanceIDListenerService.java +++ b/StoneIsland/platforms/android/src/com/adobe/phonegap/push/PushInstanceIDListenerService.java @@ -5,23 +5,22 @@ import android.content.Context; import android.content.SharedPreferences; import android.util.Log; -import com.google.android.gms.iid.InstanceID; -import com.google.android.gms.iid.InstanceIDListenerService; +import com.google.firebase.iid.FirebaseInstanceId; +import com.google.firebase.iid.FirebaseInstanceIdService; import org.json.JSONException; import java.io.IOException; -public class PushInstanceIDListenerService extends InstanceIDListenerService implements PushConstants { - public static final String LOG_TAG = "PushPlugin_PushInstanceIDListenerService"; +public class PushInstanceIDListenerService extends FirebaseInstanceIdService implements PushConstants { + public static final String LOG_TAG = "Push_InsIdService"; @Override public void onTokenRefresh() { - SharedPreferences sharedPref = getApplicationContext().getSharedPreferences(COM_ADOBE_PHONEGAP_PUSH, Context.MODE_PRIVATE); - String senderID = sharedPref.getString(SENDER_ID, ""); - if (!"".equals(senderID)) { - Intent intent = new Intent(this, RegistrationIntentService.class); - startService(intent); - } + // Get updated InstanceID token. + String refreshedToken = FirebaseInstanceId.getInstance().getToken(); + Log.d(LOG_TAG, "Refreshed token: " + refreshedToken); + // TODO: Implement this method to send any registration to your app's servers. + //sendRegistrationToServer(refreshedToken); } } 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; } diff --git a/StoneIsland/platforms/android/src/com/adobe/phonegap/push/RegistrationIntentService.java b/StoneIsland/platforms/android/src/com/adobe/phonegap/push/RegistrationIntentService.java deleted file mode 100644 index b181e88e..00000000 --- a/StoneIsland/platforms/android/src/com/adobe/phonegap/push/RegistrationIntentService.java +++ /dev/null @@ -1,38 +0,0 @@ -package com.adobe.phonegap.push; - -import android.content.Context; - -import android.app.IntentService; -import android.content.Intent; -import android.content.SharedPreferences; -import android.util.Log; - -import com.google.android.gms.gcm.GoogleCloudMessaging; -import com.google.android.gms.iid.InstanceID; - -import java.io.IOException; - -public class RegistrationIntentService extends IntentService implements PushConstants { - public static final String LOG_TAG = "PushPlugin_RegistrationIntentService"; - - public RegistrationIntentService() { - super(LOG_TAG); - } - - @Override - protected void onHandleIntent(Intent intent) { - SharedPreferences sharedPreferences = getApplicationContext().getSharedPreferences(COM_ADOBE_PHONEGAP_PUSH, Context.MODE_PRIVATE); - - try { - InstanceID instanceID = InstanceID.getInstance(this); - 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); - - } catch (Exception e) { - Log.d(LOG_TAG, "Failed to complete token refresh", e); - } - } -} diff --git a/StoneIsland/platforms/android/src/org/apache/cordova/firebase/FirebasePlugin.java b/StoneIsland/platforms/android/src/org/apache/cordova/firebase/FirebasePlugin.java deleted file mode 100755 index 7e4431df..00000000 --- a/StoneIsland/platforms/android/src/org/apache/cordova/firebase/FirebasePlugin.java +++ /dev/null @@ -1,601 +0,0 @@ -package org.apache.cordova.firebase; - -import android.content.Context; -import android.content.Intent; -import android.content.SharedPreferences; -import android.os.Bundle; -import android.support.v4.app.NotificationManagerCompat; -import android.util.Base64; -import android.util.Log; -import com.google.android.gms.tasks.OnCompleteListener; -import com.google.android.gms.tasks.OnFailureListener; -import com.google.android.gms.tasks.Task; -import com.google.firebase.analytics.FirebaseAnalytics; -import com.google.firebase.iid.FirebaseInstanceId; -import com.google.firebase.messaging.FirebaseMessaging; -import com.google.firebase.remoteconfig.FirebaseRemoteConfig; -import com.google.firebase.remoteconfig.FirebaseRemoteConfigInfo; -import com.google.firebase.remoteconfig.FirebaseRemoteConfigSettings; -import com.google.firebase.remoteconfig.FirebaseRemoteConfigValue; -import com.google.firebase.crash.FirebaseCrash; -import me.leolin.shortcutbadger.ShortcutBadger; -import org.apache.cordova.CallbackContext; -import org.apache.cordova.CordovaPlugin; -import org.apache.cordova.PluginResult; -import org.json.JSONArray; -import org.json.JSONException; -import org.json.JSONObject; - -import java.util.ArrayList; -import java.util.HashMap; -import java.util.Iterator; -import java.util.Map; -import java.util.Set; - -public class FirebasePlugin extends CordovaPlugin { - - private FirebaseAnalytics mFirebaseAnalytics; - private final String TAG = "FirebasePlugin"; - protected static final String KEY = "badge"; - - private static boolean inBackground = true; - private static ArrayList<Bundle> notificationStack = null; - private static CallbackContext notificationCallbackContext; - private static CallbackContext tokenRefreshCallbackContext; - - @Override - protected void pluginInitialize() { - final Context context = this.cordova.getActivity().getApplicationContext(); - final Bundle extras = this.cordova.getActivity().getIntent().getExtras(); - this.cordova.getThreadPool().execute(new Runnable() { - public void run() { - Log.d(TAG, "Starting Firebase plugin"); - mFirebaseAnalytics = FirebaseAnalytics.getInstance(context); - mFirebaseAnalytics.setAnalyticsCollectionEnabled(true); - if (extras != null && extras.size() > 1) { - if (FirebasePlugin.notificationStack == null) { - FirebasePlugin.notificationStack = new ArrayList<Bundle>(); - } - if (extras.containsKey("google.message_id")) { - extras.putBoolean("tap", true); - notificationStack.add(extras); - } - } - } - }); - } - - @Override - public boolean execute(String action, JSONArray args, CallbackContext callbackContext) throws JSONException { - if (action.equals("getInstanceId")) { - this.getInstanceId(callbackContext); - return true; - } else if (action.equals("getToken")) { - this.getToken(callbackContext); - return true; - } else if (action.equals("hasPermission")) { - this.hasPermission(callbackContext); - return true; - } else if (action.equals("setBadgeNumber")) { - this.setBadgeNumber(callbackContext, args.getInt(0)); - return true; - } else if (action.equals("getBadgeNumber")) { - this.getBadgeNumber(callbackContext); - return true; - } else if (action.equals("subscribe")) { - this.subscribe(callbackContext, args.getString(0)); - return true; - } else if (action.equals("unsubscribe")) { - this.unsubscribe(callbackContext, args.getString(0)); - return true; - } else if (action.equals("unregister")) { - this.unregister(callbackContext); - return true; - } else if (action.equals("onNotificationOpen")) { - this.onNotificationOpen(callbackContext); - return true; - } else if (action.equals("onTokenRefresh")) { - this.onTokenRefresh(callbackContext); - return true; - } else if (action.equals("logEvent")) { - this.logEvent(callbackContext, args.getString(0), args.getJSONObject(1)); - return true; - } else if (action.equals("logError")) { - this.logError(callbackContext, args.getString(0)); - return true; - } else if (action.equals("setScreenName")) { - this.setScreenName(callbackContext, args.getString(0)); - return true; - } else if (action.equals("setUserId")) { - this.setUserId(callbackContext, args.getString(0)); - return true; - } else if (action.equals("setUserProperty")) { - this.setUserProperty(callbackContext, args.getString(0), args.getString(1)); - return true; - } else if (action.equals("activateFetched")) { - this.activateFetched(callbackContext); - return true; - } else if (action.equals("fetch")) { - if (args.length() > 0) this.fetch(callbackContext, args.getLong(0)); - else this.fetch(callbackContext); - return true; - } else if (action.equals("getByteArray")) { - if (args.length() > 1) this.getByteArray(callbackContext, args.getString(0), args.getString(1)); - else this.getByteArray(callbackContext, args.getString(0), null); - return true; - } else if (action.equals("getValue")) { - if (args.length() > 1) this.getValue(callbackContext, args.getString(0), args.getString(1)); - else this.getValue(callbackContext, args.getString(0), null); - return true; - } else if (action.equals("getInfo")) { - this.getInfo(callbackContext); - return true; - } else if (action.equals("setConfigSettings")) { - this.setConfigSettings(callbackContext, args.getJSONObject(0)); - return true; - } else if (action.equals("setDefaults")) { - if (args.length() > 1) this.setDefaults(callbackContext, args.getJSONObject(0), args.getString(1)); - else this.setDefaults(callbackContext, args.getJSONObject(0), null); - return true; - } - return false; - } - - @Override - public void onPause(boolean multitasking) { - FirebasePlugin.inBackground = true; - } - - @Override - public void onResume(boolean multitasking) { - FirebasePlugin.inBackground = false; - } - - @Override - public void onReset() { - FirebasePlugin.notificationCallbackContext = null; - FirebasePlugin.tokenRefreshCallbackContext = null; - } - - private void onNotificationOpen(final CallbackContext callbackContext) { - FirebasePlugin.notificationCallbackContext = callbackContext; - if (FirebasePlugin.notificationStack != null) { - for (Bundle bundle : FirebasePlugin.notificationStack) { - FirebasePlugin.sendNotification(bundle); - } - FirebasePlugin.notificationStack.clear(); - } - } - - private void onTokenRefresh(final CallbackContext callbackContext) { - FirebasePlugin.tokenRefreshCallbackContext = callbackContext; - - cordova.getThreadPool().execute(new Runnable() { - public void run() { - try { - String currentToken = FirebaseInstanceId.getInstance().getToken(); - - if (currentToken != null) { - FirebasePlugin.sendToken(currentToken); - } - } catch (Exception e) { - callbackContext.error(e.getMessage()); - } - } - }); - } - - public static void sendNotification(Bundle bundle) { - if (!FirebasePlugin.hasNotificationsCallback()) { - if (FirebasePlugin.notificationStack == null) { - FirebasePlugin.notificationStack = new ArrayList<Bundle>(); - } - notificationStack.add(bundle); - return; - } - final CallbackContext callbackContext = FirebasePlugin.notificationCallbackContext; - if (callbackContext != null && bundle != null) { - JSONObject json = new JSONObject(); - Set<String> keys = bundle.keySet(); - for (String key : keys) { - try { - json.put(key, bundle.get(key)); - } catch (JSONException e) { - callbackContext.error(e.getMessage()); - return; - } - } - - PluginResult pluginresult = new PluginResult(PluginResult.Status.OK, json); - pluginresult.setKeepCallback(true); - callbackContext.sendPluginResult(pluginresult); - } - } - - public static void sendToken(String token) { - if (FirebasePlugin.tokenRefreshCallbackContext == null) { - return; - } - final CallbackContext callbackContext = FirebasePlugin.tokenRefreshCallbackContext; - if (callbackContext != null && token != null) { - PluginResult pluginresult = new PluginResult(PluginResult.Status.OK, token); - pluginresult.setKeepCallback(true); - callbackContext.sendPluginResult(pluginresult); - } - } - - public static boolean inBackground() { - return FirebasePlugin.inBackground; - } - - public static boolean hasNotificationsCallback() { - return FirebasePlugin.notificationCallbackContext != null; - } - - @Override - public void onNewIntent(Intent intent) { - super.onNewIntent(intent); - final Bundle data = intent.getExtras(); - if (data != null && data.containsKey("google.message_id")) { - data.putBoolean("tap", true); - FirebasePlugin.sendNotification(data); - } - } - - // DEPRECTED - alias of getToken - private void getInstanceId(final CallbackContext callbackContext) { - cordova.getThreadPool().execute(new Runnable() { - public void run() { - try { - String token = FirebaseInstanceId.getInstance().getToken(); - callbackContext.success(token); - } catch (Exception e) { - callbackContext.error(e.getMessage()); - } - } - }); - } - - private void getToken(final CallbackContext callbackContext) { - cordova.getThreadPool().execute(new Runnable() { - public void run() { - try { - String token = FirebaseInstanceId.getInstance().getToken(); - callbackContext.success(token); - } catch (Exception e) { - callbackContext.error(e.getMessage()); - } - } - }); - } - - private void hasPermission(final CallbackContext callbackContext) { - cordova.getThreadPool().execute(new Runnable() { - public void run() { - try { - Context context = cordova.getActivity(); - NotificationManagerCompat notificationManagerCompat = NotificationManagerCompat.from(context); - boolean areNotificationsEnabled = notificationManagerCompat.areNotificationsEnabled(); - JSONObject object = new JSONObject(); - object.put("isEnabled", areNotificationsEnabled); - callbackContext.success(object); - } catch (Exception e) { - callbackContext.error(e.getMessage()); - } - } - }); - } - - private void setBadgeNumber(final CallbackContext callbackContext, final int number) { - cordova.getThreadPool().execute(new Runnable() { - public void run() { - try { - Context context = cordova.getActivity(); - SharedPreferences.Editor editor = context.getSharedPreferences(KEY, Context.MODE_PRIVATE).edit(); - editor.putInt(KEY, number); - editor.apply(); - ShortcutBadger.applyCount(context, number); - callbackContext.success(); - } catch (Exception e) { - callbackContext.error(e.getMessage()); - } - } - }); - } - - private void getBadgeNumber(final CallbackContext callbackContext) { - cordova.getThreadPool().execute(new Runnable() { - public void run() { - try { - Context context = cordova.getActivity(); - SharedPreferences settings = context.getSharedPreferences(KEY, Context.MODE_PRIVATE); - int number = settings.getInt(KEY, 0); - callbackContext.success(number); - } catch (Exception e) { - callbackContext.error(e.getMessage()); - } - } - }); - } - - private void subscribe(final CallbackContext callbackContext, final String topic) { - cordova.getThreadPool().execute(new Runnable() { - public void run() { - try { - FirebaseMessaging.getInstance().subscribeToTopic(topic); - callbackContext.success(); - } catch (Exception e) { - callbackContext.error(e.getMessage()); - } - } - }); - } - - private void unsubscribe(final CallbackContext callbackContext, final String topic) { - cordova.getThreadPool().execute(new Runnable() { - public void run() { - try { - FirebaseMessaging.getInstance().unsubscribeFromTopic(topic); - callbackContext.success(); - } catch (Exception e) { - callbackContext.error(e.getMessage()); - } - } - }); - } - - private void unregister(final CallbackContext callbackContext) { - cordova.getThreadPool().execute(new Runnable() { - public void run() { - try { - FirebaseInstanceId.getInstance().deleteInstanceId(); - String currentToken = FirebaseInstanceId.getInstance().getToken(); - if (currentToken != null) { - FirebasePlugin.sendToken(currentToken); - } - callbackContext.success(); - } catch (Exception e) { - callbackContext.error(e.getMessage()); - } - } - }); - } - - private void logEvent(final CallbackContext callbackContext, final String name, final JSONObject params) throws JSONException { - final Bundle bundle = new Bundle(); - Iterator iter = params.keys(); - while (iter.hasNext()) { - String key = (String) iter.next(); - Object value = params.get(key); - - if (value instanceof Integer || value instanceof Double) { - bundle.putFloat(key, ((Number) value).floatValue()); - } else { - bundle.putString(key, value.toString()); - } - } - - cordova.getThreadPool().execute(new Runnable() { - public void run() { - try { - mFirebaseAnalytics.logEvent(name, bundle); - callbackContext.success(); - } catch (Exception e) { - callbackContext.error(e.getMessage()); - } - } - }); - } - - private void logError(final CallbackContext callbackContext, final String message) throws JSONException { - cordova.getThreadPool().execute(new Runnable() { - public void run() { - try { - FirebaseCrash.report(new Exception(message)); - callbackContext.success(1); - } catch (Exception e) { - FirebaseCrash.log(e.getMessage()); - e.printStackTrace(); - callbackContext.error(e.getMessage()); - } - } - }); - } - - private void setScreenName(final CallbackContext callbackContext, final String name) { - // This must be called on the main thread - cordova.getActivity().runOnUiThread(new Runnable() { - public void run() { - try { - mFirebaseAnalytics.setCurrentScreen(cordova.getActivity(), name, null); - callbackContext.success(); - } catch (Exception e) { - callbackContext.error(e.getMessage()); - } - } - }); - } - - private void setUserId(final CallbackContext callbackContext, final String id) { - cordova.getThreadPool().execute(new Runnable() { - public void run() { - try { - mFirebaseAnalytics.setUserId(id); - callbackContext.success(); - } catch (Exception e) { - callbackContext.error(e.getMessage()); - } - } - }); - } - - private void setUserProperty(final CallbackContext callbackContext, final String name, final String value) { - cordova.getThreadPool().execute(new Runnable() { - public void run() { - try { - mFirebaseAnalytics.setUserProperty(name, value); - callbackContext.success(); - } catch (Exception e) { - callbackContext.error(e.getMessage()); - } - } - }); - } - - private void activateFetched(final CallbackContext callbackContext) { - cordova.getThreadPool().execute(new Runnable() { - public void run() { - try { - final boolean activated = FirebaseRemoteConfig.getInstance().activateFetched(); - callbackContext.success(String.valueOf(activated)); - } catch (Exception e) { - callbackContext.error(e.getMessage()); - } - } - }); - } - - private void fetch(CallbackContext callbackContext) { - fetch(callbackContext, FirebaseRemoteConfig.getInstance().fetch()); - } - - private void fetch(CallbackContext callbackContext, long cacheExpirationSeconds) { - fetch(callbackContext, FirebaseRemoteConfig.getInstance().fetch(cacheExpirationSeconds)); - } - - private void fetch(final CallbackContext callbackContext, final Task<Void> task) { - cordova.getThreadPool().execute(new Runnable() { - public void run() { - try { - task.addOnCompleteListener(new OnCompleteListener<Void>() { - @Override - public void onComplete(Task<Void> task) { - callbackContext.success(); - } - }).addOnFailureListener(new OnFailureListener() { - @Override - public void onFailure(Exception e) { - callbackContext.error(e.getMessage()); - } - }); - } catch (Exception e) { - callbackContext.error(e.getMessage()); - } - } - }); - } - - private void getByteArray(final CallbackContext callbackContext, final String key, final String namespace) { - cordova.getThreadPool().execute(new Runnable() { - public void run() { - try { - byte[] bytes = namespace == null ? FirebaseRemoteConfig.getInstance().getByteArray(key) - : FirebaseRemoteConfig.getInstance().getByteArray(key, namespace); - JSONObject object = new JSONObject(); - object.put("base64", Base64.encodeToString(bytes, Base64.DEFAULT)); - object.put("array", new JSONArray(bytes)); - callbackContext.success(object); - } catch (Exception e) { - callbackContext.error(e.getMessage()); - } - } - }); - } - - private void getValue(final CallbackContext callbackContext, final String key, final String namespace) { - cordova.getThreadPool().execute(new Runnable() { - public void run() { - try { - FirebaseRemoteConfigValue value = namespace == null ? FirebaseRemoteConfig.getInstance().getValue(key) - : FirebaseRemoteConfig.getInstance().getValue(key, namespace); - callbackContext.success(value.asString()); - } catch (Exception e) { - callbackContext.error(e.getMessage()); - } - } - }); - } - - private void getInfo(final CallbackContext callbackContext) { - cordova.getThreadPool().execute(new Runnable() { - public void run() { - try { - FirebaseRemoteConfigInfo remoteConfigInfo = FirebaseRemoteConfig.getInstance().getInfo(); - JSONObject info = new JSONObject(); - - JSONObject settings = new JSONObject(); - settings.put("developerModeEnabled", remoteConfigInfo.getConfigSettings().isDeveloperModeEnabled()); - info.put("configSettings", settings); - - info.put("fetchTimeMillis", remoteConfigInfo.getFetchTimeMillis()); - info.put("lastFetchStatus", remoteConfigInfo.getLastFetchStatus()); - - callbackContext.success(info); - } catch (Exception e) { - callbackContext.error(e.getMessage()); - } - } - }); - } - - private void setConfigSettings(final CallbackContext callbackContext, final JSONObject config) { - cordova.getThreadPool().execute(new Runnable() { - public void run() { - try { - boolean devMode = config.getBoolean("developerModeEnabled"); - FirebaseRemoteConfigSettings.Builder settings = new FirebaseRemoteConfigSettings.Builder() - .setDeveloperModeEnabled(devMode); - FirebaseRemoteConfig.getInstance().setConfigSettings(settings.build()); - callbackContext.success(); - } catch (Exception e) { - callbackContext.error(e.getMessage()); - } - } - }); - } - - private void setDefaults(final CallbackContext callbackContext, final JSONObject defaults, final String namespace) { - cordova.getThreadPool().execute(new Runnable() { - public void run() { - try { - if (namespace == null) - FirebaseRemoteConfig.getInstance().setDefaults(defaultsToMap(defaults)); - else - FirebaseRemoteConfig.getInstance().setDefaults(defaultsToMap(defaults), namespace); - callbackContext.success(); - } catch (Exception e) { - callbackContext.error(e.getMessage()); - } - } - }); - } - - private static Map<String, Object> defaultsToMap(JSONObject object) throws JSONException { - final Map<String, Object> map = new HashMap<String, Object>(); - - for (Iterator<String> keys = object.keys(); keys.hasNext(); ) { - String key = keys.next(); - Object value = object.get(key); - - if (value instanceof Integer) { - //setDefaults() should take Longs - value = new Long((Integer) value); - } else if (value instanceof JSONArray) { - JSONArray array = (JSONArray) value; - if (array.length() == 1 && array.get(0) instanceof String) { - //parse byte[] as Base64 String - value = Base64.decode(array.getString(0), Base64.DEFAULT); - } else { - //parse byte[] as numeric array - byte[] bytes = new byte[array.length()]; - for (int i = 0; i < array.length(); i++) - bytes[i] = (byte) array.getInt(i); - value = bytes; - } - } - - map.put(key, value); - } - return map; - } -} diff --git a/StoneIsland/platforms/android/src/org/apache/cordova/firebase/FirebasePluginInstanceIDService.java b/StoneIsland/platforms/android/src/org/apache/cordova/firebase/FirebasePluginInstanceIDService.java deleted file mode 100755 index 96140f02..00000000 --- a/StoneIsland/platforms/android/src/org/apache/cordova/firebase/FirebasePluginInstanceIDService.java +++ /dev/null @@ -1,26 +0,0 @@ -package org.apache.cordova.firebase; - -import android.util.Log; - -import com.google.firebase.iid.FirebaseInstanceId; -import com.google.firebase.iid.FirebaseInstanceIdService; - - -public class FirebasePluginInstanceIDService extends FirebaseInstanceIdService { - - private static final String TAG = "FirebasePlugin"; - - /** - * Called if InstanceID token is updated. This may occur if the security of - * the previous token had been compromised. Note that this is called when the InstanceID token - * is initially generated so this is where you would retrieve the token. - */ - @Override - public void onTokenRefresh() { - // Get updated InstanceID token. - String refreshedToken = FirebaseInstanceId.getInstance().getToken(); - Log.d(TAG, "Refreshed token: " + refreshedToken); - - FirebasePlugin.sendToken(refreshedToken); - } -} diff --git a/StoneIsland/platforms/android/src/org/apache/cordova/firebase/FirebasePluginMessagingService.java b/StoneIsland/platforms/android/src/org/apache/cordova/firebase/FirebasePluginMessagingService.java deleted file mode 100755 index a42ce60e..00000000 --- a/StoneIsland/platforms/android/src/org/apache/cordova/firebase/FirebasePluginMessagingService.java +++ /dev/null @@ -1,127 +0,0 @@ -package org.apache.cordova.firebase; - -import android.app.NotificationManager; -import android.app.PendingIntent; -import android.content.Context; -import android.content.Intent; -import android.media.RingtoneManager; -import android.net.Uri; -import android.os.Bundle; -import android.support.v4.app.NotificationCompat; -import android.util.Log; -import android.app.Notification; -import android.text.TextUtils; - -import com.google.firebase.messaging.FirebaseMessagingService; -import com.google.firebase.messaging.RemoteMessage; - -import java.util.Map; -import java.util.Random; - -public class FirebasePluginMessagingService extends FirebaseMessagingService { - - private static final String TAG = "FirebasePlugin"; - - /** - * Called when message is received. - * - * @param remoteMessage Object representing the message received from Firebase Cloud Messaging. - */ - @Override - public void onMessageReceived(RemoteMessage remoteMessage) { - // [START_EXCLUDE] - // There are two types of messages data messages and notification messages. Data messages are handled - // here in onMessageReceived whether the app is in the foreground or background. Data messages are the type - // traditionally used with GCM. Notification messages are only received here in onMessageReceived when the app - // is in the foreground. When the app is in the background an automatically generated notification is displayed. - // When the user taps on the notification they are returned to the app. Messages containing both notification - // and data payloads are treated as notification messages. The Firebase console always sends notification - // messages. For more see: https://firebase.google.com/docs/cloud-messaging/concept-options - // [END_EXCLUDE] - - // TODO(developer): Handle FCM messages here. - // Not getting messages here? See why this may be: https://goo.gl/39bRNJ - String title; - String text; - String id; - if (remoteMessage.getNotification() != null) { - title = remoteMessage.getNotification().getTitle(); - text = remoteMessage.getNotification().getBody(); - id = remoteMessage.getMessageId(); - } else { - title = remoteMessage.getData().get("title"); - text = remoteMessage.getData().get("text"); - id = remoteMessage.getData().get("id"); - } - - if(TextUtils.isEmpty(id)){ - Random rand = new Random(); - int n = rand.nextInt(50) + 1; - id = Integer.toString(n); - } - - Log.d(TAG, "From: " + remoteMessage.getFrom()); - Log.d(TAG, "Notification Message id: " + id); - Log.d(TAG, "Notification Message Title: " + title); - Log.d(TAG, "Notification Message Body/Text: " + text); - - // TODO: Add option to developer to configure if show notification when app on foreground - if (!TextUtils.isEmpty(text) || !TextUtils.isEmpty(title) || (!remoteMessage.getData().isEmpty())) { - boolean showNotification = (FirebasePlugin.inBackground() || !FirebasePlugin.hasNotificationsCallback()) && (!TextUtils.isEmpty(text) || !TextUtils.isEmpty(title)); - sendNotification(id, title, text, remoteMessage.getData(), showNotification); - } - } - - private void sendNotification(String id, String title, String messageBody, Map<String, String> data, boolean showNotification) { - Bundle bundle = new Bundle(); - for (String key : data.keySet()) { - bundle.putString(key, data.get(key)); - } - if (showNotification) { - Intent intent = new Intent(this, OnNotificationOpenReceiver.class); - intent.putExtras(bundle); - PendingIntent pendingIntent = PendingIntent.getBroadcast(this, id.hashCode(), intent, - PendingIntent.FLAG_UPDATE_CURRENT); - - Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION); - NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this) - .setContentTitle(title) - .setContentText(messageBody) - .setVisibility(NotificationCompat.VISIBILITY_PUBLIC) - .setStyle(new NotificationCompat.BigTextStyle().bigText(messageBody)) - .setAutoCancel(true) - .setSound(defaultSoundUri) - .setContentIntent(pendingIntent); - - int resID = getResources().getIdentifier("notification_icon", "drawable", getPackageName()); - if (resID != 0) { - notificationBuilder.setSmallIcon(resID); - } else { - notificationBuilder.setSmallIcon(getApplicationInfo().icon); - } - - if(android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.M) - { - int accentID = getResources().getIdentifier("accent", "color", getPackageName()); - notificationBuilder.setColor(getResources().getColor(accentID, null)); - } - - Notification notification = notificationBuilder.build(); - if(android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP){ - int iconID = android.R.id.icon; - int notiID = getResources().getIdentifier("notification_big", "drawable", getPackageName()); - if (notification.contentView != null) { - notification.contentView.setImageViewResource(iconID, notiID); - } - } - NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); - - notificationManager.notify(id.hashCode(), notification); - } else { - bundle.putBoolean("tap", false); - bundle.putString("title", title); - bundle.putString("body", messageBody); - FirebasePlugin.sendNotification(bundle); - } - } -} diff --git a/StoneIsland/platforms/android/src/org/apache/cordova/firebase/OnNotificationOpenReceiver.java b/StoneIsland/platforms/android/src/org/apache/cordova/firebase/OnNotificationOpenReceiver.java deleted file mode 100644 index a7f63757..00000000 --- a/StoneIsland/platforms/android/src/org/apache/cordova/firebase/OnNotificationOpenReceiver.java +++ /dev/null @@ -1,24 +0,0 @@ -package org.apache.cordova.firebase; - -import android.app.PendingIntent; -import android.content.BroadcastReceiver; -import android.content.Context; -import android.content.Intent; -import android.content.pm.PackageManager; -import android.os.Bundle; - -public class OnNotificationOpenReceiver extends BroadcastReceiver { - - @Override - public void onReceive(Context context, Intent intent) { - PackageManager pm = context.getPackageManager(); - Intent launchIntent = pm.getLaunchIntentForPackage(context.getPackageName()); - - launchIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK); - Bundle data = intent.getExtras(); - data.putBoolean("tap", true); - FirebasePlugin.sendNotification(data); - launchIntent.putExtras(data); - context.startActivity(launchIntent); - } -} |
