Swift 6での書き方
現在はUserNotificationsを使うよ。通知が必要になるタイミングでTaskやasync関数から呼び出し、戻り値とエラーを処理してね。ユーザーは許可を拒否したり、あとから設定を変更したりできるよ。Apple公式:通知の許可
|
1 2 3 4 5 6 |
import UserNotifications func requestNotificationPermission() async throws -> Bool { try await UNUserNotificationCenter.current() .requestAuthorization(options: [.sound, .alert, .badge]) } |
以下は旧バージョン当時の記録
Swiftを久しぶりに再開してみて、Local Notificationを実装しようとして出た以下のエラー。
Swift 2.0 – Binary Operator “|” cannot be applied to two UIUserNotificationType operands
訂正前のLocal Notification
|
1 |
application.registerUserNotificationSettings(UIUserNotificationSettings(forTypes: UIUserNotificationType.Sound | UIUserNotificationType.Alert | UIUserNotificationType.Badge, categories: nil)) |
訂正後のLocal Notification
|
1 |
application.registerUserNotificationSettings(UIUserNotificationSettings(forTypes: [.Sound, .Alert, .Badge], categories: nil)) |
こんな感じで書き方が変わってるので注意。
参考: Swift 2.0 – Binary Operator “|” cannot be applied to two UIUserNotificationType operands
