Skip to content
This repository has been archived by the owner on Sep 4, 2020. It is now read-only.

Add setApplicationIconBadgeNumber action for Android, using ShortcutBadger #624

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@

<framework src="com.android.support:support-v13:23+" />
<framework src="com.google.android.gms:play-services-gcm:+" />
<framework src="me.leolin:ShortcutBadger:1.1.4@aar" />

<source-file src="src/android/com/adobe/phonegap/push/GCMIntentService.java" target-dir="src/com/adobe/phonegap/push/" />
<source-file src="src/android/com/adobe/phonegap/push/PushConstants.java" target-dir="src/com/adobe/phonegap/push/" />
Expand Down
6 changes: 6 additions & 0 deletions src/android/com/adobe/phonegap/push/GCMIntentService.java
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Expand Down
1 change: 1 addition & 0 deletions src/android/com/adobe/phonegap/push/PushConstants.java
Original file line number Diff line number Diff line change
Expand Up @@ -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";
}
22 changes: 22 additions & 0 deletions src/android/com/adobe/phonegap/push/PushPlugin.java
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -179,6 +181,18 @@ public void run() {
}
}
});
} else if (SET_APPLICATION_ICON_BADGE_NUMBER.equals(action)) {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If I understand correctly, this code only runs when the app is opened, not when the push message arrives (That's what happens on my Device, anyway). If this is indeed the case, it can't update the counter badge until the app opened, which doesn't solve the bug - no?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not fully no, that's what I said, not really sure how. But with content available, you could still update in background.
But for background, this could work similarly right? Not sure how to fix it otherwise.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay so your patch (master...silo-co:master) handles the updating of the push in the background, and mine in foreground for setApplicationIconBadgeNumber right?

So we can combine both methods, right? Can you submit a PR to my branch to add that?

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think we need to merge them - the code I wrote handles both cases, because it's called anyway. I'm not sure that handling the same issue from different places in the code is wise

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No the first method happened only when you called setApplicationIconBadgeNumber() directly, right?
I've abstracted it to a public static function in the PushPlugin class, and added it to be called when a count it available. Similar to your branch, but when it's null, it doesn't change.
Also, you can call it directly (eg. without push, but with setApplicationIconBadgeNumber() directly)

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));
Expand Down Expand Up @@ -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);
Expand Down