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

时间:2017-09-17 06:15:49

标签: ios iphone push-notification apple-push-notifications push

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

奇怪的行为是:

1-)如果应用程序没有运行。它会在推送通知中添加日历确定,但是......

2-)如果应用程序已经在后台运行(在前台不活动),它将不会添加到日历,直到用户将应用程序放入Foreground - Active。

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

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

1 个答案:

答案 0 :(得分:0)

经过一些调试并检查我用其他两个自定义操作实现的代码后,我在块外部进行了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.
            }

}

}