在Android中的App图标上显示徽章计数

时间:2013-10-04 14:08:37

标签: android-notifications

在我的Android应用中,我想在ios等应用程序图标中显示推送通知徽章计数。

如何在应用程序图标中显示徽章计数?

2 个答案:

答案 0 :(得分:3)

Android没有此特定功能。

替代方案可能是显示数字的通知,但在正常情况下应用程序图标本身无法使用。

如果你真的想破坏设计指南,你可以创建一个看起来像你的应用程序图标的小部件,小部件可以更好地控制它的绘制方式。一些应用程序已经发布用于Gmail,Google Voice,标准消息应用程序,并且错过了调用电话应用程序,并且它们伪装成常规应用程序图标,因此它是可能的。我并不是真的推荐这种方法,但可以使用。

答案 1 :(得分:1)

1。  使用ShortcutBadger库进行徽章计数

        dependencies {
            compile 'me.leolin:ShortcutBadger:1.1.4@aar'
        }

2

       public class MyFirebaseMessagingService extends FirebaseMessagingService {

            private static final String TAG = "MyFirebaseMsgService";

            Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);

            @Override
            public void onMessageReceived(RemoteMessage remoteMessage) {

                // Check if message contains a data payload.
                if (remoteMessage.getData().size() > 0) {
                    Log.d(TAG, "1. remoteMessage.getData().size() > 0");
                    Log.d(TAG, "Message data payload: " + remoteMessage.getData()); // {mobile=9458074124, name=Arvind}
                    Log.d(TAG, "Message Notification Body: " + remoteMessage.getNotification().getBody());
                    Log.d(TAG, "Message getTitle: " + remoteMessage.getNotification().getTitle());
                    Map<String, String> notificationData1 = remoteMessage.getData();

                    SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());

                    Log.d(TAG, sharedPreferences.getString("userid", "") + ".....id"+notificationData1.get("action"));

                    try {

                        if (notificationData1.get("actionID").equalsIgnoreCase(sharedPreferences.getString("userid", ""))) {

                        } else {
                            showNotificationForEvent(remoteMessage);
                            // }

                        }


                    }catch (Exception e)
                    {
                        e.printStackTrace();
                    }

                }

            }

            // Show Notification for Attend,Like,Comment,Rate

            private void showNotificationForEvent(RemoteMessage remoteMessage) {

                Log.d(TAG, "00. showNotificationForEvent");

                Map<String,String> notificationData = remoteMessage.getData();

                Intent resultIntent = new Intent(this, BubblesActivity.class);
                resultIntent.putExtra("action", notificationData.get("action"));
                resultIntent.putExtra("actionID", notificationData.get("actionID"));

                resultIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                resultIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
                resultIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

                PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, resultIntent,
                        PendingIntent.FLAG_ONE_SHOT);

                NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
                        .setSmallIcon(R.drawable.ipray_appicon)
        //                .setColor(ContextCompat.getColor(this, R.color.textColor))
                        .setContentTitle(remoteMessage.getNotification().getTitle())
                        .setContentText(remoteMessage.getNotification().getBody()/*+Utils.getPrefrence(getApplicationContext(), Const.KEY_USER_ID)+"...id"*/)
                        .setAutoCancel(true)
                        .setSound(defaultSoundUri)
                        .setContentIntent(pendingIntent);


                //Vibration   // new long[] { 1000, 1000, 1000, 1000, 1000 }
                notificationBuilder.setVibrate(new long[]  {500,1000});

                NotificationManager notificationManager =
                        (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

                notificationManager.notify(0 /* ID of notification */, notificationBuilder.build());
            }




            //This method will return true if the app is in background.
            private boolean isAppIsInBackground(Context context) {
                boolean isInBackground = true;
                ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
                if (Build.VERSION.SDK_INT > Build.VERSION_CODES.GINGERBREAD) {
                    List<ActivityManager.RunningAppProcessInfo> runningProcesses = am.getRunningAppProcesses();
                    for (ActivityManager.RunningAppProcessInfo processInfo : runningProcesses) {
                        if (processInfo.importance == ActivityManager.RunningAppProcessInfo.IMPORTANCE_FOREGROUND) {
                            for (String activeProcess : processInfo.pkgList) {
                                if (activeProcess.equals(context.getPackageName())) {
                                    isInBackground = false;
                                }
                            }
                        }
                    }

                }

                else {
                    List<ActivityManager.RunningTaskInfo> taskInfo = am.getRunningTasks(1);
                    ComponentName componentInfo = taskInfo.get(0).topActivity;
                    if (componentInfo.getPackageName().equals(context.getPackageName())) {
                        isInBackground = false;
                    }
                }

                return isInBackground;
            }


            @Override
            protected Intent zzae(Intent intent) {

                //Log.e("intentdata","DataPayload zzae"+intentToString(intent));

                return super.zzae(intent);
            }

            @Override
            public boolean zzag(Intent intent) {

                Log.e("intentdata","DataPayload zzag==="+intentToString(intent).toString());
                Log.e("intentdata","getData====="+intent.toString());

               if( intent.hasExtra("totalprayerdue"))
                {
                    ShortcutBadger.applyCount(getApplicationContext(), Integer.parseInt(intent.getStringExtra("totalprayerdue")));
                }

                return super.zzag(intent);
            }

            @Override
            public void zzm(Intent intent) {
                Log.e("intentdata","DataPayload zzm"+intentToString(intent));
                super.zzm(intent);
            }

            public static String intentToString(Intent intent) {
                if (intent == null)
                    return "";

                StringBuilder stringBuilder = new StringBuilder("action: ")
                        .append(intent.getAction())
                        .append(" data: ")
                        .append(intent.getDataString())
                        .append(" extras: ")
                        ;
                for (String key : intent.getExtras().keySet())
                    stringBuilder.append(key).append("=").append(intent.getExtras().get(key)).append(" ");

                return stringBuilder.toString();

            }
        }