1818import org .json .JSONObject ;
1919import org .json .JSONException ;
2020
21+ import android .content .pm .PackageManager ;
22+ import android .content .pm .ResolveInfo ;
23+
24+ import java .util .List ;
25+
2126
2227public 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}
0 commit comments