How to put notifications on iOS application to repeat every one (1) hour?

Deadly 提交于 2019-12-23 03:13:16

问题


I tried to put notifications in my app, it was supposed to repeat every one hour but it repeat unregulated, to be clear, it repeats sometimes 30min sometimes one hour sometimes for a long time etc.. Code that I used in "AppDelegate.swift":

class AppDelegate: UIResponder, UIApplicationDelegate {

var window: UIWindow?

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

    //Notification Repeat
application.registerUserNotificationSettings(UIUserNotificationSettings(forTypes: UIUserNotificationType.Alert | UIUserNotificationType.Badge | UIUserNotificationType.Sound, categories: nil))


    return true  
}

and code that I used in "ViewController.swift":

//Notification Repeat
var Time = 1



override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.


    //Notification Repeat
    var Timer = NSTimer.scheduledTimerWithTimeInterval(3600.0, target: self, selector: Selector("activateNotifications"), userInfo: nil, repeats: true)
}



//Notification Repeat
func activateNotifications() {

    Time -= 1

    if (Time <= 0){



        var activateNotifications = UILocalNotification()

        activateNotifications.alertAction = “Hey"
        activateNotifications.alertBody = “Hello World!"


        activateNotifications.fireDate = NSDate(timeIntervalSinceNow: 0)


        UIApplication.sharedApplication().scheduleLocalNotification(activateNotifications)
    }
}

Can someone help me, where I made mistake ?


回答1:


You don't need the timer at all. The UILocalNotification class has a property entitled repeatInterval that, as you can expect, set the interval at which the notification will be repeated.

According to this, you can schedule a local notifications that is repeated every hour in the following way:

func viewDidLoad() {
    super.viewDidLoad()

    var notification = UILocalNotification()
    notification.alertBody = "..." // text that will be displayed in the notification        
    notification.fireDate = NSDate()  // right now (when notification will be fired)
    notification.soundName = UILocalNotificationDefaultSoundName // play default sound
    notification.repeatInterval = NSCalendarUnit.CalendarUnitHour // this line defines the interval at which the notification will be repeated
    UIApplication.sharedApplication().scheduleLocalNotification(notification)
}

NOTE: Be sure that you execute the code when you launch the notification only once, since it schedules a different notification every time that it is executed. For a better understanding of local notifications, you can read Local Notifications in iOS 8 with Swift (Part 1) and Local Notifications in iOS 8 with Swift (Part 2).



来源:https://stackoverflow.com/questions/31567984/how-to-put-notifications-on-ios-application-to-repeat-every-one-1-hour

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