【问题标题】:iOS Custom Notification Action weird behavior add calendariOS 自定义通知操作怪异行为添加日历
【发布时间】:2017-09-17 06:15:49
【问题描述】:

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

奇怪的行为是:

1-) 如果应用程序未运行。它会立即从推送通知中添加到日历中,但是...

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

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

iOS 10.xx 和 iOS 11 正在发生这种情况...

【问题讨论】:

  • 您是否将content-available 设置为1?如果不是这样,您的应用将不会在后台被唤醒......并且只有在它到达时点击它或成为前台才能使其工作
  • 嗨@Honey,内容可用用于静默通知。这是带有操作的普通通知,它显示将事件添加到设备默认日历的按钮。当应用程序未运行时,它会正确运行操作标识符的代码。但是当应用程序在后台并且用户在手机的家中并收到通知时,该操作仅在用户弹出时才会发生应用到前台使其处于活动状态...
  • 我不明白你们两个州的区别。您的第二个状态显然是 background 状态。您的第一个状态我不确定您的意思是什么状态。你的意思是 Suspended 状态吗?状态见here
  • 嗨@honey,我提供了答案:) 一定要看看! .关于我在谈论背景和活动状态的状态。我不确定我在帖子中是否解释得很好.. 问题是如果应用程序在后台运行,通知自定义操作运行不正常,它没有添加到日历中,直到我将应用程序置于前台(活动状态)再次......现在......如果应用程序根本没有运行......通知自定义操作将完美运行......问题是completionHandle()我有它在Block之外运行我的东西......;)检查出答案..
  • 哦,我明白了,为了清楚起见,您能否编辑您的问题并显示您做错了什么。

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


【解决方案1】:

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

所以,首先对于那些不知道的人,当你有一个自定义动作通知时,你检查类别标识符,然后检查自定义动作标识符,然后处理你想要的任何东西,永远不要忘记调用 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.
            }

}

}

【讨论】:

    猜你喜欢
    • 2014-03-22
    • 2017-08-16
    • 2016-05-26
    • 1970-01-01
    • 1970-01-01
    • 2022-10-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多