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 能保证通知系统在整个应用生命周期内正常工作

相关推荐
一只鹿鹿鹿几秒前
智慧水利一体化建设方案
大数据·运维·开发语言·数据库·物联网
_codemonster30 分钟前
数据库字符集编码问题
android·数据库·oracle
莫寒清1 小时前
Spring MVC:@RequestParam 注解详解
java·spring·mvc
没有医保李先生2 小时前
字节对齐的总结
java·开发语言
Elastic 中国社区官方博客2 小时前
使用 Elastic 进行网络监控:统一网络可观测性
大数据·开发语言·网络·人工智能·elasticsearch·搜索引擎·全文检索
Codefengfeng2 小时前
Python Base环境中加包的方法
开发语言·python
清水白石0082 小时前
《Python 编程全景解析:从核心精要到测试替身(Test Doubles)五大武器的实战淬炼》
开发语言·python
甲枫叶3 小时前
【claude】Claude Code正式引入Git Worktree原生支持:Agent全面实现并行独立工作
java·人工智能·git·python·ai编程
六件套是我3 小时前
无法访问org.springframeword.beans.factory.annotation.Value
java·开发语言·spring boot
LYS_06184 小时前
C++学习(5)(函数 指针 引用)
java·c++·算法