我正在尝试做基于位置的项目。当我收到通知时,我会按是按钮并转到应用程序中的位置页面。要执行此过程,我应该使用通知标题,但我正在核心位置的 didenterregion 方法中创建通知。对于通知操作,我应该使用 didreceiveresponse 方法。如何从 didenterregion 中获取 didreceiveresponse 方法的通知标题?
这是我的代码
UNUserNotificationCenter.current().requestAuthorization(
options: [.alert,.sound,.badge],
completionHandler: { (didAllow,error) in
}
)
let answerYes = UNNotificationAction(identifier: "answerYes", title: "Evet", options: UNNotificationActionOptions.foreground)
let answerCancel = UNNotificationAction(identifier: "answerCancel", title: "İptal", options: UNNotificationActionOptions.foreground)
let category = UNNotificationCategory(identifier: "myCategory", actions: [answerYes,answerCancel], intentIdentifiers: [], options: [])
UNUserNotificationCenter.current().setNotificationCategories([category])
let topkapiLatitude: CLLocationDegrees = 41.013
let topkapiLongitude: CLLocationDegrees = 28.984
let topkapiCenter: CLLocationCoordinate2D = CLLocationCoordinate2DMake(topkapiLatitude, topkapiLongitude)
let topkapiRadius: CLLocationDistance = CLLocationDistance(100.0)
let topkapiIdentifier: String = "Topkapı Sarayı"
let topkapiRegion = CLCircularRegion(center: topkapiCenter, radius: topkapiRadius, identifier: topkapiIdentifier)
let yrbtnLatitude: CLLocationDegrees = 41.0081
let yrbtnLongitude: CLLocationDegrees = 28.9778
let yrbtnCenter: CLLocationCoordinate2D = CLLocationCoordinate2DMake(yrbtnLatitude, yrbtnLongitude)
let yrbtnRadius: CLLocationDistance = CLLocationDistance(100.0)
let yrbtnIdentifier: String = "Yerebatan Sarnıcı"
let yrbtnRegion = CLCircularRegion(center: yrbtnCenter, radius: yrbtnRadius, identifier: yrbtnIdentifier)
locationManager.distanceFilter = 10
locationManager.desiredAccuracy = kCLLocationAccuracyBest
topkapiRegion.notifyOnEntry = true
yrbtnRegion.notifyOnEntry = true
locationManager.delegate=self
UNUserNotificationCenter.current().delegate = self
locationManager.requestAlwaysAuthorization()
locationManager.startMonitoring(for: topkapiRegion)
locationManager.startMonitoring(for: yrbtnRegion)
locationManager.startUpdatingLocation()
return true
}
func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
if response.actionIdentifier == "answerYes" {
}
else {
}
}
func locationManager(_ manager: CLLocationManager, didEnterRegion region: CLRegion) {
let content = UNMutableNotificationContent()
content.title = region.identifier
content.subtitle = "You are at \(region.identifier) now"
content.body = "Do you want to learn history of this place?"
content.badge = 1
content.categoryIdentifier = "myCategory"
let trigger = UNTimeIntervalNotificationTrigger(timeInterval: 2, repeats: false)
let request = UNNotificationRequest(identifier: "timerDone", content: content, trigger: trigger)
UNUserNotificationCenter.current().add(request, withCompletionHandler: nil)
}
您可以通过UNMutableNotificationContent 的userInfo 属性自定义对象。
content.userInfo = ["yourkey": "Your object"] as [String : Any] // set in region method
并进入 ** didReceive**
print(response.notification.request.content.userInfo) // this is your data
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句