Notification in depth
THE CODE
Intent intent = new Intent(this, SecondActivity.class); PendingIntent pendingIntent = PendingIntent.getActivity(this,0,intent,0); NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context) .setSmallIcon(R.drawable.your_notification_icon) .setContentTitle("Notification Title") .setContentText("Notification ") .setContentIntent(pendingIntent ); NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); notificationManager.notify(0, mBuilder.build());
Depth of knowledge
Notification can be built using Notification. Classes Builder or NotificationCompat.Builder.
But if you need backward compatibility, you should use the NotificationCompat.Builder class, as it is part of the v4 support library, as it takes care of hard work to ensure consistent appearance and functionality of Notification for API 4 and higher.
Basic notification features
The notification has 4 main properties (3 main display properties + 1 click action property)
- Small icon
- title
- Text
- Button Click Event (Click Click Notification Event)
A button click event becomes optional on Android 3.0 and above. This means that you can create your notification using only the properties of the screen if your minSdk is oriented to Android 3.0 or higher. But if you want your notification to run on older devices than Android 3.0, you must provide a Click event, otherwise you will see an IllegalArgumentException.
Notification display
Notifications are displayed by calling the notify () method of the NotificationManger class
notify () options
There are two options for the notification method.
notify(String tag, int id, Notification notification)
or
notify(int id, Notification notification)
The notify method accepts an integer identifier to uniquely identify your notification. However, you can also provide an optional String tag to further identify your notification in the event of a conflict.
This type of conflict is rare, but let's say you created a library, and other developers use your library. Now they create their own notification, and somehow your notification and the notification identifier of another developer are the same, then you will run into a conflict.
Notification after API 11 (more control)
API 11 provides additional control over the behavior of notifications
Dismissal notice
By default, if a user clicks on a notification, he executes the designated click event, but does not delete the notification. If you want your notification to be cleared then you must add this
mBuilder.setAutoClear (true);
Prevent user from rejecting notifications
The user can also reject the notification by posting it. You can disable this default behavior by adding it when creating a notification
mBuilder.setOngoing (true);
Post Notification
You can set the relative priority of your notification.
mBuilder.setOngoing (int pri);
If your application runs with a lower API than 11, then your notification will work without the above additional features. This is the advantage of choosing NotificationCompat.Builder over Notification.Builder.
Notification after API 16 (more informative)
With the introduction of API 16, notifications have received many new features.
Notification can be much more informative.
You can add a large picture to your logo. Say you received a message from a person now with mBuilder.setLargeIcon (Bitmap), which you can display a photograph of that person. Thus, in the status bar you will see an icon, when scrolling you will see a photo of a person instead of an icon. There are other features.
- Add counter in notification
- Ticker message when you see a notification for the first time
- Expandable notification
- Multiline notification, etc.