0

i want to show the notification icon number but nothing showed, there is my code :

private static void generateNotification(Context context, String message,
            String title, String url) {
        mContext = context;
        int icon = R.drawable.ic_launcher;
        long when = System.currentTimeMillis();
        int pendingNotificationsCount = App.getPendingNotificationsCount() + 1;
        NotificationManager notificationManager = (NotificationManager) context
                .getSystemService(Context.NOTIFICATION_SERVICE);
        Notification notification = new Notification(icon, message, when);

        // detail intent
        Intent notificationIntent = new Intent(context, MainActivity.class);
        notificationIntent.putExtra("urlmessage", url);
        Log.i(null, "url de message " + url);

        notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP
                | Intent.FLAG_ACTIVITY_SINGLE_TOP);
        PendingIntent intent = PendingIntent.getActivity(context, 0,
                notificationIntent, PendingIntent.FLAG_ONE_SHOT);
        notification.setLatestEventInfo(context, title, message, intent);
        notification.flags |= Notification.FLAG_AUTO_CANCEL;

        notification.defaults |= Notification.DEFAULT_SOUND;

        notification.defaults |= Notification.DEFAULT_VIBRATE;
        App.setPendingNotificationsCount(pendingNotificationsCount);
        notification.number = pendingNotificationsCount;

        notificationManager.notify(pendingNotificationsCount, notification);
    }

did i miss something ??

Fahim
  • 12,198
  • 5
  • 39
  • 57

1 Answers1

0

Use this to add number badge.

public static void setBadge(Context context, int count) { 
String launcherClassName = getLauncherClassName(context);
 if (launcherClassName == null) { return; }
 Intent intent = new Intent("android.intent.action.BADGE_COUNT_UPDATE");
 intent.putExtra("badge_count", count);
 intent.putExtra("badge_count_package_name", context.getPackageName());
 intent.putExtra("badge_count_class_name", launcherClassName);
 context.sendBroadcast(intent);
 } 


public static String getLauncherClassName(Context context) { 
PackageManager pm = context.getPackageManager();
 Intent intent = new Intent(Intent.ACTION_MAIN);
 intent.addCategory(Intent.CATEGORY_LAUNCHER); 
List<ResolveInfo> resolveInfos = pm.queryIntentActivities(intent, 0);
 for (ResolveInfo resolveInfo : resolveInfos) { 
String pkgName = resolveInfo.activityInfo.applicationInfo.packageName; 
if(pkgName.equalsIgnoreCase(context.getPackageName())) { 
String className = resolveInfo.activityInfo.name; return className; 
} 
}
 return null; 
}

How to display count of notifications in app launcher icon

Community
  • 1
  • 1
Fahim
  • 12,198
  • 5
  • 39
  • 57