6. Notification

6. Notification
通知

6.1 两个对象
Notification和NotificationManager

NotificationManager类是一个通知管理器类,由系统维护的服务,是以单例模式的方式获得,一般不直接实例化这个类。

在Activity中,可以使用Activity.getSystemService(String)方法获取NotificationManager对象,Activity.getSystemService(String)方法可以通过Android系统级服务的句柄,返回对应的对象。在这里需要返回NotificationMansger,所以直接传递Context.NOTIFICATION_SERVICE即可。

6.2 使用通知
使用Builder构造器创建Notification对象

使用NotificationCompat类的Builder构造器来创建Notification对象,可以保证程序在所有的版本上都能正常工作。Android8.0新增了通知渠道这个概念,如果没有设置,则通知无法在Android8.0的机器上显示

在这里插入图片描述

NotificationChannel → 通知渠道:Android8.0引入,允许为要显示的每种通知类型创建用户自定义的渠道。

通知重要程度:

在这里插入图片描述

属性    含义
NONE    关闭通知
MIN    开启通知,不会弹出,没有提示音,状态栏不显示
LOW    开启通知,不弹出,没有提示音,状态栏显示
DEFAULT    开启通知,不弹出,有提示音,状态栏显示
HIGH    开启通知,会弹出,有提示音,状态栏中显示
6.3 Notification对象的常见方法
方法    含义
setContentTitle    设置标题
setContentText    设置文本内容
setSmallIcon    设置小图标
setLargeIcon    设置通知的大图标
setColor    设置小图标的颜色
setContentIntent    设置点击通知后的跳转意图
setAutoCancel    设置点击通知后自动清除通知
setWhen    设置通知被创建的时间《默认当前时间》
注意点:Android从5.0开始,所有应用程序的通知栏图标,应该只使用alpha图层进行绘制,不应该包括RGB,即不能带颜色

6.4 演示

//获取NotificationManager对象
NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

//判断版本
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.O ){
    NotificationChannel notificationChannel = new NotificationChannel("dingjiaxiong","测试通知",
            NotificationManager.IMPORTANCE_HIGH);
    manager.createNotificationChannel(notificationChannel);
}

//构建Notification对象
Notification notification = new NotificationCompat.Builder(this,"dingjiaxiong")
        .setContentTitle("官方通知")
        .setContentText("年年有风,风吹年年")
        //小
        .setSmallIcon(R.drawable.ic_baseline_person_24)
        .build();
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    >

    <Button
        android:id="@+id/send_button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="发出通知"
        />

    <Button
        android:id="@+id/cancel_button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="取消通知"
        />

</LinearLayout>

设置全局变量

在这里插入图片描述

设置点击事件

//发出通知
send.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
        manager.notify(1,notification);
    }
});

在这里插入图片描述

设置其他属性

大图标

在这里插入图片描述

//大图标
.setLargeIcon(BitmapFactory.decodeResource(getResources(),R.drawable.test1))

在这里插入图片描述

在这里插入图片描述

创建pendingintent

Intent intent = new Intent(this, NotificationActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 0);



//构建Notification对象
notification = new NotificationCompat.Builder(this, "dingjiaxiong")
        .setContentTitle("官方通知")
        .setContentText("年年有风,风吹年年")
        //小图标
        .setSmallIcon(R.drawable.ic_baseline_person_24)
        //大图标
        .setLargeIcon(BitmapFactory.decodeResource(getResources(),R.drawable.test1))
        //设置小图标颜色
        .setColor(Color.parseColor("#ff0000"))
        //设置点击跳转意图
        .setContentIntent(pendingIntent)
        //设置点击通知后自动清除通知
        .setAutoCancel(true)
        .build();

在这里插入图片描述

主动取消通知

在这里插入图片描述

id与上一个需要一致

 

posted @ 2022-09-12 16:33  随遇而安==  阅读(65)  评论(0编辑  收藏  举报