iOS本地通知详解

来源:互联网 发布:大数据时代.pdf 编辑:程序博客网 时间:2024/06/05 23:19

       iOS开发中,实现类似于闹钟的事件提醒功能,使用本地通知无疑是比较合适的、且简单易行。之前在实现这个功能时,每次都是以完成功能和任务为出发点,匆匆搞定之后,就再也不管了。今天对UILocalNotification做一个较全面的记录。

       1、注册本地通知

            即通常看到提示用户是否允许接收通知,这步很关键,若不注册即使通知加入系统,时间到了也不会发送。

           

      func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {        if #available(iOS 8, *) {            self.registerNotificationForiOS8()        } else {            self.registerNotification()        }                return true      }
    

             iOS8版本之后,通知注册与之前不同,需要分别处理

      @available(iOS 8, *)      func registerNotificationForiOS8() {        let types: UIUserNotificationType = StringUtils.userNotificationTypeCombine()         //由于swift的枚举类型比较恶心,不能用|去连接,因此没有办法搞了个OC版本封装,即 UIUserNotificationTypeBadge | UIUserNotificationTypeSound | UIUserNotificationTypeAlert        //Actions        let acceptAction: UIMutableUserNotificationAction = UIMutableUserNotificationAction()        acceptAction.identifier = "ACCEPT_IDENTIFIER"        acceptAction.title = "确定"        acceptAction.activationMode = UIUserNotificationActivationMode.Background        acceptAction.destructive = false        acceptAction.authenticationRequired = false                let cancelAction: UIMutableUserNotificationAction = UIMutableUserNotificationAction()        cancelAction.identifier = "CANCEL_IDENTIFIER"        cancelAction.title = "取消"        cancelAction.activationMode = UIUserNotificationActivationMode.Background        cancelAction.destructive = false        cancelAction.authenticationRequired = false                //Categories        let actionCategory: UIMutableUserNotificationCategory = UIMutableUserNotificationCategory()        actionCategory.identifier = "ACTION_CATEGORY"        actionCategory.setActions([acceptAction, cancelAction], forContext: UIUserNotificationActionContext.Default)        actionCategory.setActions([acceptAction, cancelAction], forContext: UIUserNotificationActionContext.Minimal)                let categories: NSSet = NSSet(object: actionCategory)        let mySettins: UIUserNotificationSettings = UIUserNotificationSettings(forTypes: types, categories: categories as? Set<UIUserNotificationCategory>)                UIApplication.sharedApplication().registerUserNotificationSettings(mySettins)        UIApplication.sharedApplication().registerForRemoteNotifications()    }        func registerNotification() {        UIApplication.sharedApplication().registerForRemoteNotificationTypes(StringUtils.userRemoteNotificationTypeCombine())    }

            

       2、添加本地通知至系统中

            

        let localNotification: UILocalNotification = UILocalNotification()   //初始化本地通知对象        localNotification.fireDate = date                                    //指定本地通知触发的时间        localNotification.timeZone = NSTimeZone.defaultTimeZone()<span style="white-space:pre"></span>     //指定为本地时区         localNotification.repeatInterval = NSCalendarUnit.Day                //通知循环方式,当为0时表示不循环        localNotification.alertBody = name! + "   时间到了"                   //应用不在前台运行时,通知显示的内容        localNotification.alertAction = alertAction                          //锁屏时显示的滑动信息        localNotification.soundName = UILocalNotificationDefaultSoundName    //设置通知到达时,播放的声音        localNotification.applicationIconBadgeNumber++                       //应用程序右上角角标数                                var userinfo: [NSObject : AnyObject] = Dictionary()                  //与通知有关的其它特定应用数据,未设置时可以省略        userinfo["planItem"] = "testItem"        userinfo["id"] = "testId"        localNotification.userInfo = userinfo                                UIApplication.sharedApplication().scheduleLocalNotification(localNotification)   //添加通知至系统

                 alertBody在通知到达时,显示部分为图中红色线框部分:

                            

                                                                                          alertBody说明

                 alertAction在锁屏通知到达时,显示为图中蓝色线框部分:

                 

                                                                                              alertAction说明

                         

       3、接收本地通知的处理

       3.1、app后台运行,通知到达处理

               

        //检测是否从消息通知启动        if launchOptions != nil {            if let remoteNotification: Dictionary = launchOptions?[UIApplicationLaunchOptionsRemoteNotificationKey] as? [NSObject : AnyObject] {//远程通知                self.processRemoteNotification(remoteNotification, launched: true)            }                        if let localNotification: UILocalNotification = launchOptions?[UIApplicationLaunchOptionsLocalNotificationKey] as? UILocalNotification {//本地通知                self.processLocalNotification(localNotification, launched: true)            }        }

        3.2、app前台运行,通知到达处理

               

        func application(application: UIApplication, didReceiveLocalNotification notification: UILocalNotification) {           self.processLocalNotification(notification, launched: false)        }
         processLocalNotification方法因业务不同而不同,这里就不贴代码了


         至此,本地消息通知,从注册->添加->处理全部介绍完成。

0 0
原创粉丝点击