歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> 用Swift在應用中在指定時間添加本地推送

用Swift在應用中在指定時間添加本地推送

日期:2017/3/1 9:20:25   编辑:Linux編程

因為項目需要,所以研究了一天的本地推送,現在,把它寫成博客,記錄下自己的想法咯。

仔細想想,本地推送還是不難的,主要是網上資料大把,比較花時間的是項目邏輯(在哪裡添加,哪裡取消,怎麼知道是否添加等等)。

現在要講的是怎麼添加本地推送,怎麼取消本地推送,和怎麼設置固定時間推送(在本文中是每天晚上九點鐘)

要在應用中添加推送通知,首先要在應用的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。啊!!終於寫完了。。。希望讀完我的博客的你能學點什麼。。。。

Swift 正式開源,同時開源 Swfit 核心庫和包管理器 http://www.linuxidc.com/Linux/2015-12/125847.htm

Apple Swift學習教程 http://www.linuxidc.com/Linux/2014-09/106420.htm

使用 Swift 構建一個 iOS 的郵件應用 http://www.linuxidc.com/Linux/2014-08/105542.htm

Swift 2.0開源化 http://www.linuxidc.com/Linux/2015-06/118594.htm

Linux下搭建Swift語言開發學習環境 http://www.linuxidc.com/Linux/2015-12/125983.htm

Swift 的詳細介紹:請點這裡

Copyright © Linux教程網 All Rights Reserved