因为项目需要,所以研究了一天的本地推送,现在,把它写成博客,记录下自己的想法咯。
仔细想想,本地推送还是不难的,主要是网上资料大把,比较花时间的是项目逻辑(在哪里添加,哪里取消,怎么知道是否添加等等)。
现在要讲的是怎么添加本地推送,怎么取消本地推送,和怎么设置固定时间推送(在本文中是每天晚上九点钟)
要在应用中添加推送通知,首先要在应用的AppDelegate的
application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?)方法添加注册通知:
iOS8或iOS之后版本注册本地通知方法:
let pushtypes : UIUserNotificationType = [UIUserNotificationType.Badge,UIUserNotificationType.Alert,UIUserNotificationType.Sound]
let mySetting : UIUserNotificationSettings = UIUserNotificationSettings(forTypes: pushtypes, categories: nil)
UIApplication.sharedApplication().registerUserNotificationSettings(mySetting)
iOS8.0以下注册通知方法:
UIApplication.sharedApplication().registerForRemoteNotificationTypes([UIRemoteNotificationType.Alert,UIRemoteNotificationType.Badge,UIRemoteNotificationType.Sound])
注册完通知,接下来就是怎么添加本地推送和设置固定时间推送了:
//MARK:添加本地推送
? func addLocalNotification() {
? ? ? //本地推送时间(暂时为晚上九点)
? ? ? let pushTime: Float = 21*60*60
?
? ? ? //初始化本地通知
? ? ? let lNotification = UILocalNotification()
? ? ? let date = NSDate()
? ? ? let dateFormatter = NSDateFormatter()
? ? ? //日期格式为“时,分,秒”
? ? ? dateFormatter.dateFormat = "HH,mm,ss"
? ? ? //设备当前的时间(24小时制)
? ? ? let strDate = dateFormatter.stringFromDate(date)
? ? ? //将时、分、秒分割出来,放到一个数组
? ? ? let dateArr = strDate.componentsSeparatedByString(",")
? ? ? //统一转化成秒为单位
? ? ? let hour = ((dateArr[0] as NSString).floatValue)*60*60
? ? ? let minute = ((dateArr[1] as NSString).floatValue)*60
? ? ? let second = (dateArr[2] as NSString).floatValue
? ? ? var newPushTime = Float()
? ? ? if hour > pushTime {
? ? ? ? ? newPushTime = 24*60*60-(hour+minute+second)+pushTime
? ? ? } else {
? ? ? ? ? newPushTime = pushTime-(hour+minute+second)
? ? ? }
? ? ? let fireDate = NSDate(timeIntervalSinceNow: NSTimeInterval(newPushTime))
? ? ?
? ? ? //设置推送时间
? ? ? lNotification.fireDate = fireDate
? ? ? //设置推送时区
? ? ? lNotification.timeZone = NSTimeZone.defaultTimeZone()
? ? ? //应用的红色数字
? ? ? lNotification.applicationIconBadgeNumber = 1
? ? ? //推送声音
? ? ? lNotification.soundName = UILocalNotificationDefaultSoundName
? ? ? //推送内容
? ? ? lNotification.alertBody = pushBody
? ? ? //重复间隔
? ? ? lNotification.repeatInterval = NSCalendarUnit.Day
? ? ? //添加一个字典类型的info,主要就是为了记录通知的标识,这里用存了一个key名
? ? ? let info = NSDictionary(object: cartLocalNotifi, forKey: key_cartLocalNotifi)
? ? ? lNotification.userInfo = info as [NSObject : AnyObject]
? ? ? //应用添加该本地通知
? ? ? UIApplication.sharedApplication().scheduleLocalNotification(lNotification)
? }? ?
添加了这个方法后,就可以在每晚的九点钟准时推送你的应用了。
接下来要说的就是怎么检测你的应用是否已经添加了你要添加的本地推送(避免重复添加)和移除你已添加的本地推送(如果每分钟推送一次,你不觉得很烦嘛??)
检测是否已添加本地推送的原理是:首先获取设备的所有本地推送(一个数组),然后你要遍历这个数组,根据你在创建本地推送的时候的key名识别你是否已添加。
废话不多说,代码上:
//获取本地推送数组
? ? ? ? ? ? let localArray = UIApplication.sharedApplication().scheduledLocalNotifications
? ? ? ? ? ? if localArray != nil && localArray?.count > 0 {
? ? ? ? ? ? ? ? for localNotif in localArray! {
? ? ? ? ? ? ? ? ? ? let dict = localNotif.userInfo
? ? ? ? ? ? ? ? ? ? if dict != nil {
? ? ? ? ? ? ? ? ? ? ? ? let notifiName = dict![key_cartLocalNotifi] as? String
? ? ? ? ? ? ? ? ? ? ? ? if notifiName != nil && notifiName == cartLocalNotifi {
? ? ? ? ? ? ? ? ? ? ? ? ? ? //取消推送
? ? ? ? ? ? ? ? ? ? ? ? ? ? UIApplication.sharedApplication().cancelLocalNotification(localNotif)
? ? ? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? }
? ? ? ? ? ? }
cartLocalNotifi和
key_cartLocalNotifi 为了方便,我写成了全局变量,其实也就是string字段,前者是通知的名字,后者是通知名字的key。啊!!终于写完了。。。希望读