天天看点

Android学习笔记(32):通知推送Notification

notification用于显示在手机状态栏的通知。一般通过notificationmanager服务来发送通知。

notification.builder类可以让我们更加方便的创建notification对象。

notification.builder常用方法:

setautocancel():设置点击通知后,状态栏自动删除通知

setcontenttext():设置通知内容

setcontenttitle():设置通知标题

setcontentintent():设置点击后要启动的程序组件对应的pendingintent

setdefaults():设置通知led灯、音乐、振动等

setlargeicon():设置大图标

setsmallicon():设置小图标

setticker():设置通知在状态栏的提示文本

发送notification的步骤:

1.调用getsystemservice(notification_service)方法获取notificationmanage服务。

2.用构造器创建一个notification对象。

3.为notification设置各种属性。

4.通过notificationmanager发送notification。

setdefault()方法可以设置的属性有:

default_sound:设置使用默认声音

default_vibrate:设置使用默认振动

default_lights:设置使用默认闪光灯

all:设置以上三种全为默认

当然我们也可以用setsound()、setvibrate()等方法设置自己中意的属性。

最后我们还有一件事情要做就是在androidmanifest.xml文件中增加相应的权限。

继续阅读