Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • How to Update Android Notification, Expanded Notification and Big Notification?

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 2.81k
    Comment on it

    This blog is about how to update android notification and how to use Expanded and Big notification.

     

    For Big style Notification you can check my previous blog which is:-

    http://findnerd.com/account#url=/list/publish/Big-View-Notification-Android--/22572/

    Another Notification we used is Expanding layout. Expanded Layout notification is avail from android 4.1. With expanded layout you can check more data of Notification withing Notification.

     

    Updating Notification is if you already got notification and now get new one. So you want to merge both notification rather then 2 different notification on Notification bar. Like WhatsApp application. For Updating old notification there will be a same id of notification.

    Here is one example code like Whats app Notification handling.

    1. Create project.

    2. Create new Activity.

    3. Create Class variable as notification id.

      public static final int NOTIFICATION_ID = 1;

    4. Create a class with name User and define variables and Getter setter like below.

    public class User {
    
        private int user_id = 0;
        private List<String> messages = new ArrayList<>();
    
        public int getUser_id() {
            return user_id;
        }
    
        public void setUser_id(int user_id) {
            this.user_id = user_id;
        }
    
        public List<String> getMessages() {
            return messages;
        }
    
        public void setMessages(String message) {
            messages.add(message);
        }
    
    }

    5. Initialize List of users.

     List<User> mUsers = new ArrayList<>();

    6. Create constants for Users.

        private static final int USER0 = 0;
        private static final int USER1 = 1;

    7. Create activity's xml like below.

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_vertical">
    
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Tap the buttons below to send a notifications. There is notification from two users 1st User and 2nd User to test advance level notifications."
            android:scrollbars="vertical"
            android:layout_gravity="fill_vertical"
            android:layout_weight="1"/>
    
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="1st User"
            android:layout_gravity="center"
            android:onClick="user1"/>
    
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="2nd User"
            android:layout_gravity="center"
            android:onClick="user2"/>
    
    </LinearLayout>
    

    8. create onClick methods of  on Buttons 1st User & 2nd User . that are user1 method and user2 like below. 

     public void user1(View view) { 
    }
    
     public void user2(View view) { 
    }

    9. Complete user1() and user2 with below code.

    public void user1(View view) {
    
            User user = null;
            for (int i = 0; i < mUsers.size(); i++) {
                if (mUsers.get(i).getUser_id() == USER0) {
                    user = mUsers.get(i);
                    break;
                }
            }
    
            if (user == null) {
                user = new User();
                user.setUser_id(USER0);
                mUsers.add(user);
            }
            user.setMessages("User1 Message");
            sendNotification();
        }
    
    
    public void user2(View view) {
    
            User user = null;
            for (int i = 0; i < mUsers.size(); i++) {
                if (mUsers.get(i).getUser_id() == USER1) {
                    user = mUsers.get(i);
                    break;
                }
            }
    
            if (user == null) {
                user = new User();
                user.setUser_id(USER1);
                mUsers.add(user);
            }
            user.setMessages("User2 Message");
            sendNotification();
        }

    10. Create sendNotification().

    11.Write code under send Notification.

    12. Create Intent for notification click event.

     Intent intent = new Intent(Intent.ACTION_VIEW,
                    Uri.parse("http://google.com"));

    *Note:- I am using ACTION_VIEW for demo intent. you can use your activity/service etc.

    13. Create Notification Builder.

    NotificationCompat.Builder builder = new NotificationCompat.Builder(this);

    14. Check if number of users and number of message is 1.

     if (mUsers.size() == 1 && mUsers.get(0).getMessages().size() == 1)

    15. If Condition is true then we will show Big style notification.

    16. Set Content Text to builder.

     builder.setContentText(mUsers.get(0).getMessages().get(0));

    17.  Create Intent for reply and dismiss .

      Intent dismissIntent = new Intent(Intent.ACTION_VIEW,
                        Uri.parse("http://google.com"));
                PendingIntent piDismiss = PendingIntent.getActivity(this, 0, dismissIntent, 0);
    
                Intent replyIntent = new Intent(Intent.ACTION_VIEW,
                        Uri.parse("http://google.com"));
                PendingIntent piReply = PendingIntent.getActivity(this, 0, replyIntent, 0);

    18.  Set Style to Builder.

     builder.setStyle(new NotificationCompat.BigTextStyle()
                        .bigText(mUsers.get(0).getMessages().get(0)))
                        .addAction (R.drawable.ic_launcher,
                                "Cancel", piDismiss)
                        .addAction (R.drawable.ic_launcher,
                                "Reply", piReply);

    19. If Users count or messages count is not 1 then write below code in else condition.

    20. create int variable count.

     int counter = 0;

    21. Create Inbox style notification object.

    NotificationCompat.InboxStyle inboxStyle =
                        new NotificationCompat.InboxStyle();

    22. get messages from different users and add to line of inboxStyle.

     for (int i = 0; i < mUsers.size(); i++) {
                    for (int j = 0; j < mUsers.get(i).getMessages().size(); j++) {
                        inboxStyle.addLine(mUsers.get(i).getMessages().get(j) + (j++));
                        counter++;
                    }
                }

    23. Set builder a inbox style notification.

     builder.setStyle(inboxStyle);

    24. Set Big content title to inboxStyle.

    inboxStyle.setBigContentTitle(counter + " Messages");

    25. Set sub text and content text to builder.

    builder.setSubText("You got " + counter + " messages from " + mUsers.size() + " users");
                builder.setContentText(counter + " messages");

    26. Set small icon and Intent to builder.

           builder.setSmallIcon(R.drawable.ic_stat_notification);
            
            builder.setContentIntent(pendingIntent);

    27. set Large icon and Content Title.

     builder.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher));
    
            builder.setContentTitle("Updating Notification");

    28. Create NotificationManager instance and notify Notification.

      NotificationManager notificationManager = (NotificationManager) getSystemService(
                    NOTIFICATION_SERVICE);
            notificationManager.notify(NOTIFICATION_ID, builder.build());

    29. Full code of sendNotification().

     public void sendNotification() {
            Intent intent = new Intent(Intent.ACTION_VIEW,
                    Uri.parse("http://google.com"));
            PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 0);
    
            NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
    
            if (mUsers.size() == 1 && mUsers.get(0).getMessages().size() == 1) {
    
                builder.setContentText(mUsers.get(0).getMessages().get(0));
    
                Intent dismissIntent = new Intent(Intent.ACTION_VIEW,
                        Uri.parse("http://google.com"));
                PendingIntent piDismiss = PendingIntent.getActivity(this, 0, dismissIntent, 0);
    
                Intent replyIntent = new Intent(Intent.ACTION_VIEW,
                        Uri.parse("http://google.com"));
                PendingIntent piReply = PendingIntent.getActivity(this, 0, replyIntent, 0);
    
               builder.setStyle(new NotificationCompat.BigTextStyle()
                        .bigText(mUsers.get(0).getMessages().get(0)))
                        .addAction (R.drawable.ic_launcher,
                                "Cancel", piDismiss)
                        .addAction (R.drawable.ic_launcher,
                                "Reply", piReply);
    
            } else {
                int counter = 0;
                NotificationCompat.InboxStyle inboxStyle =
                        new NotificationCompat.InboxStyle();
    
                for (int i = 0; i < mUsers.size(); i++) {
                    for (int j = 0; j < mUsers.get(i).getMessages().size(); j++) {
                        inboxStyle.addLine(mUsers.get(i).getMessages().get(j) + (j++));
                        counter++;
                    }
                }
    
    
                inboxStyle.setBigContentTitle(counter + " Messages");
                builder.setStyle(inboxStyle);
                builder.setSubText("You got " + counter + " messages from " + mUsers.size() + " users");
                builder.setContentText(counter + " messages");
            }
    
            builder.setSmallIcon(R.drawable.ic_stat_notification);
    
            builder.setContentIntent(pendingIntent);
            builder.setAutoCancel(true);
    
            builder.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher));
    
            builder.setContentTitle("Updating Notification");
    
            NotificationManager notificationManager = (NotificationManager) getSystemService(
                    NOTIFICATION_SERVICE);
            notificationManager.notify(NOTIFICATION_ID, builder.build());
    }

     

    Github Link:- https://github.com/AmanChugh/Notifications_Demo-master

     

    Happy Coding. :D

 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: