推送通知中的自定义按钮操作

时间:2019-06-21 07:18:50

标签: ios swift

我在推送通知中添加了两个按钮,能够在应用程序处于前台或后台模式时执行此操作。我对此感到满意,但是当应用程序被杀死时我无法执行操作。

   func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void)
{
    let userInfo = response.notification.request.content.userInfo
    print(userInfo)
    guard let apsdata = userInfo[AnyHashable("data")] as? [String:AnyObject]  else {
            return
    }

    switch response.actionIdentifier {
    case "ACCEPT_ACTION":
         if let pushdatadict = apsdata["body"] as? [String:AnyObject] {
        NotificationCenter.default.post(name: NSNotification.Name(rawValue: "AcceptOfNotification"), object: nil, userInfo: pushdatadict)
         }
        print("request accepted buddy")
    case "DECLINE_ACTION":
        NotificationCenter.default.post(name: NSNotification.Name(rawValue: "RejectOfNotification"), object: nil)
        print("request decline buddy")
    default:
        break
    }

1 个答案:

答案 0 :(得分:0)

func didReceive(_响应:UNNotificationResponse,complementHandler完成:@escaping(UNNotificationContentExtensionResponseOption)->无效){

            switch response.actionIdentifier {
            case "ACCEPT_ACTION":
                APIToAcceptTheUserRequest()
                DispatchQueue.main.async(execute: {
                    if let url = URL(string: "open://Home") {
                        self.extensionContext?.open(url) { success in
                            print(success)
                        }
                    }
                })
                print("request accepted buddy")

            case "DECLINE_ACTION":
                APIToRejectTheUserRequest()

            default:
                break
            }
completion(UNNotificationContentExtensionResponseOption.dismissAndForwardAction)

}