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_remote_view_notification);
remoteViews.setTextViewText(R.id.tv_name, "Jack");
remoteViews.setTextViewText(R.id.tv_address, "BeiJing");
notification.contentView = remoteViews;
布局文件layout_remote_view_notification.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
显示等级
notification.visibility = Notification.VISIBILITY_PRIVATE;
VISIBILITY_PUBLIC
,在任何情况都会显示通知VISIBILITY_PRIVATE
,只有在没有锁屏时才会显示通知VISIBILITY_SECRET
,在安全锁和没有锁屏的情况下才能够显示通知