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

Commit

Permalink
Issue #190: Badge on android
Browse files Browse the repository at this point in the history
  • Loading branch information
macdonst committed Apr 25, 2016
1 parent cb6b04f commit a933a2f
Showing 1 changed file with 29 additions and 17 deletions.
46 changes: 29 additions & 17 deletions src/android/com/adobe/phonegap/push/GCMIntentService.java
Original file line number Diff line number Diff line change
Expand Up @@ -193,22 +193,36 @@ private Bundle normalizeExtras(Bundle extras) {
return newExtras;
}

private int extractBadgeCount(Bundle extras) {
int count = -1;
String msgcnt = extras.getString(COUNT);

try {
if (msgcnt != null) {
count = Integer.parseInt(msgcnt);
}
} catch (NumberFormatException e) {
Log.e(LOG_TAG, e.getLocalizedMessage(), e);
}

return count;
}

private void showNotificationIfPossible (Context context, Bundle extras) {

// Send a notification if there is a message or title, otherwise just send data
String message = extras.getString(MESSAGE);
String title = extras.getString(TITLE);
String contentAvailable = extras.getString(CONTENT_AVAILABLE);
String badgeCount = extras.getString(COUNT);
int badgeCount = extractBadgeCount(extras);
if (badgeCount >= 0) {
Log.d(LOG_TAG, "count =[" + badgeCount + "]");
PushPlugin.setApplicationIconBadgeNumber(context, badgeCount);
}

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 Expand Up @@ -325,7 +339,7 @@ public void createNotification(Context context, Bundle extras) {
/*
* Notification count
*/
setNotificationCount(extras, mBuilder);
setNotificationCount(context, extras, mBuilder);

/*
* Notification add actions
Expand Down Expand Up @@ -365,9 +379,9 @@ private void createActions(Bundle extras, NotificationCompat.Builder mBuilder, R
pIntent = PendingIntent.getBroadcast(this, i, intent, PendingIntent.FLAG_UPDATE_CURRENT);
}
NotificationCompat.Action wAction =
new NotificationCompat.Action.Builder(resources.getIdentifier(action.optString(ICON, ""), DRAWABLE, packageName),
action.getString(TITLE), pIntent)
.build();
new NotificationCompat.Action.Builder(resources.getIdentifier(action.optString(ICON, ""), DRAWABLE, packageName),
action.getString(TITLE), pIntent)
.build();
wActions.add(wAction);
mBuilder.addAction(resources.getIdentifier(action.optString(ICON, ""), DRAWABLE, packageName),
action.getString(TITLE), pIntent);
Expand All @@ -382,13 +396,11 @@ private void createActions(Bundle extras, NotificationCompat.Builder mBuilder, R
}
}

private void setNotificationCount(Bundle extras, NotificationCompat.Builder mBuilder) {
String msgcnt = extras.getString(MSGCNT);
if (msgcnt == null) {
msgcnt = extras.getString(BADGE);
}
if (msgcnt != null) {
mBuilder.setNumber(Integer.parseInt(msgcnt));
private void setNotificationCount(Context context, Bundle extras, NotificationCompat.Builder mBuilder) {
int count = extractBadgeCount(extras);
if (count >= 0) {
Log.d(LOG_TAG, "count =[" + count + "]");
mBuilder.setNumber(count);
}
}

Expand Down

0 comments on commit a933a2f

Please sign in to comment.