将应用程序从后台移到前台时,无法触发didReceive通知委托

时间:2019-12-20 10:43:44

标签: ios swift xcode8 ios11

我已经为ios10实现了推送通知。点击通知警报将触发“ didReceive”委托,如果我将通知保存在coredata或静默通知中(如果我在前台)。问题是,如果我在后台收到一堆通知,并且当我将应用程序从后台调到前台时,是否可以调用“ didReceive”委托或其他任何推送通知委托,前提是我可以将项目同步到coredata。

注意 我不想使用静默通知在后台同步(didReceive或任何委托)项目,也不想点击警报。当我将应用程序置于前台时,它应该自动同步推送通知的堆栈

     func handleInboxNotification(didReceiveRemoteNotification userInfo: [AnyHashable: Any]) {
    if let inboxValue = userInfo["inbox"] as? String, let value = inboxValue.boolValue(), value {
        let mappedMessage = MessageCenterSDK.shared.constructReorderedDictionary(userInfo: userInfo)
        MessageCenterDataManager.shared.synchronize(messages: mappedMessage)
        messageCenterDelegate?.didFindNewMessages(hasNewMessages: true)
    }
}


func userNotificationCenter(_ center: UNUserNotificationCenter,
                            didReceive response: UNNotificationResponse,
                            withCompletionHandler completionHandler: @escaping () -> Void) {
    /// Handle outbound remote
    handleInboxNotification(didReceiveRemoteNotification: response.notification.request.content.userInfo)
    completionHandler()
}

2 个答案:

答案 0 :(得分:2)

我遇到了同样的问题,但是我的目标是针对iOS 13,我正在使用BGTaskScheduler从后台以及终止状态的服务器中获取数据。

我也想在应用程序处于后台时触发通知。但是尚未开发,但这似乎是不可能的,因此我们通过启用后台模式更改了实现方式,它对我很有用。

希望它也有帮助。

有关BGTaskSchedulerhttps://developer.apple.com/documentation/backgroundtasks/bgtaskscheduler

的更多参考

答案 1 :(得分:0)

您需要在appDelegate中使用willpresent委托

    func userNotificationCenter(_ center: UNUserNotificationCenter,
                            willPresent notification: UNNotification,
                            withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
    // Change this to your preferred presentation option
    completionHandler([])
}

通知进入前台时会调用此委托