Пользовательское локальное уведомление в фоновом режиме Swift 4 - PullRequest
0 голосов
/ 06 сентября 2018

Я написал некоторый код для реализации пользовательских уведомлений в моем приложении, но, похоже, он не работает, когда приложение находится в фоновом режиме. Вот код ниже:

let content = UNMutableNotificationContent()
        content.title = "test notifaction"
        content.body = "test notification after 5 second"
        content.sound = UNNotificationSound.default()

        let trigger = UNTimeIntervalNotificationTrigger(timeInterval: 5, repeats: true)
        let request  = UNNotificationRequest(identifier: "testidentifire", content: content, trigger: trigger)

В приложении делегат

//user notification method
    func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
        completionHandler([.alert,.sound])
    }
    //response to user notification
    func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
        if response.notification.request.identifier == "testidentifire"
        {
            print("test")
        }
        completionHandler()
    }




    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {

        UNUserNotificationCenter.current().delegate = self

        UNUserNotificationCenter.current().requestAuthorization(options: [.alert,.sound,.badge]) { (granted, error) in
            print("granted\(granted)")
        }
        return true
    }

теперь, когда я ищу, я находил один и тот же код везде, где не могу понять, что не так с моим кодом

1 Ответ

0 голосов
/ 06 сентября 2018

попробуйте это, по вашему мнению, действительно ли это работает у меня

 //sendign local notification you need three object a contant,trigger,represh
        let content = UNMutableNotificationContent()
        content.title = "test notifaction"
        content.body = "test notification after 5 second"
        content.sound = UNNotificationSound.default()

        let trigger = UNTimeIntervalNotificationTrigger(timeInterval: 5, repeats: true)
        let request  = UNNotificationRequest(identifier: "testidentifire", content: content, trigger: trigger)

        UNUserNotificationCenter.current().add(request) { (error) in
            print("error\(error )")

        }
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...