iOS 自定义通知操作怪异行为添加日历

iOS Custom Notification Action weird behavior add calendar

想知道是否有人对此有任何经验。因此,当用户点击添加到日历时,我有一个自定义通知操作集 ( ADDTOCALENDAR ) y 运行将事件添加到默认日历的代码。

奇怪的行为是:

1- ) 如果应用不是 运行 。它在推送通知后立即添加到日历中,但是...

2-) 如果应用程序已经 运行 在后台(在前台未激活),它不会添加到日历,直到用户将应用程序置于前台 - 激活。

为什么如果应用程序 运行 在后台,它不会从推送添加到日历?

这正在发生 iOS 10.xx 和 iOS 11...

经过一些调试和检查代码后,我通过其他两个自定义操作意识到了 CompletionHandler() 在块外的调用。

所以,首先对于那些不知道的人来说,当你有一个自定义操作通知时,你检查类别标识符,然后检查自定义操作标识符,然后处理你想要的任何东西,永远不要忘记调用 completionHandler () 紧随其后。

我的问题是我使用的是 Class 服务,其功能带有块,并且 completionHandler 在块外。这就是为什么它会做出那种奇怪的行为。

这是完整的正确代码并显示了错误所在...

// AppDelegate.swif

   // Handling notifications - When in background
    func userNotificationCenter(_ center: UNUserNotificationCenter,
                                didReceive response: UNNotificationResponse,
                                withCompletionHandler completionHandler: @escaping () -> Void) {

   // Notifcation Category: Reserve
        if response.notification.request.content.categoryIdentifier == "RESERVE" {
            // Handle the actions for the reserve


                // Action: Approve
            if response.actionIdentifier == "APPROVE" {
                // The user launched the app
                print("Approve")

               // print(response.notification.request.content.userInfo)
                print(response.notification.request.content.userInfo.description)
                print(response.notification.request.content)

                if let info = response.notification.request.content.userInfo as? Dictionary<AnyHashable,Any> {
                    // Check if value present before using it

                    let reserveid = info["reserveid"] as? String ?? ""
                    let fecha = info["fecha"] as? String ?? ""
                    let username = info["username"] as? String ?? ""
                    let qty = info["qty"] as? String ?? ""
                    let comentario = info["comentario"] as? String ?? ""
                    let approval = info["approval"] as? String ?? ""
                    let empresa_nombre = info["empresanombre"] as? String ?? ""
                    let direccion = info["direccion"] as? String ?? ""
                    let lat = info["lat"] as? String ?? ""
                    let long = info["long"] as? String ?? ""
                    let telefono = info["tel"] as? String ?? ""
                    let provincia = info["prov"] as? String ?? ""


                    let reserveService = ReserveService()
                    reserveService.process(id: reserveid, approve: true, completion: { (success) in


                        if (success) {

                            let note = "\(empresa_nombre)\n\(direccion) - \(provincia)\n\nReserva de: \(username)\nCantidad de personas: \(qty)\nFecha: \(fecha)\nComentario de cliente: \(comentario)"
                            print(note)
                            let EventServices = EventService.sharedInstance
                            EventServices.startDate = "\(fecha)"
                            EventServices.title = "Reserva de \(username)"
                            EventServices.notes = note
                            EventServices.isRestadmin = true
                            EventServices.lat = lat
                            EventServices.long = long
                            EventServices.direccion = direccion
                            EventServices.provincia = provincia
                            EventServices.emp_nombre = empresa_nombre
                            EventServices.checkCalendarAuthorizationStatusAndAdd()
                           completionHandler() // correct completionHandler Call
                        }
                    })

                }
              //  completionHandler() // had it here mistakenly.
            }

}

}