Проблема:
Когда я включаю параметр «Взаимодействие C++/Objective-C», локальные уведомления перестают работать. Ошибок и сбоев нет; уведомления просто не появляются.
Предпробованные действия:
- Проверил настройки уведомлений. и разрешения в приложении.
- Гарантировано, что код уведомления работает, когда настройка совместимости отключена.
- Версия Xcode: 15.0.1
- Цель развертывания iOS: 13.0
Язык: Swift
AppDelegate.swift
Код: Выделить всё
import UIKit
import UserNotifications
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
UNUserNotificationCenter.current().delegate = self
UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .sound, .badge]) { granted, error in
if granted {
print("Notification permission granted.")
} else if let error = error {
print("Notification permission error: \(error)")
}
}
return true
}
}
extension AppDelegate: UNUserNotificationCenterDelegate {
private func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
completionHandler([.alert, .sound])
}
func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
completionHandler()
}
}
Код: Выделить всё
@IBAction func submitBtn(_ sender: Any) {
let content = UNMutableNotificationContent()
content.title = "Testing Notification"
content.body = "This is a test notification"
let request = UNNotificationRequest(identifier: "MainViewController", content: content, trigger: nil)
UNUserNotificationCenter.current().add(request) { error in
if let error = error {
print("MainViewControllerLog", "Error delivering notification: \(error)")
} else {
print("MainViewControllerLog", "Notification scheduled: \(content.title) - \(content.body)")
}
}
}
Подробнее здесь: https://stackoverflow.com/questions/786 ... withcomple
Мобильная версия