2017-01-09 23 views
0

我實施了Estimote SDK並添加了信標測距/掃描委託方法。現在,我想在設備剛進入信標區域時觸發本地通知。在這裏我遇到了本地通知的問題。 當我進入燈塔區時,我得到的通知很好,但它在隨機間隔後重復多次。我不止一次收到本地通知。如何在iOS/Swift3中限制來自EstimoteSDK的多個通知?

第二件事是,有什麼辦法來清除通知區域觸發的通知。因爲我一次只想顯示1個通知。所以,當它觸發本地通知時,它應該清除來自通知區域的現有通知,如果已經存在的話。

我試着用cancelLocalNotificationcancelAllLocalNotifications但通知沒有被刪除。

+0

請分享您的代碼,看看您可能會做錯什麼。 –

回答

0

這是我實現的代碼:

beaconRegion.notifyOnEntry = true 
    beaconRegion.notifyOnExit = true 
    self.beaconManager.delegate = self 
    self.beaconManager.requestAlwaysAuthorization() 
    self.beaconManager.startMonitoring(for: beaconRegion) 

func beaconManager(_ manager: Any, didEnter region: CLBeaconRegion) { 
    if isAuthenticated == nil { 
     return 
    } 

    let notification = UILocalNotification() 
    notification.alertBody = "You are in beacon range." 
    notification.fireDate = Date.init(timeIntervalSinceNow: 2) 
    notification.userInfo = ["enterInBeaconRange":true] 
    UIApplication.shared.presentLocalNotificationNow(notification) 

} 

現在,如果我設置「cancelAllLocalNotifications」的方法通知之前,它不清除從通知中心所有以前的通知。

+0

您無法取消已觸發的通知。你可以爲預定的人做。 –

+0

新框架** UNUserNotificationCenter **有可能嗎? –

+0

對不起,回覆遲了,可以使用'removeAllDeliveredNotification()'來清除以前的通知。最好的方法是使用傳遞通知的標識符,然後使用removeDeliveredNotifications(「PREVIOUSLY_DEVLIVERED_NOTIFICATION_ID」)。 –