如何在 iOS 应用程序上放置通知每一 (1) 小时重复一次? [英] How to put notifications on iOS application to repeat every one (1) hour?

查看:39
本文介绍了如何在 iOS 应用程序上放置通知每一 (1) 小时重复一次?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在我的应用程序中放置通知,它应该每隔一小时重复一次,但它重复不受管制,要清楚,它有时会重复 30 分钟有时一小时有时会很长时间等等.我在AppDelegate.swift"中使用的代码:

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  
}

和我在ViewController.swift"中使用的代码:

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 ?

推荐答案

您根本不需要计时器.UILocalNotification 类有一个名为 repeatInterval,正如您所料,设置重复通知的间隔.

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)
}

注意:确保在您只启动一次通知时执行代码,因为每次执行时它都会安排不同的通知.为了更好地理解本地通知,您可以阅读 iOS 8 中的 Swift 本地通知(第 1 部分)iOS 8 中使用 Swift 的本地通知(第 2 部分).

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).

这篇关于如何在 iOS 应用程序上放置通知每一 (1) 小时重复一次?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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