func application(application: UIApplication,dIDFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool { //1.创建UIUserNotificationSettings,并设置消息的显示类类型 let pushtypes : UIUserNotificationType = [UIUserNotificationType.Badge,UIUserNotificationType.Alert,UIUserNotificationType.sound] let userSetting = UIUserNotificationSettings(forTypes: pushtypes,categorIEs:nil) //2.注册推送 //本地通知只有app位于后台才会显示 application.registerUserNotificationSettings(userSetting) //远程通知 //application.registerForRemoteNotificationTypes([UIRemoteNotificationType.Alert,UIRemoteNotificationType.Badge,UIRemoteNotificationType.sound]) //3.添加本地推送和设置固定时间推送了 self.sendNotification()}
//添加本地推送和设置固定时间推送了 func sendNotification( ) { // //取消所有的本地通知 UIApplication.sharedApplication().cancelAlllocalnotifications() //数字清零 UIApplication.sharedApplication().applicationIconBadgeNumber = 0 let localnotificationAM = UIlocalnotification() //设置为5妙后localnotificationAM.fireDate = NSDate(timeIntervalSinceNow: 5) //早九点提醒上班打卡 localnotificationAM.fireDate = getFireDate(9.0) localnotificationAM.repeatInterval = NSCalendarUnit.Day localnotificationAM.timeZone = NSTimeZone.defaultTimeZone() localnotificationAM.alertbody = "别忘上班打卡啊" localnotificationAM.applicationIconBadgeNumber = 1 localnotificationAM.soundname = UIlocalnotificationDefaultSoundname UIApplication.sharedApplication().schedulelocalnotification(localnotificationAM) let localnotificationPM = UIlocalnotification() //下午6点提醒打卡 localnotificationPM.fireDate = getFireDate(18.5) localnotificationAM.repeatInterval = NSCalendarUnit.Day localnotificationPM.timeZone = NSTimeZone.defaultTimeZone() localnotificationPM.alertbody = "别忘下班打卡和写日志啊" localnotificationPM.applicationIconBadgeNumber = UIApplication.sharedApplication().applicationIconBadgeNumber + 1 localnotificationPM.soundname = UIlocalnotificationDefaultSoundname UIApplication.sharedApplication().schedulelocalnotification(localnotificationPM) print("@@@@@@@@@@@@@@@@@@注册上午和下午的本地通知 每天重复") } //24小时制 func getFireDate( hourOfDay:float)->NSDate{ //本地推送时间 hourOfDay let pushTime: float = hourOfDay*60*60 let date = NSDate() let dateFormatter = NSDateFormatter() //日期格式为“时,分,秒” dateFormatter.dateFormat = "HH,mm,ss" //设备当前的时间(24小时制) let strDate = dateFormatter.stringFromDate(date) //将时、分、秒分割出来,放到一个数组 let dateArr = strDate.componentsSeparatedByString(",") //统一转化成秒为单位 let hour = ((dateArr[0] as Nsstring).floatValue)*60*60 let minute = ((dateArr[1] as Nsstring).floatValue)*60 let second = (dateArr[2] as Nsstring).floatValue var newPushTime = float() if hour > pushTime { newPushTime = 24*60*60-(hour+minute+second)+pushTime } else { newPushTime = pushTime-(hour+minute+second) } return NSDate(timeIntervalSinceNow: NSTimeInterval(newPushTime)) }
//点击通知打开app func application(application: UIApplication,dIDReceivelocalnotification notification: UIlocalnotification) { print("dIDReceivelocalnotification 接收到本地通知") }总结
以上是内存溢出为你收集整理的swift开发笔记25 定时的本地通知全部内容,希望文章能够帮你解决swift开发笔记25 定时的本地通知所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)