iOS8之后 交互性通知

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()

    }
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 版权声明 本文翻译自:raywenderlich.com 原文作者: Jack Wu 译者: JMStack 转载...
    jmstack阅读 7,301评论 6 30
  • 前言 本文是一篇转载文章,在这一篇实用的文章里,你可以按照上面的步骤实现不借助第三方和服务器端,自己给自己的设备发...
    進无尽阅读 1,700评论 6 6
  • 应用程序必须进行适当配置,才可以接受本地或远程通知。配置过程在iOS和OS X略有不同,但基本原理是相同的。在启动...
    shenzhenboy阅读 1,421评论 1 2
  • 介绍一下iOS10的通知新功能,用户体验的提升和开发者能够发挥的地方非常多,使得iOS更具有竞争力。 1.iOS ...
    F麦子阅读 3,900评论 3 4
  • 昨天晚上难得的做了一个梦,不太好!而且还觉得很真实,结果醒来发现是个梦中梦。 梦里我躺在床上,和女儿在一起,但那不...
    44e9933106c7阅读 357评论 0 0