This example shows how to create a simple notification that starts an application when the user clicks it.
Specify the notification's content:
Create the intent to fire on click:
Finally, build the notification and show it
Dynamically getting the correct pixel size for the large icon
If you're creating an image, decoding an image, or resizing an image to fit the large notification image area, you can get the correct pixel dimensions like so:
Heads Up Notification with Ticker for older devices
Here is how to make a Heads Up Notification for capable devices, and use a Ticker for older devices.
Here is what it looks like on Android Marshmallow with the Heads Up Notification:
Here is what it looks like on Android KitKat with the Ticker:
On all Android versions, the Notification is shown in the notification drawer.
Android 6.0 Marshmallow:
Android 4.4.x KitKat:
Ongoing notification with Action button
Register a broadcast receiver for the same action to handle action button click event.
Sometimes it is required to display a notification at a specific time, a task that unfortunately is not trivial on the Android system, as there is no method setTime() or similiar for notifications. This example outlines the steps needed to schedule notifications using the AlarmManager:
Add a BroadcastReceiver that listens to Intents broadcasted by the Android AlarmManager.
This is the place where you build your notification based on the extras provided with the Intent:
Register the BroadcastReceiver in your AndroidManifest.xml file (otherwise the receiver won't receive any Intents from the AlarmManager):
Schedule a notification by passing a PendingIntent for your BroadcastReceiver with the needed Intent extras to the system AlarmManager. Your BroadcastReceiver will receive the Intent once the given time has arrived and display the notification. The following method schedules a notification:
Please note that the 42 above needs to be unique for each scheduled notification, otherwise the PendingIntents will replace each other causing undesired effects!
Cancel a notification by rebuilding the associated PendingIntent and canceling it on the system AlarmManager. The following method cancels a notification:
Note that the 42 above needs to match the number from step 3!
Set custom notification - show full content text
If you want have a long text to display in the context, you need to set a custom content.
For example, you have this:
But you wish your text will be fully shown:
All you need to do, is to add a style to your content like below:
Set custom notification icon using `Picasso` library.
And then define a layout inside your layouts folder:
Setting Different priorities in notification
When notification contains image and you want to auto expand image when notification received use "PRIORITY_MAX", you can use other priority levels as per requirments
Different Priority Levels Info:
PRIORITY_MAX -- Use for critical and urgent notifications that alert the user to a condition that is time-critical or needs to be resolved before they can continue with a particular task.
PRIORITY_HIGH -- Use primarily for important communication, such as message or chat events with content that is particularly interesting for the user. High-priority notifications trigger the heads-up notification display.
PRIORITY_DEFAULT -- Use for all notifications that don't fall into any of the other priorities described here.
PRIORITY_LOW -- Use for notifications that you want the user to be informed about, but that are less urgent. Low-priority notifications tend to show up at the bottom of the list, which makes them a good choice for things like public or undirected social updates: The user has asked to be notified about them, but these notifications should never take precedence over urgent or direct communication.
PRIORITY_MIN -- Use for contextual or background information such as weather information or contextual location information. Minimum-priority notifications do not appear in the status bar. The user discovers them on expanding the notification shade.