1. 创建通知
使用Notification.Builder来创建,低版本的可以使用NotificationCompat.Builder来替代。
Notification notification = new Notification.Builder(this)
.setSmallIcon(R.drawable.small)
.setContentTitle("标题")
.setContentText("内容")
.setTicker("您有新消息") // 状态栏显示信息
.setAutoCancel(true) // 点击自动退出
.build();
如果调用setLargeIcon()
方法,小图标会放在右下角。
2. 发送和取消通知
NotificationManager是所有通知的管理类,负责发送通知、消除通知等操作。
NotificationManager nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
NotificationManager常用方法
public void notify(int id, Notification notification) // 发送通知
public void cancel(int id) // 取消通知
public void cancelAll() // 取消所有通知
3. 点击事件
通知通过设置PendIntent来定义点击事件。
// 点击事件
Builder.setContentIntent(PendingIntent)
// 删除事件
Builder.setDeleteIntent(PendingIntent)
PendingIntent用于延迟的Intent,主要通过三种方式获取
// 启动Activity
public static PendingIntent getActivity(Context context, int requestCode,
Intent intent, @Flags int flags)
// 启动Service
public static PendingIntent getService(Context context, int requestCode,
@NonNull Intent intent, @Flags int flags)
// 发送广播
public static PendingIntent getBroadcast(Context context, int requestCode,
Intent intent, @Flags int flags)
PendingIntent的flags
- FLAG_ CANCEL_ CURRENT:如果当前系统中已经存在相同的PendingIntent对象,那么取消原有的,重新生成一个PendingIntent对象。
- FLAG_ NO_ CREATE:如果当前系统中不存在相同的PendingIntent对象,系统将不会创建该PendingIntent对象而是直接返回null。
- FLAG_ ONE_ SHOT:该PendingIntent只作用一次。在该PendingIntent对象通过send()方法触发过后,PendingIntent将自动调用
cancel()
进行销毁,那么如果你再调用send()
方法的话,系统将会返回SendIntentException。 - FLAG_ UPDATE_ CURRENT:如果系统中有一个和你描述的PendingIntent对等的PendingInent,那么系统将使用该PendingIntent对象,但是会使用新的Intent来更新之前PendingIntent中的Intent对象数据,例如更新Intent中的Extras。
判断两个相同的PendingIntent对象,主要是依据requestCode
和intent.filterEquals()
是否相同。
Intent intentA = new Intent();
intentA.putExtra("paramA", "paramA");
Intent intentB = new Intent();
intentB.putExtra("paramB", "paramB");
// intentA.filterEquals(intentB) 返回true;
4. 通知的提醒方式
声音提醒
// 默认
notification.defaults |= Notification.DEFAULT_SOUND;
// 指定声音
notification.sound = Uri.parse("file:///xxxx");
震动提醒
// 默认
notification.defaults |= Notification.DEFAULT_VIBRATE;
// 指定震动效果
long[] vibrate = {0, 200, 300, 400};
notification.vibrate = vibrate;
闪烁提醒
// 默认
notification.defaults |= Notification.DEFAULT_LIGHTS;
notification.ledARGB = 0xff00ff00; // 灯的颜色
notification.ledOnMS = 300; // 灯显示的毫秒数,300毫秒
notification.ledOffMS = 1000; // 灯关闭的毫秒数,1000毫秒
notification.flags |= Notification.FLAG_SHOW_LIGHTS; // 必须加上这个标志
5. 自定义remoteview
使用RemoteViews来自定义通知。
RemoteViews remoteViews = new RemoteViews("com.blog.demo",
R.layout.layout_notification_remote_view);
remoteViews.setTextViewText(R.id.tv_name, "Jack");
remoteViews.setTextViewText(R.id.tv_address, "BeiJing");
notification.contentView = remoteViews;
布局文件layout_notification_remote_view.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="40dp"
android:background="#ffa6a5aa">
<TextView
android:id="@+id/tv_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_alignParentLeft="true"
android:layout_marginLeft="15dp"
android:textSize="16sp"
android:textColor="#ffffffff" />
<TextView
android:id="@+id/tv_address"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_alignParentRight="true"
android:layout_marginRight="15dp"
android:textSize="16sp"
android:textColor="#ffcccccc" />
</RelativeLayout>
显示如下
6. Notification显示等级
Android5.0加入了Notification显示等级
- VISIBILITY_PUBLIC,在任何情况都会显示通知
- VISIBILITY_PRIVATE,只有在没有锁屏时才会显示通知
- VISIBILITY_SECRET,在安全锁和没有锁屏的情况下才能够显示通知
notification.visibility = Notification.VISIBILITY_PRIVATE