From f9f33360fc9d706e1c66ab77c77460f491938fd9 Mon Sep 17 00:00:00 2001 From: "Barry vd. Heuvel" Date: Tue, 16 Feb 2016 11:49:53 +0100 Subject: [PATCH] Add setApplicationIconBadgeNumber action for Android, using ShortcutBadger --- plugin.xml | 1 + .../adobe/phonegap/push/GCMIntentService.java | 6 +++++ .../adobe/phonegap/push/PushConstants.java | 1 + .../com/adobe/phonegap/push/PushPlugin.java | 22 +++++++++++++++++++ 4 files changed, 30 insertions(+) diff --git a/plugin.xml b/plugin.xml index a599b1455..b330162f7 100755 --- a/plugin.xml +++ b/plugin.xml @@ -86,6 +86,7 @@ + diff --git a/src/android/com/adobe/phonegap/push/GCMIntentService.java b/src/android/com/adobe/phonegap/push/GCMIntentService.java index b97660175..fc242f4af 100644 --- a/src/android/com/adobe/phonegap/push/GCMIntentService.java +++ b/src/android/com/adobe/phonegap/push/GCMIntentService.java @@ -197,10 +197,16 @@ private void showNotificationIfPossible (Context context, Bundle extras) { String message = extras.getString(MESSAGE); String title = extras.getString(TITLE); String contentAvailable = extras.getString(CONTENT_AVAILABLE); + String badgeCount = extras.getString(COUNT); Log.d(LOG_TAG, "message =[" + message + "]"); Log.d(LOG_TAG, "title =[" + title + "]"); Log.d(LOG_TAG, "contentAvailable =[" + contentAvailable + "]"); + Log.d(LOG_TAG, "badgeCount =[" + badgeCount + "]"); + + if (badgeCount != null) { + PushPlugin.setApplicationIconBadgeNumber(context, Integer.parseInt(badgeCount)); + } if ((message != null && message.length() != 0) || (title != null && title.length() != 0)) { diff --git a/src/android/com/adobe/phonegap/push/PushConstants.java b/src/android/com/adobe/phonegap/push/PushConstants.java index f24565848..cfd90dc04 100644 --- a/src/android/com/adobe/phonegap/push/PushConstants.java +++ b/src/android/com/adobe/phonegap/push/PushConstants.java @@ -54,4 +54,5 @@ public interface PushConstants { public static final String GCM = "GCM"; public static final String CONTENT_AVAILABLE = "content-available"; public static final String TOPICS = "topics"; + public static final String SET_APPLICATION_ICON_BADGE_NUMBER = "setApplicationIconBadgeNumber"; } diff --git a/src/android/com/adobe/phonegap/push/PushPlugin.java b/src/android/com/adobe/phonegap/push/PushPlugin.java index 2fac93320..b63a478ae 100644 --- a/src/android/com/adobe/phonegap/push/PushPlugin.java +++ b/src/android/com/adobe/phonegap/push/PushPlugin.java @@ -23,6 +23,8 @@ import java.util.HashSet; import java.util.Iterator; +import me.leolin.shortcutbadger.ShortcutBadger; + public class PushPlugin extends CordovaPlugin implements PushConstants { public static final String LOG_TAG = "PushPlugin"; @@ -179,6 +181,18 @@ public void run() { } } }); + } else if (SET_APPLICATION_ICON_BADGE_NUMBER.equals(action)) { + cordova.getThreadPool().execute(new Runnable() { + public void run() { + Log.v(LOG_TAG, "setApplicationIconBadgeNumber: data=" + data.toString()); + try { + setApplicationIconBadgeNumber(getApplicationContext(), data.getJSONObject(0).getInt(BADGE)); + } catch (JSONException e) { + callbackContext.error(e.getMessage()); + } + callbackContext.success(); + } + }); } else { Log.e(LOG_TAG, "Invalid action : " + action); callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.INVALID_ACTION)); @@ -219,6 +233,14 @@ public static void sendExtras(Bundle extras) { } } + public static void setApplicationIconBadgeNumber(Context context, int badgeCount) { + if (badgeCount > 0) { + ShortcutBadger.applyCount(context, badgeCount); + } else { + ShortcutBadger.removeCount(context); + } + } + @Override public void initialize(CordovaInterface cordova, CordovaWebView webView) { super.initialize(cordova, webView);