Android Notification with Kotlin

Tram Ho

1. Overview

Notifications are notifications or messages displayed to users outside of the phone app interface. Notification is displayed on the top of the screen if the device is unlocked or subject to the security settings on the lock screen when the phone is locked.

 

A typical Notification will include a title, a description, an icon. It can also have clickable actions, quick replies, expand the content inside, and possibly images.

Notification can provide timely information, and it can have buttons that allow users to perform quick actions, such as replying to a message or resetting an alarm. Clicking on Notification will take us to a view related to the notification content.

Notification is a useful way to remind users about an important task, show them what is going on, or communicate important information right away, while their application is in use. background (running in the background) j. Using Notification in moderation, this not only respects the user but also has the ability to bring Notification our application to receive the attention of users appropriately.

2. App Overview

You have never cooked eggs, simple right. It is actually a challenging task if you are not following time. We will make an application that allows users to set different cooking times for different types of eggs. The timer counts down from the selected interval and toast message when the egg is ready.

This seems to be more functional and not perfect and user friendly. Toast Message is displayed for a short period of time and is therefore easy to miss. In addition, the application is not in the foreground or the phone is locked, there will be no visual notification for the status of the timer when the toast message disappears.

Best Egg Timer should use Notification to notify users when time runs out. The user will know the eggs are ready and ready immediately, otherwise the eggs will not overcook. Notification is very intuitive, can include sound, can make the phone vibrate and attract users. This way we can achieve the perfect egg dish and the user will be happy.

3. Structure

The application will only include a single activity, MainActivity, with 3 packages: receiver, ui and util

  • The package receiver includes 2 broadcast receivers, AlarmReceiver and SnoozeReceiver. AlarmReceiver is enabled by AlarmManager to send notifications at the end of user-defined time. SnoozeReceiver handles when the user clicks the notification.
  • The ui package contains EggTimerFragment which is part of the application interface. EggTimerViewModel is responsible for starting and canceling timer cycles.
  • util package includes 2 files. BindingUtils.kt has binding adapters that allow data link between application UI and ViewModel. NotificationUtils.kt has extension methods

4. Add Notification into the application

Using notifications is a great way to get users to notice your app, whether or not your app runs in the foreground, the notification that appears on the screen can include images and sounds. and vibration.

4.1 Create a Basic Notification

In the NotificationUtils.kt file create senNotification function

Get an instance of the notification builder, which needs the app context and channel ID parameters. Channel ID is a String.

The Notification Channel is a way to group similar notifications. Once a Channel ID is created, it can be used to provide any number of notifications.

Note that with Android API level 26 and above, all notifications must have a channel

Add Icon, Title, Content for Notification.

Next we need to call notify () with a unique ID for the notification and with the Notification object from the builder

Open ui / EggTimerViewModel.kt and create startTimer () method. This method will generate an alarm with the egg timer interval

We will activate the notification when the user starts the timer. To call the previous setNotification method, we need an instance of NotificationManager. NotificationManager is a system service that provides all the functionality displayed for API notifications. Whenever you want to send, cancel, or upate a notification you need to request an instance of NotificationManager from the system.

4.2 Notification Channels

Call createNotificationChannel () in NotificationManager and pass notificationChannel which we created earlier.

To create a channel we need to call the createChannel () method. This method has 2 parameters that are channel ID and channel name.

We need to change the channel ID for the notification builder. In NotificationUtils.kt verify the channel ID again

Running the application, we will see the application has nofification every time we schedule it.

4.3 Add notification to the application

In AlarmReceiver.kt an instance for NotificationManager, and call sendNotification () with message and context parameters

4.4 Add an intent intent

In NotificationUtils.kt

Create pending Intent

4.5 Cancel Notification

In the NotificationsUtil.kt file add an extension function cancelAll ()

In the startTimer () method of EggTimerViewMode.kt

5. Custom Notification

5.1 Style for Notification

NotificationCompat provides the available styles:

  • BigTextStyle, can display a large block of text, such as displaying the content of an email when expanded.
  • BigPictureStyle, displays large format notifications including large image attachments.
  • InboxStyle, showing the message text type content.
  • MediaStyle, shows the media controls

Initially, the notification appears a small image, when we expand it to enlarge, like below.

5.2 Notification actions

You can add actions for Notification. A basic action is that when clicking on the notification will open the application.

A Notification can provide up to 3 buttons to give users quick feedback. But reset the alarm or reply to a message. These buttons do not have duplicate actions.

5.3 Notification importance

Depending on the importance, Notification may interrupt the user visually and clearly. There are 5 levels, from IMPORTANCE_NONE (0) to IMPORTANCE_HIGHT (4)

6. Summary and source code

Through the article I introduced through an application to apply Notification with Kotlin language. For you to read and follow is very difficult, so you can refer to the soure code below to better understand the source code download from google

Share the news now

Source : Viblo