2017-01-09 24 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”)。 –