显示Android的通知编号

时间:2015-02-27 14:00:40

标签: android notifications cgm

我想显示通知图标编号,但没有显示,有我的代码:

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);
    }

我错过了什么吗?

1 个答案:

答案 0 :(得分:0)

使用此功能添加数字徽章。

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