Recurring Local Notification in iOS

好久不见. 提交于 2019-12-12 05:20:02

问题


I just want to ensure I understand how local notifications work. Is it true that once I run the below code one time, I'll have weekly notifications?

//Goes in AppDelegate.swift
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
    // Override point for customization after application launch.

    //Added this for notifications. 
    //Without this, I don't believe the user gets the opportunity to approve notifications from the app
    let settings = UIUserNotificationSettings(forTypes: [.Alert, .Badge], categories: nil)
    UIApplication.sharedApplication().registerUserNotificationSettings(settings)

    return true
}

//Goes in viewController
func weeklyNotifications () {
    let localNotification = UILocalNotification()
    localNotification.fireDate = NSDate(timeIntervalSinceNow: 60*60)
    localNotification.alertBody = "This is the message"
    localNotification.timeZone = NSTimeZone.localTimeZone()
    localNotification.repeatInterval = NSCalendarUnit.WeekOfYear
    localNotification.soundName = UILocalNotificationDefaultSoundName
    localNotification.category = "Message" //Optional
    UIApplication.sharedApplication().scheduleLocalNotification(localNotification)
}

In other words, the result of this code is that the first time I run the app, I can execute this code and the notification will repeat every week without me running additional code?


回答1:


localNotification.repeatInterval = NSCalendarUnit.WeekOfYear

Yes this will schedule local notification for every week.



来源:https://stackoverflow.com/questions/35601515/recurring-local-notification-in-ios

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!