Sunday, 11 June 2017

Android: BigPictureStyle Notification

Now a days, you must have seen a notification from many android apps that contain a picture/image along with the message. This notification with a big image is known as BigPicturestyle Notifications. These notifications are generally used for the promotional purpose of some brand or some product or you can say about an offer.
With help of V4 appcompat library, we can implement this for all devices from Android 2.0 onward.
Building this type of notification in android is very simple and can be implemented as follows:

1. First create Notification object and set the title, content text, ticker text etc as follows:
        
        //Create notification object and set the content.
        NotificationCompat.Builder nb= new NotificationCompat.Builder(this);
        nb.setSmallIcon(R.drawable.small_icon);
        nb.setContentTitle("Notification title");
        nb.setContentText("Notification content/message");
        nb.setTicker("Notification ticker text");

2. Now create a bitmap of the image which needs to be shown as big picture & set it as shown in below code. Set the summaryText, this will appear on expanding the notification.
        
        //get the bitmap to show in notification bar
        Bitmap bm = BitmapFactory.decodeResource(this.getResources(),R.drawable.big_icon);
        NotificationCompat.BigPictureStyle s = new NotificationCompat.BigPictureStyle().bigPicture(bm);
        s.setSummaryText("Notification text that appears on expanding the notification");
        nb.setStyle(s);

3. Now set the intent that should be launched on clicking on the notification.
        Intent resultIntent = new Intent(this, MainActivity.class);
        TaskStackBuilder taskBuilder = TaskStackBuilder.create(this);
        taskBuilder.addParentStack(MainActivity.class);
       
        // Adds the Intent that starts the Activity to the top of the stack
        taskBuilder.addNextIntent(resultIntent);
        PendingIntent resultPendingIntent =
                taskBuilder.getPendingIntent(0,PendingIntent.FLAG_UPDATE_CURRENT);
        nb.setContentIntent(resultPendingIntent);
        nb.setAutoCancel(true);
        NotificationManager mNotificationManager =
                (NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE);
        // mId allows you to update the notification later on.
        mNotificationManager.notify(11221, nb.build());

That's all.
Share and comment if any issues.


1 comment:

  1. Brujos en Santa Maria are believed to have special skills to help others. These brujos often use herbs, spells, and rituals to solve problems like love issues, health concerns, or luck. Some have shops where they sell items like candles and oils, while others offer personal readings and advice.

    ReplyDelete