Android 控件 - 最简单的 Notification、Application Context 应用于 Notification

最简单的 Notification

1、演示
java 复制代码
final String CHANNEL_ID = "my_test_channel";
int notificationId = 1;

NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
    CharSequence name = "my test channel";
    int importance = NotificationManager.IMPORTANCE_DEFAULT;

    NotificationChannel channel = new NotificationChannel(CHANNEL_ID, name, importance);

    notificationManager.createNotificationChannel(channel);
}

Notification notification = new NotificationCompat.Builder(this, CHANNEL_ID)
        .setContentTitle("系统通知")
        .setContentText("Hello World")
        .setSmallIcon(R.drawable.ic_notification)
        .build();

notificationManager.notify(notificationId, notification);
java 复制代码
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
2、解读
java 复制代码
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
  1. 获取通知管理器
java 复制代码
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {

    ...

}
  1. 如果系统版本大于 Android 8.0(API 26),必须创建通知渠道才能发送通知
java 复制代码
CharSequence name = "my test channel";
int importance = NotificationManager.IMPORTANCE_DEFAULT;

NotificationChannel channel = new NotificationChannel(CHANNEL_ID, name, importance);
  1. 创建通知渠道,所需参数:通知渠道的唯一标识符、通知渠道的显示名称、通知渠道的重要等级
java 复制代码
notificationManager.createNotificationChannel(channel);
  1. 将通知渠道注册到系统,这个操作只需在应用首次运行时执行一次即可
java 复制代码
Notification notification = new NotificationCompat.Builder(this, CHANNEL_ID)
  1. 创建通知,传入通知渠道的唯一标识符,指明该通知属于哪个通知渠道
java 复制代码
.setContentTitle("系统通知")
.setContentText("Hello World")
.setSmallIcon(R.drawable.ic_notification)
  1. 设置通知的标题,设置通知的正文内容,设置通知的图标
java 复制代码
notificationManager.notify(notificationId, notification);
  1. 发送通知,传入通知的唯一标识符

Application Context 应用于 Notification

1、演示
java 复制代码
Context context = getApplicationContext();

final String CHANNEL_ID = "my_test_channel";

NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
    NotificationChannel channel = new NotificationChannel(CHANNEL_ID, "my test channel", NotificationManager.IMPORTANCE_DEFAULT);
    notificationManager.createNotificationChannel(channel);
}

Notification notification = new NotificationCompat.Builder(context, CHANNEL_ID)
        .setContentTitle("系统通知")
        .setContentText("Hello World")
        .setSmallIcon(R.drawable.ic_notification)
        .build();

notificationManager.notify(1, notification);
java 复制代码
Context context = getApplicationContext();

final String CHANNEL_ID = "my_test_channel";

NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
    NotificationChannel channel = new NotificationChannel(CHANNEL_ID, "my test channel", NotificationManager.IMPORTANCE_DEFAULT);
    notificationManager.createNotificationChannel(channel);
}

Intent intent = new Intent(context, TargetActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_IMMUTABLE);

Notification notification = new NotificationCompat.Builder(context, CHANNEL_ID)
        .setContentTitle("系统通知")
        .setContentText("Hello World")
        .setSmallIcon(R.drawable.ic_notification)
        .setContentIntent(pendingIntent)
        .build();

notificationManager.notify(1, notification);
2、小结
  1. 通知系统会持有 Notification Builder 中的 Context

  2. 如果使用 Activity Context,即使 Activity 被销毁,通知系统仍会持有它的引用

  3. 通知的生命周期通常比 Activity 更长,即使应用在后台,通知也应能正常显示

  4. Application Context 能保证通知系统在整个应用生命周期内正常工作

相关推荐
翊谦11 小时前
Java Agent开发 Milvus 向量数据库安装
java·数据库·milvus
晓晓hh11 小时前
JavaSE学习——迭代器
java·开发语言·学习
Laurence11 小时前
C++ 引入第三方库(一):直接引入源文件
开发语言·c++·第三方库·添加·添加库·添加包·源文件
查古穆12 小时前
栈-有效的括号
java·数据结构·算法
kyriewen1112 小时前
你点的“刷新”是假刷新?前端路由的瞒天过海术
开发语言·前端·javascript·ecmascript·html5
Java面试题总结12 小时前
Spring - Bean 生命周期
java·spring·rpc
硅基诗人12 小时前
每日一道面试题 10:synchronized 与 ReentrantLock 的核心区别及生产环境如何选型?
java
014-code12 小时前
String.intern() 到底干了什么
java·开发语言·面试
421!12 小时前
GPIO工作原理以及核心
开发语言·单片机·嵌入式硬件·学习
摇滚侠13 小时前
JAVA 项目教程《苍穹外卖-12》,微信小程序项目,前后端分离,从开发到部署
java·开发语言·vue.js·node.js