android notification 的总结分析

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 的基本组成和创建流程,以及示范了两个常见的用例。在实践中,你可以根据自己的需求对通知进行自定义。

阅读剩余 67%

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

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

相关文章

  • C语言动态内存管理的原理及实现方法

    C语言动态内存管理的原理及实现方法 动态内存管理是C语言中非常重要的概念,它允许程序在运行时动态地分配和释放内存。本文将详细讲解C语言动态内存管理的原理及实现方法,并提供两个示例说明。 原理 C语言中的动态内存管理是通过以下几个函数来实现的: malloc(size_t size):用于分配指定大小的内存块,并返回指向该内存块的指针。 calloc(size…

    other 2023年7月31日
    00
  • C#读写配置文件方式(config.ini)入门

    下面我将详细讲解C#读写配置文件方式(config.ini)入门的完整攻略。 1. 什么是配置文件 配置文件是一个文本文件,用于保存程序运行时需要使用的配置信息。通常情况下,我们会将程序中的一些可变设置存储在这个文件中,以便于用户在后续的使用中进行修改。 2. 配置文件的格式 在C#中,常用的配置文件格式有INI、XML和JSON等。INI格式的配置文件通常…

    other 2023年6月25日
    00
  • linux下安装wireshark

    简介 Wireshark是一款开源的网络协议分析工具,可以用于捕获和分析网络数据包。在本攻略中,我们将介绍如何在Linux下安装Wireshark,并提供两个示例说明。 步骤 以下是在Linux下安装Wireshark的步骤。 步骤1:更新软件包列表 在安装Wireshark之前,我们需要更新软件列表。我们按照以下步骤更新软件包列表: 打开终端。 我们可以打…

    other 2023年5月6日
    00
  • Javascript数组常用方法你都知道吗

    Javascript数组常用方法攻略 什么是Javascript数组? Javascript中,数组(Array)是一种非常重要的数据类型,它可以用来存储一组有序的数据。一个数组是一个有序的数据集合,每个数据项可以是任意类型的数据。数组中的每个元素都有一个与之对应的数字键,可以通过这个键值来访问对应的元素。 Javascript数组常用方法 Javascri…

    other 2023年6月25日
    00
  • c与c++之间的相互调用及函数区别示例详解

    相关基础知识 在介绍 C 和 C++ 之间相互调用的过程之前,需要梳理一下 C 和 C++ 函数的区别。 C 函数和 C++ 函数的定义和调用有以下区别: 函数重载 C++ 支持函数重载,即同名函数的参数个数和类型不同,可以被认为是不同的函数。例如: // C++ 中使用函数重载 int sum(int a, int b) { return a + b; }…

    other 2023年6月26日
    00
  • ubuntu查看进程

    ubuntu查看进程 在使用 Ubuntu 的过程中,经常需要查看当前运行的进程情况,以便于监控和管理系统。 下面介绍两种常见的方法来查看 Ubuntu 中的进程。 1. 使用命令行 可以通过在命令行下使用 ps 命令来查看当前运行的进程。 # 查看当前所有进程 ps -ef # 查看指定进程 ps -p [进程号] 其中,-e 参数表示显示所有进程;-f …

    其他 2023年3月28日
    00
  • Solr全文检索框架

    Solr全文检索框架的完整攻略 Solr是一个基于Lucene的全文检索框架,可以用于快速、准确地搜索和分析大量文本数据。在本文中,我们将提供一个完整的Solr全文检索框架攻略,包括Solr的基本概念、配置和使用方法,并提供两个示例说明。 Solr的基本概念 Solr的基本概念包括以下几个方面: 文档(Document):Solr中的文档是指需要进行检索的数…

    other 2023年5月5日
    00
  • Android模拟美团客户端进度提示框

    Android模拟美团客户端进度提示框攻略 1. 创建进度提示框布局 首先,我们需要创建一个布局文件来定义进度提示框的外观。在res/layout目录下创建一个名为progress_dialog.xml的文件,并添加以下代码: <RelativeLayout xmlns:android=\"http://schemas.android.com…

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