文章目录
一、通知
在Android 8.0(API26)
后对通知更改了一些内容,以前是通过Notification,Builder(Context context).se..
来设置通知的震动、灯光、音效的设置,新内容加了NotificationChannel
(通知渠道),通过NotificatonChannel
来进行震动、灯光、音效的设置,且通知必须添加通知渠道,同样需进行版本判断,否则通知不会被发送。
步骤:
- 首先创建
NotificationManager
对象 - 创建
Notificationchannel
对象 - 通过
Notificationcompat.Builder(Context.Notification)
对象指定channel
并设置通知的各项信息,调用build()
方法创建Notifcation
对象 - 调用
NotificationManager
的notify(id,Notification)
方法发送通知
1. 申请权限
在 AndroidManifest.xml
文件中声明权限
<uses-permission android:name="android.permission.POST_NOTIFICATIONS"/>
在运行时请求权限
if (ContextCompat.checkSelfPermission(this, Manifest.permission.POST_NOTIFICATIONS)
!= PackageManager.PERMISSION_GRANTED) {
// 权限尚未授予,申请权限
ActivityCompat.requestPermissions(this,
new String[]{
Manifest.permission.POST_NOTIFICATIONS},1);
}
2. 创建通道
使用NotificationChannel
类创建通知通道的实例
创建通知通道:
- 设置唯一的id
- 通道的用户可见的名称
- 通道的重要性
private static final String MESSAGES_CHANNEL = "default_channel";
private void createMessagesNotificationChannel(Context context) {
// 检查当前设备的系统版本是否大于或等于 Android 8.0 (API 级别 26)
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
// 从资源中获取通知渠道的名称
CharSequence name = context.getString(R.string.message_channel_name);
// 创建一个通知渠道对象
NotificationChannel channel = new NotificationChannel(
MESSAGES_CHANNEL, // 渠道 ID,用于标识这个通知渠道
name, // 渠道名称,从资源文件中获取
NotificationManager.IMPORTANCE_HIGH // 渠道的重要性级别,决定通知的行为和显示方式
);
// 获取 NotificationManager 系统服务
NotificationManager manager = context.getSystemService(NotificationManager.class);
// 创建通知渠道
manager.createNotificationChannel(channel);
}
}
这样后就创建了渠道,下来直接构建通知然后发送即可
3. 创建通知
public void onClick(View v) {
NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
Notification notification = new NotificationCompat.Builder(this, MESSAGES_CHANNEL)
.setContentTitle("重要通知")
.setContentText("您今天睡眠时间不足")
.setWhen(System.currentTimeMillis())
.setSmallIcon(R.drawable.messages_1)
.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.messages_2))
.build();
manager.notify(1, notification);
}
4. 发送通知
使用NotificationManager
类发送通知
- 调用
getSystemService()
方法创建NotificationManager
的对象 - 调用它的
notify()
方法发送消息,传递通知id
和Notification
对象
manager.notify(1, notification); // 参数 1 是通知的 ID,用于更新或取消通知,notification 是要显示的通知对象
拓展功能
点击行为
所有通知在点击后都必须作出响应,一般是启动一个Activity
-
调用
setContentlntent()
方法设置一个内容Intent
; -
然后通过一个
PendingIntent
对象传递内容Intent
public void onClick(View v) {
//----在这里设置PendingIntent----
Intent intent = new Intent(this, NotificationActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(
this,
0,
intent,
PendingIntent.FLAG_IMMUTABLE
);
NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
Notification notification = new NotificationCompat.Builder(this, MESSAGES_CHANNEL)
.setContentTitle("重要通知")
.setContentText("您今天睡眠时间不足")
.setWhen(System.currentTimeMillis())
.setSmallIcon(R.drawable.messages_1)
.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.messages_2))
.setContentIntent(pendingIntent)// 设置点击通知时的 PendingIntent
.setAutoCancel(true)// 点击通知后自动取消
.build();
manager.notify(1, notification);
}
public static PendingIntent getActivity
(Context context, int requestCode, Intent intent, int flags)
PendingIntent
是一种封装了 Intent
的机制,可以在将来某个时刻代替应用程序的进程执行特定的操作,即使应用程序不在运行中。
flags
: 标志位,指定 PendingIntent
的行为。常用标志包括:
通常使用FLAG_IMMUTABLE
PendingIntent.FLAG_UPDATE_CURRENT
: 如果PendingIntent
已存在,更新其内容。PendingIntent.FLAG_CANCEL_CURRENT
: 如果PendingIntent
已存在,先取消它,然后创建一个新的。PendingIntent.FLAG_IMMUTABLE
: 使PendingIntent
不可变,即创建后不会被修改。PendingIntent.FLAG_MUTABLE
: 使PendingIntent
可变,即创建后可以被修改。
更新通知
- 使用
NotificationCompat.Builder
构造其创建有更新内容的通知 - 将相同的通知
Id
和更新内容后的通知传递给notify()
方法即可实现更新- 若之前的通知仍然可见,则系统更新
- 若之前的通知已被取消,则发送新的通知
取消通知
通知会一直保存可见,以下操作会使消息取消
- 用户通过滑动或使用"清除所有"取消它
- 创建通知时调用
setAutoCancel()
,那么当用户单击通知时会从状态栏消失 - 通过
NotificationManager
的cancel(通知ld)
取消,或调用cancelAll()
取消所有通知
锁屏通知
Android 5.0(APl21)开始,通知可以显示在锁屏上。用户可以通过设置选择是否允许敏感的通知内容显示在安全的锁屏上。
android5.0加入一种新的模式Notification的显示等级,共有三种:
- VISIBILITY_PUBLIC 任何情况都会显示通知
- VISIBILITY_PRIVATE 显示基本信息隐藏通知内部信息
- VISIBILITY SECRET 在没有锁屏的情况下才能够显示
- 调用
setVisibility
方法设置
builder.setVisibility(Notification.VISIBILITY_PUBLIC);
富文本通知
富文本通知指的是可以展示复杂内容的通知,例如包含多种样式的文本、图片、链接等。要创建富文本通知,可以使用
NotificationCompat
类的一些方法和样式。
setStyle(new NotificationCompat.BigTextStyle())
- 使用
BigTextStyle
:BigTextStyle
用于显示大段文本内容,这些内容会在展开时显示完整。
Notification notification = new NotificationCompat.Builder(this, MESSAGES_CHANNEL)
.setContentTitle("富文本通知")
.setContentText("点击查看详细内容")
.setWhen(System.currentTimeMillis())
.setSmallIcon(R.drawable.messages_1)
.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.messages_2))
.setStyle(new NotificationCompat.BigTextStyle()
.bigText("这是一个长文本示例。在展开通知后,可以看到更多的内容," +
"而不是只显示简短的预览文本。这样可以展示更详细的信息。"))
.setContentIntent(pendingIntent)
.setAutoCancel(true)
.build();
-
使用
BigPictureStyle
:BigPictureStyle
用于显示一张大图片,可以同时显示文本。 -
使用
InboxStyle
:InboxStyle
用于显示一组消息的列表,适合显示多个简短的消息。 -
MessageingStyle通知
使用
MessagingStyle
的通知可以展示一系列的消息,仿佛是一个对话线程,让用户能够在通知栏中查看消息交流的上下文。
// 创建 MessagingStyle 对象
NotificationCompat.MessagingStyle messagingStyle = new NotificationCompat.MessagingStyle("聊天机器人")
.addMessage("你好!有什么我可以帮助你的吗?", System.currentTimeMillis(), "聊天机器人")
// 添加一条消息,内容为“你好!有什么我可以帮助你的吗?”,时间为当前时间,发送者为“聊天机器人”
.addMessage("我想知道今天的天气怎么样。", System.currentTimeMillis(), "用户")
// 添加另一条消息,内容为“我想知道今天的天气怎么样。”,时间为当前时间,发送者为“用户”
.setConversationTitle("天气咨询");
// 设置对话标题为“天气咨询”,用来标识对话的主题
// 创建通知
Notification notification = new NotificationCompat.Builder(this, MESSAGES_CHANNEL)
.setContentTitle("消息通知")
.setContentText("您有新的消息")
.setWhen(System.currentTimeMillis())
.setSmallIcon(R.d