android notification 的总结分析

yizhihongxing

Android Notification 的总结分析

概述

Android Notification 是 Android 系统中的一个提醒用户的机制。当应用程序需要提醒用户时,它可以创建一个 Notification 对象并在系统状态栏中显示。用户可以点击该 Notification 对象以打开应用程序或处理特定的任务。Android Notification 可以显示到应用程序在后台或者设备已锁定时。在本文中,我们将对 Android Notification 进行总结和分析。

Android Notification 的基本组成

  • 通知标题及内容:通知的标题和内容告诉用户通知所涉及的信息。
  • 时间戳:显示通知发布的时间(当然,你可以在通知后端设置)
  • 通知图标:一个小图标,用于标识通知来源。
  • 扩展内容:收件人可以下拉通知以获得完整内容,否则就只能看到标题和简短内容。
  • 通知行为:通知可以设置一些特定的操作。例如,当用户点击通知时应该打开哪个 Activity 或打开一个 Web 页面。

Android Notification 的创建流程

  1. 创建 NotificationChannel 对象
  2. 构建 NotificationCompat.Builder 对象
  3. 设置通知内容
  4. 策略性解析 PendingIntent 对象
  5. 将通知发送到状态栏

示例1:基本的文本通知

以一个普通的文本通知作为示例

  • 在 AndroidManifest.xml 文件中添加 Vibrate 和 WakeLock 权限:
<uses-permission android:name="android.permission.VIBRATE" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
  • 在 MainActivity 中创建 NotificationChannel 和 NotificationCompat.Builder 对象:
private val CHANNEL_ID = "default"

private fun createNotificationChannel() {
    // Create the NotificationChannel, but only on API 21+ because
    // the NotificationChannel class is new and not in the support library
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        val name = getString(R.string.channel_name)
        val descriptionText = getString(R.string.channel_description)
        val importance = NotificationManager.IMPORTANCE_DEFAULT
        val channel = NotificationChannel(CHANNEL_ID, name, importance).apply {
            description = descriptionText
        }
        // Register the channel with the system
        val notificationManager: NotificationManager =
                getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
        notificationManager.createNotificationChannel(channel)
    }
}

private fun sendNotification(messageBody: String) {
    val intent = Intent(this, MainActivity::class.java).apply {
        flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK
    }
    val pendingIntent: PendingIntent = PendingIntent.getActivity(this, 0, intent, 0)

    val builder = NotificationCompat.Builder(this, CHANNEL_ID)
            .setSmallIcon(R.drawable.ic_launcher_background)
            .setContentTitle(getString(R.string.notification_title))
            .setContentText(messageBody)
            .setPriority(NotificationCompat.PRIORITY_DEFAULT)
            .setContentIntent(pendingIntent)
            .setAutoCancel(true)

    with(NotificationManagerCompat.from(this)) {
        // notificationId is a unique int for each notification that you must define
        notify(0, builder.build())
    }
}
  • 在 onCreate 中调用 createNotificationChannel:
override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        createNotificationChannel()
    }
  • 在需要发送通知时调用 sendNotification :
sendNotification("This is the message body of the notification.")

示例2:添加扩展内容的通知

示例2 演示了如何向通知中添加扩展内容。

  • 在 MainActivity 中创建 NotificationCompat.Builder 对象,并添加扩展内容
private fun sendNotificationWithTextAndPicture(messageBody: String, pictureUrl: String) {
    val intent = Intent(this, MainActivity::class.java).apply {
        flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK
    }
    val pendingIntent: PendingIntent = PendingIntent.getActivity(this, 0, intent, 0)

    val notificationBuilder = NotificationCompat.Builder(this, CHANNEL_ID)
            .setSmallIcon(R.drawable.ic_launcher_background)
            .setContentTitle(getString(R.string.notification_title))
            .setContentText(messageBody)
            .setPriority(NotificationCompat.PRIORITY_DEFAULT)
            .setContentIntent(pendingIntent)
            .setAutoCancel(true)

    // Add the picture
    val picture =
            BitmapFactory.decodeStream(URL(pictureUrl).openConnection().getInputStream())
    notificationBuilder.setStyle(
            NotificationCompat.BigPictureStyle()
                    .bigPicture(picture)
                    .bigLargeIcon(null)
    )

    with(NotificationManagerCompat.from(this)) {
        // notificationId is a unique int for each notification that you must define
        notify(0, notificationBuilder.build())
    }
}
  • 在需要发送通知时调用 sendNotificationWithTextAndPicture :
sendNotificationWithTextAndPicture(
        "This is the message body of the notification with picture.",
         "https://www.example.com/picture.png")

总结

本文总结了 Android Notification 的基本组成和创建流程,以及示范了两个常见的用例。在实践中,你可以根据自己的需求对通知进行自定义。

本站文章如无特殊说明,均为本站原创,如若转载,请注明出处:android notification 的总结分析 - Python技术站

(0)
上一篇 2023年6月20日
下一篇 2023年6月20日

相关文章

  • MySQL中TEXT与BLOB字段类型的区别

    MySQL中TEXT与BLOB字段类型的区别 在MySQL中,TEXT和BLOB都是用来存储大型数据的字段类型。然而,它们之间仍然存在很重要的区别。 TEXT类型 TEXT类型用于存储长文本字符串,最大可存储65535个字符。除了存储普通文本之外,它还支持存储长文本,如XML、HTML和JSON等。 TEXT类型的列的语法 column_name TEXT …

    other 2023年6月25日
    00
  • 霍格沃茨之遗弹光敏警告时闪退怎么办 光敏警告闪退解决方法

    霍格沃茨之遗弹光敏警告时闪退怎么办 问题描述 在玩霍格沃茨之遗弹游戏时,当光敏警告出现时,游戏会出现闪退现象,导致无法正常游戏。该如何解决这个问题呢? 解决方法 1.禁用光敏警告 可以通过修改游戏配置文件来禁用光敏警告,从而避免游戏闪退。具体步骤如下: 找到游戏配置文件,通常在游戏安装目录下的 Config 文件夹中。 打开 Config 文件夹,找到并打开…

    other 2023年6月27日
    00
  • MFC对话框中添加状态栏的方法

    MFC是一种微软的C++类库,开发人员使用MFC可以方便地构建Windows图形化界面应用程序。在MFC对话框应用程序中,我们经常需要添加状态栏来显示当前程序的状态信息。下面是MFC对话框中添加状态栏的方法: 步骤1:打开对话框资源 首先需要打开对话框资源文件,可以通过如下步骤完成: 打开MFC对话框应用程序 在Solution Explorer窗口中找到R…

    other 2023年6月26日
    00
  • mybatis-plus中wrapper的用法实例详解

    MyBatis-Plus中Wrapper的用法实例详解 MyBatis-Plus是一个基于MyBatis的增强工具,提供了许多便捷的功能来简化数据库操作。其中,Wrapper是MyBatis-Plus中一个重要的概念,用于构建查询条件。 1. Wrapper简介 Wrapper是MyBatis-Plus中的一个查询条件构造器,它可以帮助我们动态地构建查询条件…

    other 2023年7月28日
    00
  • 服务机器人的小脑——SLAM技术

    服务机器人的小脑——SLAM技术的完整攻略 SLAM(Simultaneous Localization and Mapping)技术是指在未知环境中,同时实现机器人的自我定位和环境地图构建的技术。在服务机器人中,SLAM技术是实现机器人自主导航和环境感知的关键技术之一。本文将详细讲解SLAM技术的相关知识,包括SLAM技术的概念、算法和示例说明。 SLAM…

    other 2023年5月5日
    00
  • locate命令详解

    当然,我很乐意为您提供有关“locate命令详解”的完整攻略。以下是详细的步骤和两个示例: 1 locate命令详解 locate命令是Linux系统中的一个命令行工具,用于快速查找文件。它可以在系统中搜索文件名或路径,并返回匹配的文件列表。 2 locate命令的使用 以下是使用locate命令的方法: 2.1 安装locate命令 在大多数Linux发行…

    other 2023年5月6日
    00
  • Nginx服务器的location指令匹配规则详解

    Nginx服务器的location指令匹配规则详解 Nginx是一款高性能的Web服务器和反向代理服务器,它使用location指令来匹配URL,并根据匹配结果执行相应的操作。在本攻略中,我们将详细讲解Nginx服务器的location指令的匹配规则。 1. 精确匹配 精确匹配是最基本的location匹配规则,它使用=操作符进行匹配。示例如下: locat…

    other 2023年8月18日
    00
  • java中3种将byte转换为string的方法

    以下是关于“Java中3种将byte转换为String的方法”的完整攻略: 1. 使用String构造函数 Java中的String类提供了一个构造函数,可以将byte数组转换为String。该构造函数的语法如下: String(byte[] bytes) 示例: byte[] bytes = {72, 101, 108, 108, 111, 32, 87,…

    other 2023年5月6日
    00
合作推广
合作推广
分享本页
返回顶部