Skip to content

Commit 695adeb

Browse files
Reset badge method + badge count on android - credits etabard
1 parent 239951c commit 695adeb

7 files changed

Lines changed: 110 additions & 0 deletions

File tree

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ Parse.Push plugin for Cordova/Phonegap/ionic. Works for both hosted Parse.com an
2222
- **getSubscriptions**( successCB, errorCB )
2323
- **subscribe**( channel, successCB, errorCB )
2424
- **unsubscribe**( channel, successCB, errorCB )
25+
- **resetBadge**( successCB, errorCB )
2526
- **register**( successCB, errorCB ) //optional, see [Advanced Configuration](#advanced-configuration)
2627

2728
#### Notification events

src/.DS_Store

2 KB
Binary file not shown.

src/android/ParsePushPlugin.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ public class ParsePushPlugin extends CordovaPlugin {
2828
private static final String ACTION_UNSUBSCRIBE = "unsubscribe";
2929
private static final String ACTION_REGISTER_CALLBACK = "registerCallback";
3030
private static final String ACTION_REGISTER_FOR_PN = "register";
31+
public static final String ACTION_RESET_BADGE = "resetBadge";
3132

3233
private static CallbackContext gEventCallback = null;
3334
private static Queue<PluginResult> pnQueue = new LinkedList();
@@ -69,6 +70,10 @@ public boolean execute(String action, JSONArray args, CallbackContext callbackCo
6970
this.unsubscribe(args.getString(0), callbackContext);
7071
return true;
7172
}
73+
if (action.equals(ACTION_RESET_BADGE)) {
74+
ParsePushPluginReceiver.resetBadge(this.cordova.getActivity().getApplicationContext());
75+
return true;
76+
}
7277
if (action.equals(ACTION_REGISTER_FOR_PN)) {
7378
this.registerDeviceForPN(callbackContext);
7479
return true;

src/android/ParsePushPluginReceiver.java

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,19 @@
1818
import org.json.JSONObject;
1919
import org.json.JSONException;
2020

21+
import android.content.pm.PackageManager;
22+
import android.content.pm.ResolveInfo;
23+
24+
import java.util.List;
25+
2126

2227
public class ParsePushPluginReceiver extends ParsePushBroadcastReceiver
2328
{
2429
public static final String LOGTAG = "ParsePushPluginReceiver";
2530
public static final String RESOURCE_PUSH_ICON_COLOR = "parse_push_icon_color";
2631

2732
private static JSONObject MSG_COUNTS = new JSONObject();
33+
private static int badgeCount = 0;
2834

2935

3036
@Override
@@ -92,6 +98,8 @@ protected Notification getNotification(Context context, Intent intent){
9298
JSONObject pnData = getPushData(intent);
9399
String pnTag = getNotificationTag(context, pnData);
94100

101+
Log.d(LOGTAG, "onPushOpen - pnTag: " + pnData);
102+
95103
Intent cIntent = new Intent(ACTION_PUSH_OPEN);
96104
Intent dIntent = new Intent(ACTION_PUSH_DELETE);
97105

@@ -118,6 +126,26 @@ protected Notification getNotification(Context context, Intent intent){
118126
builder.setSound(android.provider.Settings.System.DEFAULT_NOTIFICATION_URI);
119127
}
120128

129+
if(pnData.has("badge")){
130+
try {
131+
if (pnData.getString("badge").equals("Increment")) {
132+
badgeCount += 1;
133+
}
134+
} catch (JSONException e) {
135+
Log.e(LOGTAG, "JSONException while parsing Increment:", e);
136+
}
137+
138+
try {
139+
if (pnData.getInt("badge") >= 0) {
140+
badgeCount = pnData.getInt("badge");
141+
}
142+
} catch (JSONException e) {
143+
Log.e(LOGTAG, "JSONException while parsing badge:", e);
144+
}
145+
146+
setBadge(context, badgeCount);
147+
}
148+
121149
builder.setSmallIcon(getSmallIconId(context, intent))
122150
.setLargeIcon(getLargeIcon(context, intent))
123151
.setNumber(nextCount(pnTag))
@@ -175,4 +203,65 @@ private static void resetCount(String pnTag){
175203
Log.e(LOGTAG, "JSONException while resetting pn count for tag: [" + pnTag + "]", e);
176204
}
177205
}
206+
207+
/*
208+
* Badge Counter methods. This will display badge counters on Samsung and Sony launchers.
209+
*/
210+
211+
public static void resetBadge(Context context) {
212+
badgeCount = 0;
213+
setBadgeSamsung(context, 0);
214+
setBadgeSony(context, 0);
215+
}
216+
217+
public static void setBadge(Context context, int count) {
218+
setBadgeSamsung(context, count);
219+
setBadgeSony(context, count);
220+
}
221+
222+
public static void setBadgeSamsung(Context context, int count) {
223+
String launcherClassName = getLauncherClassName(context);
224+
if (launcherClassName == null) {
225+
return;
226+
}
227+
Intent intent = new Intent("android.intent.action.BADGE_COUNT_UPDATE");
228+
intent.putExtra("badge_count", count);
229+
intent.putExtra("badge_count_package_name", context.getPackageName());
230+
intent.putExtra("badge_count_class_name", launcherClassName);
231+
context.sendBroadcast(intent);
232+
Log.d("PushReceiver", "Samsung: "+context.getPackageName()+" "+launcherClassName);
233+
}
234+
235+
public static void setBadgeSony(Context context, int count) {
236+
String launcherClassName = getLauncherClassName(context);
237+
238+
Intent intent = new Intent();
239+
intent.setAction("com.sonyericsson.home.action.UPDATE_BADGE");
240+
intent.putExtra("com.sonyericsson.home.intent.extra.badge.ACTIVITY_NAME", launcherClassName);
241+
intent.putExtra("com.sonyericsson.home.intent.extra.badge.SHOW_MESSAGE", count>0);
242+
intent.putExtra("com.sonyericsson.home.intent.extra.badge.MESSAGE", ""+count);
243+
intent.putExtra("com.sonyericsson.home.intent.extra.badge.PACKAGE_NAME", context.getPackageName());
244+
245+
context.sendBroadcast(intent);
246+
Log.d("PushReceiver", "Sony: " + context.getPackageName() + " " + launcherClassName);
247+
}
248+
249+
public static String getLauncherClassName(Context context) {
250+
251+
PackageManager pm = context.getPackageManager();
252+
253+
Intent intent = new Intent(Intent.ACTION_MAIN);
254+
intent.addCategory(Intent.CATEGORY_LAUNCHER);
255+
256+
List<ResolveInfo> resolveInfos = pm.queryIntentActivities(intent, 0);
257+
for (ResolveInfo resolveInfo : resolveInfos) {
258+
String pkgName = resolveInfo.activityInfo.applicationInfo.packageName;
259+
if (pkgName.equalsIgnoreCase(context.getPackageName())) {
260+
String className = resolveInfo.activityInfo.name;
261+
return className;
262+
}
263+
}
264+
265+
return null;
266+
}
178267
}

src/ios/ParsePushPlugin.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
- (void)getSubscriptions: (CDVInvokedUrlCommand *)command;
1717
- (void)subscribe: (CDVInvokedUrlCommand *)command;
1818
- (void)unsubscribe: (CDVInvokedUrlCommand *)command;
19+
- (void)resetBadge: (CDVInvokedUrlCommand *)command;
1920

2021
//
2122
// methods exposed to JS but not intended for users (not part of API)

src/ios/ParsePushPlugin.m

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,16 @@ - (void)unsubscribe: (CDVInvokedUrlCommand *)command
104104
[self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
105105
}
106106

107+
- (void)resetBadge:(CDVInvokedUrlCommand *)command {
108+
CDVPluginResult* pluginResult = nil;
109+
PFInstallation *currentInstallation = [PFInstallation currentInstallation];
110+
currentInstallation.badge = 0;
111+
112+
[currentInstallation saveInBackground];
113+
pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK];
114+
[self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
115+
}
116+
107117

108118
- (void)registerForPN {
109119
//

www/parse-push-plugin.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,10 @@ var ParsePushPlugin = {
6666
unsubscribe: function(channel, successCb, errorCb) {
6767
cordova.exec(successCb, errorCb, serviceName, 'unsubscribe', [ channel ]);
6868
},
69+
70+
resetBadge: function(successCb, errorCb) {
71+
cordova.exec(successCb, errorCb, serviceName, 'resetBadge', []);
72+
},
6973

7074
register: function(successCb, errorCb) {
7175
cordova.exec(successCb, errorCb, serviceName, 'register', []);

0 commit comments

Comments
 (0)