本地通知 UILocalNotification

发送一个本地通知

// 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)
    }

取消本地通知

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
                }
            }
        }
    }

在接到通知时调用的方法

//在接到通知时,appdelegate会调用该方法
func application(application: UIApplication, didReceiveLocalNotification notification: UILocalNotification) {
       
    }


你可能感兴趣的:(本地通知 UILocalNotification)