本地通知 UILocalNotification

發送一個本地通知app

// MARK:本地推送
    func sendNotification(time: Double, title: String, remindId: NSNumber) {
     
        var notification = UILocalNotification()
        notification.fireDate = NSDate(timeIntervalSinceNow: time)
        notification.timeZone = NSTimeZone.systemTimeZone()
        notification.soundName = UILocalNotificationDefaultSoundName
        notification.alertBody = title
        /*  給通知加上標識,
        *  1.方便在接到對應通知時作出相應操做
        *  2.方便在想要取消該通知時,找到該通知
        */
        var infoDictionary = NSMutableDictionary(objects: [notifiName, remindId], forKeys: ["localKey", "remindId"])
        notification.userInfo = infoDictionary

        UIApplication.sharedApplication().scheduleLocalNotification(notification)
    }

取消本地通知code

func deleteLocalNotification(NSNumber: id) {
      
        var array = UIApplication.sharedApplication().scheduledLocalNotifications as NSArray
        if array.count > 0 {
            
            for var i = 0; i < array.count; i++ {
               
                var myLocalNot = array[i] as UILocalNotification        //獲取通知
                var info = myLocalNot.userInfo! as NSDictionary         //獲取通知的userInfo
                var remindId = info.objectForKey("remindId") as NSNumber//獲取通知的標識
                
                if id == remindId {
                
                    UIApplication.sharedApplication().cancelLocalNotification(myLocalNot)
                    break
                }
            }
        }
    }

在接到通知時調用的方法rem

//在接到通知時,appdelegate會調用該方法
func application(application: UIApplication, didReceiveLocalNotification notification: UILocalNotification) {
       
    }
相關文章
相關標籤/搜索