swift开发笔记25 定时的本地通知

前端之家收集整理的这篇文章主要介绍了swift开发笔记25 定时的本地通知前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
 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 接收到本地通知") 
   }
原文链接:https://www.f2er.com/swift/324209.html

猜你在找的Swift相关文章