[英]'UNLocationNotificationTrigger' is not available in Mac Catalyst
我正在嘗試通過 Catalyst 將應用程序移植到 MacOS,但我立即遇到了障礙。
UNLocationNotificationTrigger 在 MacOS 中不可用是完全有意義的。 您不會在筆記本電腦打開導航的情況下四處游盪。 但是,無論我是否使用@available(iOS 13.0, *)
或#if.(TARGET_OS_MACCATALYST)... #endif
,它都不會構建。
這是 class 的代碼:
import CoreLocation
import UserNotifications
@available(iOS 13.0, *)
class NotificationManager {
// MARK: Shared Instance
static let session = NotificationManager()
// MARK: Initialisers
init() {
self.askForNotificationPermissions()
}
// MARK: Properties
var isPermitted: Bool = false
// MARK: Start Route Methods
public func addStationStartNotification(for stations: [Stations.Station]) {
guard UserLocation.current.isPermitted && self.isPermitted else { return }
for station in stations {
let region = CLCircularRegion(center: station.coordinates(), radius: 50, identifier: station.name)
region.notifyOnEntry = true
region.notifyOnExit = false
let content = self.createStationStartContent(for: station)
let startJourneyAction = UNNotificationAction(identifier: "Start Journey", title: "Start Journey", options: .foreground)
#if !(TARGET_OS_MACCATALYST)
let trigger = UNLocationNotificationTrigger(region: region, repeats: true)
#endif
let request = UNNotificationRequest(identifier: UUID().uuidString, content: content, trigger: trigger)
let category = UNNotificationCategory(identifier: "Start Journey", actions: [startJourneyAction], intentIdentifiers: [], hiddenPreviewsBodyPlaceholder: "", options: .customDismissAction)
self.addNotification(request)
self.addCategory(category)
}
}
private func createStationStartContent(for station: Stations.Station) -> UNNotificationContent {
let notification = UNMutableNotificationContent()
notification.title = "Start a Journey?"
notification.body = "It looks like you're near \(station.name) station."
notification.sound = UNNotificationSound(named: UNNotificationSoundName(rawValue: "Resources/Alert Tones/alert_tone.caf"))
notification.categoryIdentifier = "Start Journey"
return notification
}
}
extension NotificationManager {
private func askForNotificationPermissions() {
UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .sound, .badge]) { (authorized, error) in
if error != nil {
self.isPermitted = false
} else {
self.isPermitted = authorized
}
}
}
private func addNotification(_ notification: UNNotificationRequest) {
let center = UNUserNotificationCenter.current()
center.removeAllPendingNotificationRequests()
center.removeAllDeliveredNotifications()
center.getPendingNotificationRequests { (pendingNotifications) in
if !pendingNotifications.contains(where: {$0.content.title == notification.content.title && $0.content.body == notification.content.body}) {
center.add(notification) { (error) in
if let error = error {
fatalError(error.localizedDescription)
}
}
}
pendingNotifications.forEach({print("[NotificationManager] Pending Notification: \($0.content.title) - \($0.content.body)")})
}
}
private func addCategory(_ category: UNNotificationCategory) {
let center = UNUserNotificationCenter.current()
center.getNotificationCategories { (categories) in
if !categories.contains(category) {
var newCategories = categories
newCategories.insert(category)
center.setNotificationCategories(newCategories)
}
}
}
private func removeNotification(title: String = "", body: String = "") {
let center = UNUserNotificationCenter.current()
center.getPendingNotificationRequests { (pendingNotifications) in
let notificationsToRemove = pendingNotifications.filter({$0.content.title == title && $0.content.body == body}).map({$0.identifier})
center.removePendingNotificationRequests(withIdentifiers: notificationsToRemove)
}
}
}
用條件換行是不夠的,您還應該對依賴於該行的每一段代碼都這樣做。
例如,在這里創建請求時不能使用變量trigger
,因為編譯器看不到該變量的定義位置:
#if !(TARGET_OS_MACCATALYST)
let trigger = UNLocationNotificationTrigger(region: region, repeats: true)
#endif
let request = UNNotificationRequest(identifier: UUID().uuidString, content: content, trigger: trigger)
PS 我更喜歡使用#if !targetEnvironment(macCatalyst)
而不是#if !(TARGET_OS_MACCATALYST)
因為前者是后者的 Swifty 版本。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.