iOS8之后 交互性通知

2016-08-24  本文已影响29人  此生浮华祇盼伊亽

1、在AppDelegate中注册交互性通知:

func  application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {

// application.registerUserNotificationSettings(UIUserNotificationSettings(forTypes: UIUserNotificationType.Alert, categories: nil))

        //MARK: - 提醒操作 设置

        let notificationActionOk : UIMutableUserNotificationAction = UIMutableUserNotificationAction()

        notificationActionOk.identifier = "completeRemindRater"

        notificationActionOk.title = "再工作一会儿"

        //是否取消提醒

        notificationActionOk.destructive = false

        //是否需要权限,例如锁屏的时候,执行操作是否需要解锁再执行

        notificationActionOk.authenticationRequired = false

        //启动app还是后台执行

        notificationActionOk.activationMode = UIUserNotificationActivationMode.Background

        let notificationActionRestNow: UIMutableUserNotificationAction = UIMutableUserNotificationAction()

        notificationActionRestNow.identifier = "relaxNow"

        notificationActionRestNow.title = "休息"

        notificationActionRestNow.destructive = false

        notificationActionRestNow.authenticationRequired = false

        notificationActionRestNow.activationMode = UIUserNotificationActivationMode.Background

        //MARK: -Notification Category 设置

        let notificationCompleteCategory: UIMutableUserNotificationCategory = UIMutableUserNotificationCategory()

        //记住这个identifier ,待会用

        notificationCompleteCategory.identifier = "COMPLETE_CATEGORY"

        notificationCompleteCategory.setActions([notificationActionOk,notificationActionRestNow], forContext: .Default)

        notificationCompleteCategory.setActions([notificationActionOk,notificationActionRestNow], forContext: .Minimal)

        

        //请求用户允许通知

        application.registerUserNotificationSettings(UIUserNotificationSettings(forTypes:[.Sound,.Alert,.Badge], categories: NSSet(array: [notificationCompleteCategory]) as? Set<UIUserNotificationCategory>))

        

        return true

    }

2、在需要发送通知的地方实现以下代码

@IBAction func sendNoti(sender: AnyObject) {

        //设置10秒的提醒

//        let completeNotification = setNotification("时间到了,已完成任务",timeToNotification: 10,soundName: "提醒音乐.mp3",category:"")

        let completeNotification = setNotification("时间到了,已完成任务",timeToNotification: 5,soundName: "提醒铃声.mp3",category: "COMPLETE_CATEGORY")

        UIApplication.sharedApplication().scheduleLocalNotification(completeNotification)

    }

    func setNotification(body:String,timeToNotification:Double,soundName:String,category:String) -> UILocalNotification {

        let localNotification:UILocalNotification = UILocalNotification()

        localNotification.alertAction = "滑动查看信息"

        localNotification.alertBody = body

        

        //在mainBundle的短于30秒的播放文件,否则就会是系统默认声音

        localNotification.soundName = soundName

        localNotification.fireDate = NSDate(timeIntervalSinceNow: timeToNotification)

        

        //下面这条category语句是对应下面的“有选项的本地操作”章节

        localNotification.category = category

        

        return localNotification

    }

3、在AppDelegate中实现点击交互按钮的回调,
​completionHandler()回调必须实现,否则会报警告,而且执行不了里面的流程

  func application(application: UIApplication, handleActionWithIdentifier identifier: String?, forLocalNotification notification: UILocalNotification, withResponseInfo responseInfo: [NSObject : AnyObject], completionHandler: () -> Void) {

        print("按下的选项的identifier是:\(identifier)")

        //必须实现的方法,否则会报一个警告

          completionHandler()

    }
上一篇下一篇

猜你喜欢

热点阅读