在这里我把Notification的使用大概分为两点
1.使用Notification.Builder(通知构建器)构建一个通知,在其中设置他的相关属性,
android 8 及以上必须设置NotificationChannel(通知渠道),最后不要忘了调用.build()方法构建通知。
2.NotificationManager(通知管理器),管理器主要调用createNotificationChannel()方法创建通过渠道,
以及最后调用notify()方法发出通知。
布局文件:添加了三个Button实现三种通知的效果
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".NotificationActivity">
<Button
android:id="@+id/bt_notification_1"
android:text="普通通知"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<Button
android:id="@+id/bt_notification_2"
android:text="折叠式通知"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<Button
android:id="@+id/bt_notification_3"
android:text="悬挂式通知"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
代码如下(示例):
//在点击了通知后会跳转到百度网页
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.baidu.com"));
//pendingIntent可以看作一个对Intent的封装,不会立刻执行
PendingIntent pendingIntent = PendingIntent.getActivity(this,0,intent,0);
NotificationChannel mChannel = new NotificationChannel("001", "普通通知", NotificationManager.IMPORTANCE_LOW);
Notification notification = new Notification.Builder(this)
.setChannelId("001")//通知渠道id
.setContentTitle("普通通知")//标题
.setContentText("您有一项新活动")//内容
.setSmallIcon(R.drawable.ic_launcher_foreground)//小图标
.setContentIntent(pendingIntent)//设置内容跳转
.build();//构建通知
//获取通知管理器
NotificationManager notificationManager = (NotificationManager) this.getSystemService(NOTIFICATION_SERVICE);
notificationManager.createNotificationChannel(mChannel);//创建通知渠道
notificationManager.notify(0, notification);//发出通知
折叠式通知 (是一种自定义视图的Notification)用来显示长文本和
一些自定义的布局场景,在使用中就是多定义了RemoteViews,调用了notification1.bigContentView()方法
//在点击了通知后会跳转到百度网页
Intent intent = new Intent(Intent.ACTION_VIEW,Uri.parse("http://www.baidu.com"));
PendingIntent pendingIntent = PendingIntent.getActivity(this,1,intent,0);
Notification notification = new Notification.Builder(this)
.setContentTitle("折叠式通知")
.setContentText("您有一项新活动")
.setSmallIcon(R.drawable.ic_launcher_foreground)
.setContentIntent(pendingIntent)
.setChannelId("002")
.build();
//设置展开后的视图
RemoteViews remoteViews = new RemoteViews(getPackageName(),R.layout.view_inside);
notification.bigContentView = remoteViews;
NotificationChannel channel = new NotificationChannel("002","折叠式通知",NotificationManager.IMPORTANCE_LOW);
NotificationManager manager = (NotificationManager) this.getSystemService(NOTIFICATION_SERVICE);
manager.createNotificationChannel(channel);
manager.notify(1,notification);
展开后的布局:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:background="@drawable/aaa"
android:id="@+id/iv_notification"
android:layout_width="100dp"
android:layout_height="100dp"/>
<TextView
android:id="@+id/tv_notification"
android:text="这是展开后的视图"
android:layout_marginLeft="100dp"
android:layout_marginTop="30dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
代码如下(示例):
//在点击了通知后会跳转到百度网页
Intent intent = new Intent(Intent.ACTION_VIEW,Uri.parse("http://www.baidu.com"));
PendingIntent pendingIntent = PendingIntent.getActivity(this,2,intent,0);
Notification notification = new Notification.Builder(this)
.setContentIntent(pendingIntent)
.setSmallIcon(R.drawable.ic_launcher_foreground)
.setAutoCancel(true)//自动关闭
.setContentTitle("悬挂式通知")
.setContentText("您有一项新活动")
.setChannelId("003")
.setPriority(Notification.PRIORITY_HIGH)//设置该通知优先级
.setAutoCancel(true)//自动取消
.setDefaults(Notification.DEFAULT_VIBRATE)//默认设置震动
.build();
//NotificationChannel需要设置优先级IMPORTANCE_HIGH
NotificationChannel channel = new NotificationChannel("003","悬挂式通知",NotificationManager.IMPORTANCE_HIGH);
NotificationManager manager = (NotificationManager) this.getSystemService(NOTIFICATION_SERVICE);
manager.createNotificationChannel(channel);
manager.notify(1,notification);
提示:悬挂式通知需要在手机里开启悬浮通知权限
以上就是今天要讲的内容,本文仅仅简单介绍了Notification的使用。