本地通知属性
本地通知是一个UILocalNotification对象,有如下属性:
(1) fireDate:设置本地通知将在什么时间触发。
(2) repeatInterval:设置本地通知重复发送的时间间隔。
(3) alertBody:设置本地通知的消息体。
(4) alertAction:设置当设备处理锁屏状态时,显示通知在警告框下方的title。
(5) hasAction:设置是否显示Action。
(6) alertLaunchImage:当用户通过该通知启动对应的应用时,该属性设置为加载图片。
(7) applicationIconBadgeNumber:设置显示在应用程序上红色徽标中的数字。
(8) soundName:设置通知的声音。
(9) userInfo:设置该通知携带的附加信息。
创建了UILocalNotification对象之后,接下来就可以通过UIApplication的如下两个方法发送通知了:
(1) -scheduleLocalNotification:该方法指定调度通知。
(2) -presentLocalNotification:该方法指定立即发送通知。
每个应用程序最多只能发送最近的64个本地通知,超过该限制的通知将会操作系统自动放弃。重复出现的通知会被认为是一个通知。
除此之外,如果系统发出通知时,应用程序处于前台运行,系统将会触发应用程序委托类的application:didReceiveLocalNotification:方法。
发送本地通知步骤
在iOS应用中,发送本地通知步骤如下:
(1) 创建UILocalNotification对象。
(2) 设置UILocalNotification的属性。
(3) 调用UIApplication的方法发送或调用通知。
(4) 如果希望应用程序在前台运行时可以对通知进行相应的处理,则需要重写应用程序的委托类application:didReceiveLocalNotification:方法。
(5) 当应用需要取消本地通知时,可调用UIApplication的cancelLocalNotification:方法取消指定通知,或调用cancelLocalNotifications方法取消所有通知。