Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Group notification with summary in android nougat 7

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 1.34k
    Comment on it

    As we know we have notification bar to show all the notifications in that are, in Android M we can show multiple notifications but from android N we can group them easily with better UI experience.

    A summary will also add whenever user wants to add more than one notifications.

    public class MainActivity extends AppCompatActivity {
    
        private NotificationsFragment mNotificationsFragment;
    
        protected static final String NOTIFICATION_DELETE
                = "delete";
    
        private BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() {
            @Override
            public void onReceive(Context context, Intent intent) {
                if (mNotificationsFragment == null) {
                    checkFragment();
                }
                mNotificationsFragment.updateNotifications();
            }
        };
    
        @Override
        public void onCreate(Bundle savedInstanceState, PersistableBundle persistentState) {
            super.onCreate(savedInstanceState, persistentState);
            checkFragment();
            mFragment.updateNotifications();
        }
    
        private void checkFragment() {
            mFragment = (NotificationsFragment) getSupportFragmentManager()
                    .findFragmentById(R.id.notificaiton_content_fragment);
        }
    
        @Override
        protected void onResume() {
            super.onResume();
            registerReceiver(mBroadcastReceiver, new IntentFilter(NOTIFICATION_DELETE));
        }
    
        @Override
        protected void onPause() {
            super.onPause();
            unregisterReceiver(mBroadcastReceiver);
        }
    }

     

    Main Fragment to generate multiple fragments with summary like this :

    public class NotificationsFragment extends Fragment {
    
        private static final int REQUEST_CODE = 2323;
    
        private static final String NOTIFICATION_TYPE =
                "notification_type";
    
        private static final int NOTIFICATION_GROUP_ID = 1;
    
        private NotificationManager mNotificationManager;
    
        private TextView tvTotalNotifications;
    
        private static int sNotificationId = NOTIFICATION_GROUP_ID + 1;
    
        private PendingIntent mDeletePendingIntent;
    
        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {
            return inflater.inflate(R.layout.fragment_notification_builder, container, false);
        }
    
        @Override
        public void onResume() {
            super.onResume();
            updateNotifications();
        }
    
        @Override
        public void onViewCreated(View view, Bundle savedInstanceState) {
            super.onViewCreated(view, savedInstanceState);
    
            mNotificationManager = (NotificationManager) getActivity().getSystemService(
                    Context.NOTIFICATION_SERVICE);
    
            tvTotalNotifications = (TextView) view.findViewById(R.id.tvTotalNotifications);
    
            view.findViewById(R.id.add_notification).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    updateNotificationAndSummaries();
                }
            });
    
            //creating pending intent that will fire whenever user will generate any notificaiton.
    
            Intent deleteIntent = new Intent(ActiveNotificationsActivity.ACTION_NOTIFICATION_DELETE);
            mDeletePendingIntent = PendingIntent.getBroadcast(getActivity(),
                    REQUEST_CODE, deleteIntent, 0);
        }
    
        private void updateNotificationAndSummaries() {
    
            //create first time notification in notification bar only setting group type, later we will set group summary and ll update notifications.
                    
            final NotificationCompat.Builder builder = new NotificationCompat.Builder(getActivity())
                    .setSmallIcon(R.mipmap.ic_notification)
                    .setContentTitle(getString(R.string.app_name))
                    .setContentText(getString(R.string.sample_notification_content))
                    .setAutoCancel(true)
                    .setDeleteIntent(mDeletePendingIntent)
                    .setGroup(NOTIFICATION_TYPE);
    
            final Notification notification = builder.build();
            mNotificationManager.notify(getNewNotificationId(), notification);
            updateSummary();
            updateNotifications();
        }
    
        protected void updateSummary() {
    
            // StatusBarNotification store total number of active notifications we can get it from Notification manager get Active notificaiton method and then can get its length to identify total notifications.
    
            final StatusBarNotification[] activeNotifications = mNotificationManager
                    .getActiveNotifications();
            int numberOfNotifications = activeNotifications.length;
            for (StatusBarNotification notification : activeNotifications) {
              if (notification.getId() == NOTIFICATION_GROUP_ID) {
                numberOfNotifications--;
                break;
              }
            }
            // if we have more than 1 notifications then we can set some summary type on it and group.
    
            if (numberOfNotifications > 1) {
                String notificationContent = "Active notifiations are :" + numberOfNotifications;
                final NotificationCompat.Builder builder = new NotificationCompat.Builder(getActivity())
                        .setSmallIcon(R.mipmap.ic_notification)
                        .setStyle(new NotificationCompat.BigTextStyle()
                                .setSummaryText(notificationContent))
                        .setGroup(NOTIFICATION_TYPE)
                        .setGroupSummary(true);
                final Notification notification = builder.build();
                mNotificationManager.notify(NOTIFICATION_GROUP_ID, notification);
            } else {
                mNotificationManager.cancel(NOTIFICATION_GROUP_ID);
            }
        }
    
        protected void updateNotifications() {
    
            // StatusBarNotification store total number of active notifications we can get it from Notification manager get Active notificaiton method and then can get its length to identify total notifications.
    
            final StatusBarNotification[] activeNotifications = mNotificationManager
                    .getActiveNotifications();
            final int numberOfNotifications = activeNotifications.length;
            tvTotalNotifications.setText(getString(R.string.active_notifications,
                    numberOfNotifications));
        }
    
    
        public int getNewNotificationId() {
            int notificationId = sNotificationId++;
    
            if (notificationId == NOTIFICATION_GROUP_ID) {
                notificationId = sNotificationId++;
            }
            return notificationId;
        }
    }
    

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: