Локальные уведомления планируются, но не доставляются в приостановленном и не запущенном режимах. - PullRequest
0 голосов
/ 10 апреля 2019

Я хочу добавить фоновую выборку для моего приложения, когда приходит тихое удаленное уведомление (с content-available: 1), поэтому, когда оно приходит, я запускаю фоновое обновление локально, проверяю новые данные и, если да, генерирую локальноеpush-уведомление для пользователя.Тем не менее, он прекрасно работает, только когда приложение находится в фоновом режиме, не приостановлено или не запущено.В этих состояниях я получаю его для получения некоторых данных, но локальное уведомление не запускается (оно создается, как видно на консоли устройства), есть идеи, как мне это решить?

Вот мой код для генерации push (работает как положено)

func generatePushNotification(with title: String, body: String, badge: Int = 1){
        let content = UNMutableNotificationContent()
        content.badge = badge as NSNumber
        content.title = title
        content.body = body
        content.sound = UNNotificationSound.default
        let trigger = UNTimeIntervalNotificationTrigger(timeInterval: 1,
                                                        repeats: false)
        let notification = UNNotificationRequest(identifier: "pushLocal", content: content, trigger: trigger)
        UNUserNotificationCenter.current().add(notification) { (error) in
            if error != nil {
                NSLog((error?.localizedDescription)!)
            }
        }
    }

Вот мой код для извлечения уведомлений

func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
        networkManager.getScheduleForGroup(group) { (resp, error) in
            self.generatePushNotification(with: "Back", body: "ground")
            completionHandler(.newData)

        }
        completionHandler(.noData)

    }

Вот вывод на консоль:

dasd    CANCELED: com.apple.pushLaunch.**:297454 <private>!

SpringBoard    Saving notification 802A-AE8C: 0 [ hasAlertContent: 0, shouldPresentAlert: 1 ]

SpringBoard  Delivered user visible push notification 802A-AE8C

SpringBoard  Load 0 pending notification dictionaries

SpringBoard  Adding notification 802A-AE8C to destinations 0 [ hasAlertContent: 0, shouldPresentAlert: 1 hasSound: 0 shouldPlaySound: 1 ]

Ответы [ 2 ]

2 голосов
/ 10 апреля 2019
let content = UNMutableNotificationContent()
content.title = title
content.body = body
content.sound = UNNotificationSound.default()

var dateComponents = Calendar.current.dateComponents([.hour, .minute, .second], from: Date())

let trigger = UNCalendarNotificationTrigger(dateMatching: dateComponents, repeats: false)

let request = UNNotificationRequest(identifier: identifier, content: content, trigger: trigger)

UNUserNotificationCenter.current().add(request, withCompletionHandler: { (error) in
    if let error = error{
        print(error)
    }
})
UNUserNotificationCenter.current().delegate = self

Используйте это

0 голосов
/ 10 апреля 2019

// AppDelegate

UNUserNotificationCenter.current().delegate = self
let authOptions: UNAuthorizationOptions = [.alert, .badge, .sound]
        UNUserNotificationCenter.current().requestAuthorization(
            options: authOptions,
            completionHandler: {_, _ in })


application.registerUserNotificationSettings(UIUserNotificationSettings(types: [.alert, .badge, .sound], categories: nil))

// В ViewController

 let content = UNMutableNotificationContent()
        content.title = “Notification Title”
        content.body = “Notification Body”
        content.sound = UNNotificationSound.default()
        content.badge = 1

        let date = Date()

        let localDate = date.toLocalTime()

        var triggerDaily = Calendar.current.dateComponents([.hour,.minute,.second,], from: localDate)
        triggerDaily.hour = 22
        triggerDaily.minute = 00
        triggerDaily.second = 00

        let trigger = UNCalendarNotificationTrigger(dateMatching: triggerDaily, repeats: true)

        let identifier = "Local Notification"
        let request = UNNotificationRequest(identifier: identifier, content: content, trigger: trigger)

        UNUserNotificationCenter.current().add(request) { (error) in
            if let error = error {
                print("Error \(error.localizedDescription)")
            }
        }

// Добавить расширение для преобразования времени

extension Date { 

  func convertToGlobalTime() -> Date {
     let currentTimezone = TimeZone.current
     let interval = -TimeInterval(currentTimezone.secondsFromGMT(for: self))
     return Date(timeInterval: interval, since: self)
 }

 func convertToLocalTime() -> Date {
     let currentTimezone = TimeZone.current
     let interval = TimeInterval(currentTimezone(for: self))
     return Date(timeInterval: interval, since: self)
 }

}
...