本地通知无限期重复 - 斯威夫特

时间:2016-06-05 19:23:07

标签: ios swift localnotification

我安排了一个本地通知,应该在几个小时后激活,而无需用户使用该应用。我遇到的问题是,一旦它触发,通知会不断重复,直到应用程序进入前台。我希望它只出现一次。

func applicationDidEnterBackground(application: UIApplication) {

        localNotification.fireDate = NSDate(timeIntervalSinceNow: (86400)*3)
        localNotification.alertTitle = "Te Extrañamos"
        localNotification.alertBody = "No olvides tu estudio en Momentos de Tora"
        localNotification.timeZone = NSTimeZone.defaultTimeZone()
        localNotification.applicationIconBadgeNumber = UIApplication.sharedApplication().applicationIconBadgeNumber + 1
        UIApplication.sharedApplication().scheduleLocalNotification(localNotification)     
    }

func applicationWillEnterForeground(application: UIApplication) {
        // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
        UIApplication.sharedApplication().cancelLocalNotification(localNotification)
        UIApplication.sharedApplication().applicationIconBadgeNumber = 0
    }

提前感谢您的帮助。

1 个答案:

答案 0 :(得分:0)

您需要做的就是跟踪您是否已发送通知。在发送通知检查此变量之前,在方法中保存let sentNotification: bool = false之类的变量。然后在该检查中将其设置为true。在applicationDidEnterForground方法中,将其设置为false。因此,当达到参数时它将再次触发。