2016-01-21 27 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

不,同樣的事情=/ –

相關問題