Big Picture Expandable Notifications for Android

Expandable Notifications for Android – Examples and Source

Expandable Notifications

Jelly Bean we not have the ability to provide more functionality such as expandable notifications and actions buttons.  We can use the rich styles provided or even create our own custom style for our expandable notifications.  This gives us the flexibility in both size and layout to create the perfect expandable notifications.

NotificationManager

Android Notifications are created by the Notification class.  Before you utilize the Notification class you first need to declare NotificationManager to be able to get the proper Context from the activity or service depending on your application setup.

Builder classes

Notification.Builder – This class was added in Android 3.0 Honeycomb [API 11].  So if you need to support older SDK’s you will need to use NotificationCompact instead.

NotificationCompat.Builder – This class is in the version 4 Support Library (compatible Android 1.6 and up).

The NotificationCompact.Builder, similar to other class builders, provides an interface that is used to create a Notification object. You use a PendingIntent to specify the intent action which should be shown when the user is notified.  The PendingIntent will be used almost like a token,  passed to the builder using setContentIntent. If you are in need of some extra actions, like the archive button in gmail notificaitons, you can specify three of these action buttons by using the builders addAction.  Once you have the notification built, the next step you will see how it is called.

In Android 3.0 the app navigation using the back key changed.  You can read more about this here in the TaskStackBuilder Class Overview.  This Class was not introduced until API 16 so if you do implement this recommended convention for the back key

make sure you use the TaskStackBuilder located in the Support Library.  This will ensure you have backwards capability for constructing synthetic back stacks for cross-task navigation on Android 3.0 and newer.

Notification views

Normal View – A notification in normal view appears in an area that’s up to 64 dp tall. Even if you create a notification with a big view style, it will appear in normal view until it’s expanded.

  1. Content title
  2. Large icon
  3. Content text
  4. Content info
  5. Small icon
  6. Notification time

Big View – A notification’s big view appears only when the notification is expanded, which happens when the notification is at the top of the notification drawer, or when the user expands the notification with a gesture.  Expanded notifications were first introduced in Android 4.1 JellyBean [API 16].  Expandable notifications were designed to support rich notification style objects called Notification.Style.

  1. Details area

Creating Notifications

Normal View

One point to remember is that all notification objects, including a Normal View, are required to have a small icon, a title, and detail text.

Normal Notification for Android

Big View

You will use this style the most when setting up expandable notifications.  This Notification.Style class contains three direct subclasses which are:

  1. Big Text Style – Displays a large text block to show the user more details on the item at hand.
  2. Big Picture Style – Displays a bitmap up to 256 dp tall similar to a screenshot notification.
  3. Inbox Style – Displays rows of text like a listView similar to the Gmail notification for multiple emails.

To apply a rich notification style to the notification, you first need to create the style object itself.  For this example I am showing you the BigPictureStyle subclass.

Big Picture Expandable Notifications for Android

 

Custom View

So what if the rich notification styles don’t provide you with the layout you need?  Easy, just create your own layout and pass it to the builder.  One little fact to retain is that notifications use remote views, which means you need to create a layout using a RemoteView.  Below is exactly how you would need to create this custom RemoteView.  When using a custom view, it will still function like the other expandable notifications we talked about.

First you need to create your layout file, here is one for an example.

Next create the RemoteView that uses your custom layout and apply it RemoteView using bigContentView.

Custom View Expandable Notifications for Android

 

Source Code

https://github.com/srafx/Notifications

  • Eliran Lazar

    Hi Steve, thank you for this great post.
    I want to recommend your readers to try my Push Notifications service – PushApps
    http://www.pushapps.mobi
    With PushApps you can easily create wonderful android custom view push notifications.

    You can see how easy it is in this link –
    http://wiki.pushapps.mobi/pages/viewpage.action?pageId=5407074

    Happy coding :)

  • TheRealChx kay

    You failed to mention which API levels are supported.

    • stevealbright

      Yeah, that is a good point. This year I plan on writing a lot more and notifications will get an update. I will elaborate on what’s supported where along with limitations. Thanks for reading!

  • Peter

    Is it possible to add custom views in the custom layouts? like MyTextView extending TextView…

    • Sua Mãe Aquela Honesta Senhora

      Nope

  • sc_calcutta

    will it work if app is in background