Group notifications on Android

家住魔仙堡 提交于 2019-12-08 06:45:24

问题


I want to show notifications like on picture. If there is more than one, I want to show a counter too. I didn't find info in official doc. Now I just update my notification by id:

((NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE))
                .notify(PUSH_NOTIFICATION_ID, notification);

How can I do it ?


回答1:


NotificationCompat.Builder mBuilder = new  NotificationCompat.Builder(getApplicationContext());
            mBuilder.setSmallIcon(R.mipmap.ic_launcher);
            mBuilder.setContentTitle(topic);
            mBuilder.setContentText(new String(message.getPayload()));
            mBuilder.setAutoCancel(true);
            mBuilder.mNumber = 1;//get your number of notification from where you have save notification

            NotificationManager mNotifyMgr = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
            mNotifyMgr.notify(notify_id, mBuilder.build());



回答2:


To create a stack, call setGroup() for each notification you want in the stack and specify a group key.

final static String GROUP_KEY_EMAILS = "group_key_emails";

    // Build the notification, setting the group appropriately
    Notification notif = new NotificationCompat.Builder(mContext)
             .setContentTitle("New mail from " + sender1)
             .setContentText(subject1)
             .setSmallIcon(R.drawable.new_mail)
             .setGroup(GROUP_KEY_EMAILS)
             .build();

    // Issue the notification
    NotificationManagerCompat notificationManager =
            NotificationManagerCompat.from(this);
    notificationManager.notify(notificationId1, notif);

Reference: https://developer.android.com/training/wearables/notifications/stacks.html



来源:https://stackoverflow.com/questions/41692501/group-notifications-on-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!