(点击
上方公众号
,可快速关注)
来源:伯乐在线专栏作者 - 踏歌行
链接:http://android.jobbole.com/85045/
点击 → 了解如何加入专栏作者
接上文
case
R
.
id
.
btn_send_flag_no_clear_notification
:
//发送 ID = 1, flag = FLAG_NO_CLEAR 的 Notification
//下面两个 Notification 的 ID 都为 1,会发现 ID 相等的 Notification 会被最新的替换掉
sendFlagNoClearNotification
();
break
;
case
R
.
id
.
btn_send_flag_auto_cancecl_notification
:
sendFlagOngoingEventNotification
();
break
;
case
R
.
id
.
btn_send_flag_ongoing_event_notification
:
sendFlagAutoCancelNotification
();
break
;
}
}
/**
* 发送最简单的通知,该通知的ID = 1
*/
private
void
sendNotification
()
{
//这里使用 NotificationCompat 而不是 Notification ,因为 Notification 需要 API 16 才能使用
//NotificationCompat 存在于 V4 Support Library
NotificationCompat
.
Builder
builder
=
new
NotificationCompat
.
Builder
(
this
)
.
setSmallIcon
(
R
.
mipmap
.
ic_launcher
)
.
setContentTitle
(
"Send Notification"
)
.
setContentText
(
"Hi,My id is 1"
);
mNotificationManager
.
notify
(
DEFAULT_NOTIFICATION_ID
,
builder
.
build
());
}
/**
* 使用notify(String tag, int id, Notification notification)方法发送通知
* 移除对应通知需使用 cancel(String tag, int id)
*/
private
void
sendNotificationWithTag
()
{
NotificationCompat
.
Builder
builder
=
new
NotificationCompat
.
Builder
(
this
)
.
setSmallIcon
(
R
.
mipmap
.
ic_launcher
)
.
setContentTitle
(
"Send Notification With Tag"
)
.
setContentText
(
"Hi,My id is 1,tag is "
+
NOTIFICATION_TAG
);
mNotificationManager
.
notify
(
NOTIFICATION_TAG
,
DEFAULT_NOTIFICATION_ID
,
builder
.
build
());
}
/**
* 循环发送十个通知
*/
private
void
sendTenNotifications
()
{
for
(
int
i
=
0
;
i
10
;
i
++
)
{
NotificationCompat
.
Builder
builder
=
new
NotificationCompat
.
Builder
(
this
)
.
setSmallIcon
(
R
.
mipmap
.
ic_launcher
)
.
setContentTitle
(
"Send Notification Batch"
)
.
setContentText
(
"Hi,My id is "
+
i
);
mNotificationManager
.
notify
(
i
,
builder
.
build
());
}
}
/**
* 设置FLAG_NO_CLEAR
* 该 flag 表示该通知不能被状态栏的清除按钮给清除掉,也不能被手动清除,但能通过 cancel() 方法清除
* Notification.flags属性可以通过 |= 运算叠加效果
*/
private
void
sendFlagNoClearNotification
()
{
NotificationCompat
.
Builder
builder
=
new
NotificationCompat
.
Builder
(
this
)
.
setSmallIcon
(
R
.
mipmap
.
ic_launcher
)
.
setContentTitle
(
"Send Notification Use FLAG_NO_CLEAR"
)
.
setContentText
(
"Hi,My id is 1,i can't be clear."
);
Notification
notification
=
builder
.
build
();
//设置 Notification 的 flags = FLAG_NO_CLEAR
//FLAG_NO_CLEAR 表示该通知不能被状态栏的清除按钮给清除掉,也不能被手动清除,但能通过 cancel() 方法清除
//flags 可以通过 |= 运算叠加效果
notification
.
flags
|=
Notification
.
FLAG_NO_CLEAR
;
mNotificationManager
.
notify
(
DEFAULT_NOTIFICATION_ID
,
notification
);
}
/**
* 设置FLAG_AUTO_CANCEL
* 该 flag 表示用户单击通知后自动消失
*/
private
void
sendFlagAutoCancelNotification
()
{
//设置一个Intent,不然点击通知不会自动消失
Intent
resultIntent
=
new
Intent
(
this
,
MainActivity
.
class
);
PendingIntent
resultPendingIntent
=
PendingIntent
.
getActivity
(
this
,
0
,
resultIntent
,
PendingIntent
.
FLAG_UPDATE_CURRENT
);
NotificationCompat
.
Builder
builder
=
new
NotificationCompat
.
Builder
(
this
)
.
setSmallIcon
(
R
.
mipmap
.
ic_launcher
)
.
setContentTitle
(
"Send Notification Use FLAG_AUTO_CLEAR"
)
.
setContentText
(
"Hi,My id is 1,i can be clear."
)
.
setContentIntent
(
resultPendingIntent
);
Notification
notification
=
builder
.
build
();
//设置 Notification 的 flags = FLAG_NO_CLEAR
//FLAG_AUTO_CANCEL 表示该通知能被状态栏的清除按钮给清除掉
//等价于 builder.setAutoCancel(true);
notification
.
flags
|=
Notification
.
FLAG_AUTO_CANCEL
;
mNotificationManager
.
notify
(
DEFAULT_NOTIFICATION_ID
,
notification
);
}
/**
* 设置FLAG_ONGOING_EVENT
* 该 flag 表示发起正在运行事件(活动中)
*/
private
void
sendFlagOngoingEventNotification
()
{
NotificationCompat
.
Builder
builder
=
new
NotificationCompat
.
Builder
(
this
)
.
setSmallIcon
(
R
.
mipmap
.
ic_launcher
)
.
setContentTitle
(
"Send Notification Use FLAG_ONGOING_EVENT"
)
.
setContentText
(
"Hi,My id is 1,i can't be clear."
);
Notification
notification
=
builder
.
build
();
//设置 Notification 的 flags = FLAG_NO_CLEAR
//FLAG_ONGOING_EVENT 表示该通知通知放置在正在运行,不能被手动清除,但能通过 cancel() 方法清除
//等价于 builder.setOngoing(true);
notification
.
flags
|=
Notification
.
FLAG_ONGOING_EVENT
;
mNotificationManager
.
notify
(
DEFAULT_NOTIFICATION_ID
,
notification
);
}
}
设置 Notification 的通知效果
前面讲了 Notification 的创建、更新和取消,以及给 Notification 设置 Action 等基本操作。那么,我怎么给 Notification 设置诸如震动、铃声、呼吸灯等效果呢?别急,接下来马上就会告诉你怎么给 Notification 添加效果。
Notification 有震动、响铃、呼吸灯三种响铃效果,可以通过 setDefaults(int defualts) 方法来设置。 Default 属性有以下四种,一旦设置了 Default 效果,自定义的效果就会失效。楼主在这里踩了坑,愣是调了半天没找到为什么自定义效果会消失,忘大家慎之。
//设置系统默认提醒效果,一旦设置默认提醒效果,则自定义的提醒效果会全部失效。具体可看源码
//添加默认震动效果,需要申请震动权限
//
Notification
.
DEFAULT_VIBRATE
//添加系统默认声音效果,设置此值后,调用setSound()设置自定义声音无效
Notification
.
DEFAULT_SOUND
//添加默认呼吸灯效果,使用时须与 Notification.FLAG_SHOW_LIGHTS 结合使用,否则无效
Notification
.
DEFAULT_LIGHTS
//添加上述三种默认提醒效果
Notification
.
DEFAULT_ALL
除了以上几种设置 Notification 默认通知效果,还可以通过以下几种 FLAG 设置通知效果。
//提醒效果常用 Flag
//三色灯提醒,在使用三色灯提醒时候必须加该标志符
Notification
.
FLAG_SHOW_LIGHTS
//发起正在运行事件(活动中)
Notification
.
FLAG_ONGOING_EVENT
//让声音、振动无限循环,直到用户响应 (取消或者打开)
Notification
.
FLAG_INSISTENT
//发起Notification后,铃声和震动均只执行一次
Notification
.
FLAG_ONLY_ALERT_ONCE
//用户单击通知后自动消失
Notification
.
FLAG_AUTO_CANCEL
//只有调用NotificationManager.cancel()时才会清除
Notification
.
FLAG_NO_CLEAR
//表示正在运行的服务
Notification
.
FLAG_FOREGROUND_SERVICE
Notification 通知效果的设置方式及注意事项全部在代码中,核心代码如下:
/**
* 最普通的通知效果
*/
private
void
showNotifyOnlyText
()
{
NotificationCompat
.
Builder
builder
=
new
NotificationCompat
.
Builder
(
this
)
.
setSmallIcon
(
R
.
mipmap
.
ic_launcher
)
.
setLargeIcon
(
mLargeIcon
)
.
setContentTitle
(
"我是只有文字效果的通知"
)
.
setContentText
(
"我没有铃声、震动、呼吸灯,但我就是一个通知"
);
mManager
.
notify
(
1
,
builder
.
build
());
}
/**
* 展示有自定义铃声效果的通知
* 补充:使用系统自带的铃声效果:Uri.withAppendedPath(Audio.Media.INTERNAL_CONTENT_URI, "6");
*/
private
void
showNotifyWithRing
()
{
NotificationCompat
.
Builder
builder
=
new
NotificationCompat
.
Builder
(
this
)
.
setSmallIcon
(
R
.
mipmap
.
ic_launcher
)
.
setContentTitle
(
"我是伴有铃声效果的通知"
)
.
setContentText
(
"美妙么?安静听~"
)
//调用系统默认响铃,设置此属性后setSound()会无效
//.setDefaults(Notification.DEFAULT_SOUND)
//调用系统多媒体裤内的铃声
//.setSound(Uri.withAppendedPath(MediaStore.Audio.Media.INTERNAL_CONTENT_URI,"2"));
//调用自己提供的铃声,位于 /res/values/raw 目录下
.
setSound
(
Uri
.
parse
(
"android.resource://com.littlejie.notification/"
+
R
.
raw
.
sound
));
//另一种设置铃声的方法
//Notification notify = builder.build();
//调用系统默认铃声
//notify.defaults = Notification.DEFAULT_SOUND;
//调用自己提供的铃声
//notify.sound = Uri.parse("android.resource://com.littlejie.notification/"+R.raw.sound);
//调用系统自带的铃声
//notify.sound = Uri.withAppendedPath(MediaStore.Audio.Media.INTERNAL_CONTENT_URI,"2");
//mManager.notify(2,notify);
mManager
.
notify
(
2
,
builder
.
build
());
}
/**
* 展示有震动效果的通知,需要在AndroidManifest.xml中申请震动权限
*
* 补充:测试震动的时候,手机的模式一定要调成铃声+震动模式,否则你是感受不到震动的
*/
private
void
showNotifyWithVibrate
()
{
//震动也有两种设置方法,与设置铃声一样,在此不再赘述
long
[]
vibrate
=
new
long
[]{
0
,
500
,
1000
,
1500
};
NotificationCompat
.
Builder
builder
=
new
NotificationCompat
.
Builder
(
this
)
.
setSmallIcon
(
R
.
mipmap
.
ic_launcher
)
.
setContentTitle
(
"我是伴有震动效果的通知"
)
.
setContentText
(
"颤抖吧,凡人~"
)
//使用系统默认的震动参数,会与自定义的冲突
//.setDefaults(Notification.DEFAULT_VIBRATE)
//自定义震动效果
.
setVibrate
(
vibrate
);
//另一种设置震动的方法
//Notification notify = builder.build();
//调用系统默认震动
//notify.defaults = Notification.DEFAULT_VIBRATE;
//调用自己设置的震动
//notify.vibrate = vibrate;
//mManager.notify(3,notify);
mManager
.
notify
(
3
,
builder
.
build
());
}
/**
* 显示带有呼吸灯效果的通知,但是不知道为什么,自己这里测试没成功
*/
private
void
showNotifyWithLights
()
{
final
NotificationCompat
.
Builder
builder
=
new
NotificationCompat
.
Builder
(
this
)
.
setSmallIcon
(
R
.
mipmap
.
ic_launcher
)
.
setContentTitle
(
"我是带有呼吸灯效果的通知"
)
.
setContentText
(
"一闪一闪亮晶晶~"
)
//ledARGB 表示灯光颜色、 ledOnMS 亮持续时间、ledOffMS 暗的时间
.
setLights
(
0xFF0000
,
3000
,
3000
);
Notification
notify
=
builder
.
build
();
//只有在设置了标志符Flags为Notification.FLAG_SHOW_LIGHTS的时候,才支持呼吸灯提醒。
notify
.
flags
=
Notification
.
FLAG_SHOW_LIGHTS
;
//设置lights参数的另一种方式