2016-01-21 26 views
0

这是我的函数:为什么我在Swift中获得多个UILocalNotifications?

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哪有我解决了这个问题?我想知道,为什么会发生?

+1

你有没有调整旧的通知? – Wain

+0

使用'UIApplication.sharedApplication()。cancelAllLocalNotifications'来取消旧的通知。 – Sulthan

+0

为什么你将重复间隔设置为每分钟? – Muneeba

回答

0

首先,我想你的要求是“在关闭应用程序时必须显示通知”。

按本,而不是在写上述代码viewWillDisappear,写在AppDelegate类,- (无效)applicationDidEnterBackground:(UIApplication的*)应用方法。

这将解决您的问题。

+0

它仍然会向我发送3-4个通知 –

+0

要进行测试,只需卸载应用并将设备时间更改为将来的日期。现在测试应用程序。从viewWillDisappear方法中删除代码。 –

+0

不,同样的事情=/ –

相关问题