设置每天午夜的本地通知 [英] Set local notification for everyday at midnight

查看:53
本文介绍了设置每天午夜的本地通知的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个每天午夜显示的简单通知.这是我使用的代码:

I'm trying to create a simple notification that will appear every day at midnight. This is the code I use:

    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
    // Override point for customization after application launch.
    application.registerUserNotificationSettings(UIUserNotificationSettings(forTypes: UIUserNotificationType.Sound | UIUserNotificationType.Alert | UIUserNotificationType.Badge, categories: nil))

    var thisDate = NSDateComponents()
    thisDate.hour = 0
    thisDate.minute = 0
    thisDate.second = 0

    var calender = NSCalendar()

    var localNotification:UILocalNotification = UILocalNotification()
    localNotification.timeZone = NSTimeZone.localTimeZone()
    localNotification.fireDate = calender.dateFromComponents(thisDate)
    localNotification.repeatInterval = .CalendarUnitDay
    localNotification.alertAction = "Open App"
    localNotification.alertBody = "Here is the seven o'clock notification"
    localNotification.soundName = UILocalNotificationDefaultSoundName
    localNotification.applicationIconBadgeNumber = UIApplication.sharedApplication().applicationIconBadgeNumber + 1
    UIApplication.sharedApplication().scheduleLocalNotification(localNotification)

    return true
}    

我不知道为什么它不起作用,知道吗?

I cant figure out why it's not working, any idea?

推荐答案

你的代码有很多问题,所以我通过代码注释解释如下:

There are many problems at your code so I will explain it with comments through the code as follow:

import UIKit
class ViewController: UIViewController {
    @IBOutlet weak var midnightSwitch: UISwitch!
    // you should declare your vars here
    let localNotificationDailyAt12am = UILocalNotification()
    // you should declare your funcions here
    // you have to register the user notifications settings options (badge, alert and sound if desired)
    func setUIUserNotificationOptions() {
        UIApplication.sharedApplication().registerUserNotificationSettings(UIUserNotificationSettings(forTypes: UIUserNotificationType.Badge | UIUserNotificationType.Sound | UIUserNotificationType.Alert, categories: nil))
    }
    // now lets setup your notification
    func notificationsOptions12am() {
        // defines localtime
        localNotificationDailyAt12am.timeZone = NSTimeZone.localTimeZone()
        // confirms the alert dialog box action
        localNotificationDailyAt12am.hasAction = true
        // defines the notification alert body text
        localNotificationDailyAt12am.alertBody = "The midnight notification message"
        // defines the daily time interval
        localNotificationDailyAt12am.repeatCalendar = NSCalendar(calendarIdentifier: NSCalendarIdentifierGregorian)
        localNotificationDailyAt12am.repeatInterval = .CalendarUnitDay
        // defines the notification alert button text
        localNotificationDailyAt12am.alertAction = "Open App"
        // defines the default sound for your notification
        localNotificationDailyAt12am.soundName = UILocalNotificationDefaultSoundName
        // increments the badge counter
        localNotificationDailyAt12am.applicationIconBadgeNumber = UIApplication.sharedApplication().applicationIconBadgeNumber + 1
        // defines your firedate tomorrow at 12am ( note: extension NSDate tomorrowAt12am at the bottom )
        localNotificationDailyAt12am.fireDate = NSDate().tomorrowAt12am
        // lets set it up
        UIApplication.sharedApplication().scheduleLocalNotification(localNotificationDailyAt12am)
    }
    override func viewDidLoad() {
        super.viewDidLoad()
        setUIUserNotificationOptions()
        notificationsOptions12am()

    }
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    @IBAction func switched(sender: AnyObject) {
        localNotificationDailyAt12am.fireDate = midnightSwitch.on ?
            NSDate().tomorrowAt12am :                                // tomorrow at 12am
                NSDate().dateByAddingTimeInterval(3155673600)        // will never fire (100 years from now)
    }
}
extension NSDate {
    var day:   Int { return NSCalendar.currentCalendar().components(.Day,   fromDate: self).day   }
    var month: Int { return NSCalendar.currentCalendar().components(.Month, fromDate: self).month }
    var year:  Int { return NSCalendar.currentCalendar().components(.Year,  fromDate: self).year  }
    var tomorrowAt12am: NSDate {
        return  NSCalendar.currentCalendar().dateWithEra(1, year: year, month: month, day: day+1, hour: 0, minute: 0, second: 0, nanosecond: 0)!
    }
}

这篇关于设置每天午夜的本地通知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆