Simple use of message notifications in Android

send notification

Message notifications can be created in Activity, BroadcastReceiver, and service, but no matter where they are created, the overall steps are basically the same. Android uses a notification channel to enable the notification function. Once a notification channel is set, it cannot be changed , so it is best to classify it carefully when creating a notification channel. After the channel is created, the Notification is sent through the channel. Using notifications in Android is generally divided into four steps.

In the first step, create channels and create NotificationManager to manage notification channels.

The as here is mandatory type conversion. Because the notification function was added in Android 8.0, it is necessary to judge the version. Build.VERSION_CODES.O represents the Android 8.0 version. The NotificationChannel method of creating a channel passes in three parameters, which are channelId (channel ID, write casually, As long as it is unique), channelName (the name of the channel, for users to see, just write it casually, and users can understand it), importance (importance, use it with caution, to harass users, there are mainly IMPORTANCE_HIGH, IMPORTANCE_DEFAULT, IMPORTANCE_LOW, IMPORTANCE_MIN these types, Messages of different levels will be pushed to users in different forms) , and then add this channel to the manager for easy management.

val manager=getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
   if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.O){
            val channel=NotificationChannel(channelId,channelName,importance)
            manager.createNotificationChannel(channel)
     }

The second step is to use a Builder constructor to create a Notification object, here we choose the constructor of the NotificationCompat class to create the object.

This constructor needs two parameters, context (context), channelId (channel ID, which must be consistent with the above, otherwise I don’t know which channel to send through) , and finally call the build method to create.

val notification=NotificationCompat.Builder(context,channelId).build()

The third step is to supplement the content of the notification before executing the build method of the Notification object, so as to enrich the notification message.

Fill in the content of the message and enrich the content of the notification. You can set the title, text content, display icon in the status bar, drop down the icon in the notification bar, and finally call the build method to complete the creation.

val notification=NotificationCompat.Builder(context,channelId)
            .setContentTitle("this is a notification")
            .setContentText("this is text")
            .setSmallIcon(R.drawable.small_icon)
            .setLargeIcon(BitmapFactory.decodeResource(resources,R.drawable.Large_icon))
            .build()
        }

The fourth step is to activate the notification message.

Use the notify method of the manager to activate the message and send it. This method requires two parameters, here 1 (represents the message ID, just write whatever you want, the only one is fine), and notification is the notification message we wrote above .

manager.notify(1,notification)

Click on the notification to enter the page

To realize the function of clicking a message to enter a certain page, PendingIntent is also used. It is similar to Intent, except that intent is executed immediately, while PendingIntent is executed at the right time. We use the getActivity method of PendingIntent to complete the click transmission event. This method has four parameters. The first parameter is the context, the second (I don’t know what it is, it is generally not used, just pass in 0), and the third One is the Intent we built, and the fourth one is used for the behavior of PendingIntent, which has 4 values. You can check it in the document when you use it. Generally, just pass 0. Add .setContextIntent(pi) to the message to write the event into the message, and when clicked, it will be sent to the page guided by our Intent. Then add .setAutoCancel(true) to make the message disappear automatically after being clicked.

val intent=Intent(this,NotificationActivity::class.java)
val pi=PendingIntent.getActivity(this,0,intent,0)
val notification=NotificationCompat.Builder(context,channelId)
            .setContentTitle("this is a notification")
            .setContentText("this is text")
            .setSmallIcon(R.drawable.small_icon)
            .setLargeIcon(BitmapFactory.decodeResource(resources,R.drawable.Large_icon))
            .setContextIntent(pi)
            .setAutoCancel(true)
            .build()
        }

Build rich text notifications

This requires the use of the setStyle method.

val intent=Intent(this,NotificationActivity::class.java)
val pi=PendingIntent.getActivity(this,0,intent,0)
val notification=NotificationCompat.Builder(context,channelId)
    .setContentTitle("this is a notification")
    .setContentText("this is text")
    .setSmallIcon(R.drawable.small_icon)
    .setLargeIcon(BitmapFactory.decodeResource(resources,R.drawable.Large_icon))
    .setContextIntent(pi)
    .setAutoCancel(true)
    .setStyle(NotificationCompat.BigTextStyle().bigText("Learn how to build notifications, send and sync data, and use voice actions. Get the official Android IDE and developer tools to build apps for Android."))
    .setStyle(NotificationCompat.BigPictureStyle().bigPicture(BitmapFactory.decodeResource(resources, R.drawable.big_image)))
    .build()
}

Before the build method, use the setStyle method to add a piece of text and a large picture, so that you can build rich text, like this.

 

 

Guess you like

Origin blog.csdn.net/liny70858/article/details/127273902