问题描述
我正在创建一个程序,当用户进入指定位置时通知他们。该代码对我来说看起来正确,但我无法让它实际显示通知。当我将触发器更改为 UNTimeIntervalNotificationTrigger 时它确实有效,但这只会根据指定的时间限制而不是位置通知用户。下面是我的代码,我希望有人可以为我提供指导或指向其他可以解释为什么会出现此问题的资源。
我对 iOS 编程很陌生,所以非常感谢! :)
import SwiftUI
import UserNotifications
import CoreLocation
struct ContentView: View {
private let locationManager = CLLocationManager()
var body: some View {
vstack {
Button(action: {
self.locationManager.requestAlwaysAuthorization()
}) {
Text("Request authorization")
}
Button("Request Permission") {
UNUserNotificationCenter.current().requestAuthorization(options: [.alert,.badge,.sound]) { success,error in
if success {
print("All set!")
} else if let error = error {
print(error.localizedDescription)
}
}
}
Button("Notify when arrived at location") {
let content = UNMutableNotificationContent()
content.title = "Feed the cat"
content.subtitle = "It looks hungry"
content.sound = UNNotificationSound.default
//sets geofence
let center = CLLocationCoordinate2D(latitude: 37.7861353,longitude: -122.4532538)
let region = CLCircularRegion(center: center,radius: 2000.0,identifier: "Headquarters")
region.notifyOnEntry = true //notify user when they enter
region.notifyOnExit = false //do not notify when they exit
//when user enters,trigger notification
let trigger = UNLocationNotificationTrigger(region: region,repeats: false)
// choose a random identifier
let request = UNNotificationRequest(identifier: UUID().uuidString,content: content,trigger: trigger)
print("waiting to enter location")
// add our notification request
UNUserNotificationCenter.current().add(request)
}
}
}
}
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)