本地通知由本应用负责调用,只能从当前设备上得iOS发出。
本地通知适用于基于时间的程序,包括简单的日历程序或者to-do列表类型的应用程序。
本地通知是一个UILocalNotification,它有如下属性:
var fireDate: NSDate?:指定通知在什么时间出发var repeatInterval: NSCalendarUnit:设置本地通知重复发送的时间间隔var alertBody: String?:设置本地通知的消息体var hasAction: Bool:设置是否显示Actionvar alertAction: String?:设置当设备处于锁屏状态时,显示通知的警告框下方的titlevar alertLaunchImage: String?:当用户通过该通知启动对应的应用时,该属性设置为加载图片var applicationIconBadgeNumber: Int:设置显示在应用程序上红色徽标中的数字var soundName: String?:设置通知的声音var userInfo: [NSObject : AnyObject]?:设置该通知携带的附加信息
创建了UILocalNotification对象后,接下来可以通过UIApplication的如下方法发送通知:
func presentLocalNotificationNow(notification: UILocalNotification):该方法指定调度通知。通知将会于fireDate指定的事件触发,而且会按repeatInterval指定的时间间隔重复触发func presentLocalNotificationNow(notification: UILocalNotification):该方法指定立即发送通知。该方法会忽略UILocalNotification的fireDate属性。
每个应用程序最多只能发送64个本地通知。
如果系统发出通知时,应用程序处于前台运行,系统将会触发应用程序委托类的方法:
optional public func application(application: UIApplication, didReceiveLocalNotification notification: UILocalNotification)
应用程序需要取消本地通知时,可调用UIApplication的func cancelLocalNotification(notification: UILocalNotification)方法取消指定通知,或调用func cancelAllLocalNotifications()方法取消所有通知。
注意:
首先在didFinishLaunchingWithOptions方法内添加代码,IOS8推送消息首先要获得用户的同意,在初次安装App时会提示用户是否允许程序推送消息,此方法是App第一次运行的时候被执行一次,每次从后台激活时不执行该方法.
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
if (UIDevice.currentDevice().systemVersion as NSString).floatValue >= 8 {
application.registerUserNotificationSettings(UIUserNotificationSettings(forTypes: [UIUserNotificationType.Sound,UIUserNotificationType.Badge,UIUserNotificationType.Alert], categories: nil))
}
return true
}
全部代码如下:
AppDelegate.swift代码
import UIKit
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
if (UIDevice.currentDevice().systemVersion as NSString).floatValue >= 8 {
application.registerUserNotificationSettings(UIUserNotificationSettings(forTypes: [UIUserNotificationType.Sound,UIUserNotificationType.Badge,UIUserNotificationType.Alert], categories: nil))
}
return true
}
func application(application: UIApplication, didReceiveLocalNotification notification: UILocalNotification) {
application.applicationIconBadgeNumber=0
print("ok")
}
func applicationWillResignActive(application: UIApplication) {
}
func applicationDidEnterBackground(application: UIApplication) {
}
func applicationWillEnterForeground(application: UIApplication) {
}
func applicationDidBecomeActive(application: UIApplication) {
}
func applicationWillTerminate(application: UIApplication) {
}
}
ViewController.swift代码
import UIKit
class ViewController: UIViewController {
var app:UIApplication=UIApplication.sharedApplication()
override func viewDidLoad() {
super.viewDidLoad()
let sw:UISwitch=UISwitch(frame: CGRectMake(100,100,100,100))
self.view.addSubview(sw)
sw.addTarget(self, action: "valueChanged:", forControlEvents: .ValueChanged)
}
func valueChanged(sender:AnyObject){
print("ok")
let sw=sender as! UISwitch
if sw.on {
let notificaiton=UILocalNotification()
notificaiton.fireDate=NSDate(timeIntervalSinceNow: 10)
notificaiton.timeZone=NSTimeZone.defaultTimeZone()
notificaiton.alertAction="打开"
notificaiton.hasAction=true
notificaiton.alertLaunchImage="icon_os7"
notificaiton.alertBody="轮到你去打扫卫生了"
notificaiton.applicationIconBadgeNumber=3
notificaiton.userInfo=["ricky":"key"]
app.scheduleLocalNotification(notificaiton)
print("notification")
}
else{
let notifications:[UILocalNotification]=app.scheduledLocalNotifications!
for notification in notifications{
let dictOptional:[NSObject:AnyObject]?=notification.userInfo
if let dict=dictOptional {
let value=dict["key"] as! String
print(value)
if value=="ricky"{
[app .cancelLocalNotification(notification)]
}
}
}
}
}
}
效果图:
550

被折叠的 条评论
为什么被折叠?



