Ios 为什么我在Swift中收到多个UILocalNotification?
这是我的职责:Ios 为什么我在Swift中收到多个UILocalNotification?,ios,swift,uilocalnotification,Ios,Swift,Uilocalnotification,这是我的职责: func sendNotificationEvery() { print("HEY!") notification.alertBody = "Message here" // text that will be displayed in the notification notification.fireDate = NSDate() // right now (when notification will be fired) notificat
func sendNotificationEvery() {
print("HEY!")
notification.alertBody = "Message here" // 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.Minute // this line defines the interval at which the notification will be repeated
notification.applicationIconBadgeNumber = 1
UIApplication.sharedApplication().scheduleLocalNotification(notification)
}
我称之为:
override func viewWillDisappear(animated: Bool) {
sendNotificationEvery()
}
所以,问题是:当我关闭我的应用程序时,它会发送3-4个通知,而不是1个。如何解决此问题?我想知道,为什么会发生这种情况?首先,我认为您的要求是“在关闭应用程序时,您必须显示通知” 因此,不要在视图中编写上述代码,而是在Appdelegate类中编写-(void)applicationidentinterbackground:(UIApplication*)application方法
这将解决您的问题。您是否曾经取消旧通知的计划?使用
UIApplication.sharedApplication().cancelAllLocalNotifications
取消旧通知。为什么将重复间隔设置为每分钟?@Muneeba我正在测试。稍后它将是1或3hours@Sulthan在这种情况下,它会重复通知吗?它仍然会向我发送3-4条通知进行测试,只需卸载应用程序并将设备时间更改为将来的日期。现在就开始测试应用程序。从ViewWillEnglishe方法中删除代码。增加notification.repeatInterval并检查。