Notification
What is Notification?
A notification is a message that is used to Provide information and information about apps outside of app lifecycle.
You can use Notification to take some action using tap on Notification
Where is Notification Displayed?
Notification is Displayed in status bar, Notification Drawer, Heads-Up Notification and Lock Screen.
In Status bar and Notification Drawer Notification is displayed by default.
If you want to show Heads-up Notification It should be Important by nature
What are the Parts of Notification UI?
- Small Icon : Required , setSmallIcon()
- App Name: provided by system
- Time Stamp: setWhen(), setShowwhen(false) to hide
- Large Icon: optional, setLargeIcon()
- Title: optional, setContentTitle()
- Text: optional, setContentText()
What permission is required for showing Notification?
Android 13 (API level 33) and higher supports a runtime permission for sending non-exempt (including Foreground Services (FGS)) notifications from an app: POST_NOTIFICATIONS.
What classes are Required for Notification?
You need Notification , NotificationCompat.Builder, NotificationChannel(above android API 26 (8)), NotificationManager for creating and showing Notification.
What is a Notification Channel?
Notification Channel is basically a category which represents the type of Notification. You can add several notifications in Single channel.You can set the behavior of the notification channel which is applied to all the notification in that channel
When is the Notification Channel introduced?
It is introduced in android API version 26 and above (android version 8)
How to Create a Notification Channel?
To create Notification Channel you have to add create a Object of NotificationChannel and then create channel using NotificationManager
What parameters does NotificationChannel require?
NotificationChannel requires Channel ID, Channel Name and Importance of Channel
How to set the Importance of Notification Channel?
To set the importance , NotificationChannel Constructor is used , It can be set from IMPORTANCE_NONE(0) to IMPORTNCE_HIGH(4)
| Urgent Notifications make a sound and appear as heads-up notifications. | IMPORTANCE_HIGH
|
| High Notifications make a sound. | IMPORTANCE_DEFAULT
|
| Medium Notifications make no sound. | IMPORTANCE_LOW
|
| Low Notifications make no sound and do not appear in the status bar. | IMPORTANCE_MIN
|
How to set the behavior of NotificationChannel?
You can set Behavior like Enable light, Light color, Vibration and Channel description,
It can be done using this methods:
- enableLight(bool)
- setLightColor(int_color)
- enableVibration(bool)
- setDescription(string)
How to create a Notification Channel?
It can be created using createNotificationChannel(NotificationChannel) of NotificationManager class.
**if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {**
**NotificationManager mNotificationManager =**
** (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);**
**mNotifyManager.createNotificationChannel(notificationChannel);**
**}**
You can use this only above android version 8 and above , So mention condition always
Once you create a notification and notification channel and submit it to the NotificationManager, you cannot change the importance level using code.
How to create a Notification?
The NotificationCompat.Builder class is used to create Notification.
How to use NotificationCompat.Builder ?
The NotificationCompat.Builder constructor takes context and Notification channel id and returns its object, Now You can set notification properties like small icon,large icon,text,title etc using Builder object.
NotificationComapt.Builder.build() will return a Notification
What is the priority for Notification?
Priority can be set using setPriority() before android 7.1 and below
What does setAutoCancel() use for?
setAllowCancel() will automatically remove the notification when the user taps it.
How to set the intent for the notification's tap action?
Notification’s tap action can be defined by setting Content Intent using setContentIntent() on NotificationCompat Builder class.
This method takes the Pending Intent as a parameter.Here PendingIntent contains the Intent and action whatever you want to perform
How to Show the notification?
To make the notification appear, call[ NotificationManagerCompat.notify()](https://developer.android.com/reference/androidx/core/app/NotificationManagerCompat#notify(int, android.app.Notification)) or NotificationManager.notify() , passing it a unique ID for the notification and the result of NotificationCompat.Builder.build().
- To create an instance of
NotificationManager, callgetSystemService(), passing in the NOTIFICATION_SERVICE constant. - To create an instance of [ NotificationManagerCompat](https://developer.android.com/reference/androidx/core/app/NotificationManagerCompat#notify(int, android.app.Notification)), call
NotificationManagerCompat.from(this)
How to add action buttons in Notification?
You can use addAction() method to add action button in Notification.An action button needs the following components:
- An icon, to be placed in the notification.
- A label string, to be placed next to the icon.
- A
PendingIntent, to be sent when the user taps the notification action.
Ongoing notifications are notifications that the user can't dismiss. To make a notification ongoing, set setOngoing() to true.
Your app must explicitly cancel ongoing notifications by calling cancel() or cancelAll().
Can I start Foreground service from Notification even after the restriction on foreground service after android 12?
Yes. It is included in following exception cases:
- The user performs an action on a UI element related to your app. For example, they might interact with a bubble, notification, widget, or activity.
TODO:
- Custom Notification: allowed in content area only , set RemoteView in setCustomContentView, SetStyle with DecoratedCustomView , Android 12 and above don't allow full custom Notification layout. Ref: Create a Custom Notification Layout | Android Developers
- Expandable Notification
- Start an Activity from Notification
REF: