diff --git a/plugin.xml b/plugin.xml index d504241d..d7334d04 100644 --- a/plugin.xml +++ b/plugin.xml @@ -46,6 +46,7 @@ + diff --git a/src/android/ParsePushPlugin.java b/src/android/ParsePushPlugin.java index 26956498..62d0ae9c 100644 --- a/src/android/ParsePushPlugin.java +++ b/src/android/ParsePushPlugin.java @@ -35,6 +35,7 @@ public class ParsePushPlugin extends CordovaPlugin { private static CordovaWebView gWebView; private static boolean gForeground = false; + private static boolean helperPause = false; public static final String LOGTAG = "ParsePushPlugin"; @@ -137,13 +138,21 @@ public static void jsCallback(JSONObject _json){ jsCallback(_json, "RECEIVE"); } - public static void jsCallback(JSONObject _json, String pushAction){ +public static void jsCallback(JSONObject _json, String pushAction){ List cbParams = new ArrayList(); cbParams.add(new PluginResult(PluginResult.Status.OK, _json)); cbParams.add(new PluginResult(PluginResult.Status.OK, pushAction)); - - PluginResult dataResult = new PluginResult(PluginResult.Status.OK, cbParams); - dataResult.setKeepCallback(true); + //avoid blank + PluginResult dataResult; + if (pushAction.equals("OPEN")) { + if (helperPause) + dataResult = new PluginResult(PluginResult.Status.OK, _json); + else + dataResult = new PluginResult(PluginResult.Status.OK, cbParams); + } else { + dataResult = new PluginResult(PluginResult.Status.OK, _json); + } + dataResult.setKeepCallback(true); if(gEventCallback != null){ @@ -159,6 +168,7 @@ public static void jsCallback(JSONObject _json, String pushAction){ } } + private static void flushPNQueue(){ while(!pnQueue.isEmpty() && gEventCallback != null){ gEventCallback.sendPluginResult(pnQueue.remove()); @@ -175,6 +185,7 @@ protected void pluginInitialize() { public void onPause(boolean multitasking) { super.onPause(multitasking); gForeground = false; + helperPause = true; } @Override @@ -189,6 +200,7 @@ public void onDestroy() { gWebView = null; gForeground = false; gEventCallback = null; + helperPause = false; super.onDestroy(); } diff --git a/src/android/ParsePushPluginReceiver.java b/src/android/ParsePushPluginReceiver.java index fb6e55b1..1069339c 100644 --- a/src/android/ParsePushPluginReceiver.java +++ b/src/android/ParsePushPluginReceiver.java @@ -27,6 +27,14 @@ import java.util.List; import java.util.Random; +import android.content.SharedPreferences; + +import org.apache.cordova.CallbackContext; +import org.apache.cordova.PluginResult; +import org.json.JSONArray; + +import me.leolin.shortcutbadger.ShortcutBadger; + public class ParsePushPluginReceiver extends ParsePushBroadcastReceiver { @@ -36,6 +44,8 @@ public class ParsePushPluginReceiver extends ParsePushBroadcastReceiver private static JSONObject MSG_COUNTS = new JSONObject(); private static int badgeCount = 0; + private static final String KEY = "badge"; + @Override protected void onPushReceive(Context context, Intent intent) { @@ -167,7 +177,7 @@ protected Notification getNotification(Context context, Intent intent){ Log.e(LOGTAG, "JSONException while parsing badge:", e); } - setBadge(context, badgeCount); + setBadge(badgeCount, context); } builder.setSmallIcon(getSmallIconId(context, intent)) @@ -232,44 +242,37 @@ private static void resetCount(String pnTag){ * Badge Counter methods. This will display badge counters on Samsung and Sony launchers. */ - public static void resetBadge(Context context) { - badgeCount = 0; - setBadgeSamsung(context, 0); - setBadgeSony(context, 0); - } - - public static void setBadge(Context context, int count) { - setBadgeSamsung(context, count); - setBadgeSony(context, count); - } - - public static void setBadgeSamsung(Context context, int count) { - String launcherClassName = getLauncherClassName(context); - if (launcherClassName == null) { - return; - } - Intent intent = new Intent("android.intent.action.BADGE_COUNT_UPDATE"); - intent.putExtra("badge_count", count); - intent.putExtra("badge_count_package_name", context.getPackageName()); - intent.putExtra("badge_count_class_name", launcherClassName); - context.sendBroadcast(intent); - Log.d("PushReceiver", "Samsung: "+context.getPackageName()+" "+launcherClassName); - } - - public static void setBadgeSony(Context context, int count) { - String launcherClassName = getLauncherClassName(context); + /** + * Sets the badge of the app icon. + * + * @param args + * The new badge number + * @param ctx + * The application context + */ + public static void setBadge (int badgeCount, Context ctx) { + int badge = badgeCount; + + saveBadge(badge, ctx); + ShortcutBadger.applyCount(ctx, badge); + } + + public static void resetBadge (Context ctx) { + saveBadge(0, ctx); + ShortcutBadger.removeCount(ctx); + } - Intent intent = new Intent(); - intent.setAction("com.sonyericsson.home.action.UPDATE_BADGE"); - intent.putExtra("com.sonyericsson.home.intent.extra.badge.ACTIVITY_NAME", launcherClassName); - intent.putExtra("com.sonyericsson.home.intent.extra.badge.SHOW_MESSAGE", count>0); - intent.putExtra("com.sonyericsson.home.intent.extra.badge.MESSAGE", ""+count); - intent.putExtra("com.sonyericsson.home.intent.extra.badge.PACKAGE_NAME", context.getPackageName()); + private static void saveBadge (int badge, Context ctx) { + SharedPreferences.Editor editor = getSharedPreferences(ctx).edit(); - context.sendBroadcast(intent); - Log.d("PushReceiver", "Sony: " + context.getPackageName() + " " + launcherClassName); - } + editor.putInt(KEY, badge); + editor.apply(); + } + private static SharedPreferences getSharedPreferences (Context context) { + return context.getSharedPreferences(KEY, Context.MODE_PRIVATE); + } + public static String getLauncherClassName(Context context) { PackageManager pm = context.getPackageManager(); diff --git a/src/android/parse-push-plugin.gradle b/src/android/parse-push-plugin.gradle new file mode 100644 index 00000000..539356b7 --- /dev/null +++ b/src/android/parse-push-plugin.gradle @@ -0,0 +1,9 @@ + + +repositories { + mavenCentral() +} + +dependencies { + compile 'me.leolin:ShortcutBadger:1.1.11@aar' +} diff --git a/www/parse-push-plugin.js b/www/parse-push-plugin.js index a9218a6c..304320a4 100644 --- a/www/parse-push-plugin.js +++ b/www/parse-push-plugin.js @@ -11,7 +11,7 @@ require('cordova/channel').onCordovaReady.subscribe(function() { } if(pn !== null){ - if(pushAction === 'OPEN'){ + if(pushAction === 'OPEN' || pn.OPEN){ // // trigger a callback when user click open a notification. // One usecase for this pertains a cordova app that is already running in the background. @@ -93,7 +93,7 @@ function poorManExtend(object, source){ var eventSplitter = /\s+/; var slice = Array.prototype.slice; var EventMixin = { - _coldStartDelayMs: 300, + _coldStartDelayMs: 1000, on: function(events, callback, context) { var calls, event, node, tail, list;