我的appDelegate有以下内容:
func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
let dictPayload = userInfo as NSDictionary
if let data = dictPayload.value(forKey: "aps") as? NSDictionary {
let link = data.value(forKey: "Link") as? String
print("link: \(link!)")
UserDefaults.standard.set(link, forKey: "link_from_push")
UserDefaults.standard.synchronize()
UserDefaults.standard.set("open_link", forKey: "push_action")
UserDefaults.standard.synchronize()
}
print("Push notification received: \(userInfo)")
}
我可以通过Xcode看到信息流。现在我想在应用程序打开并且在前台时弹出警报,无论用户当前在哪个视图上。
appDelegate使用以下函数:
let topWindow = UIWindow(frame: UIScreen.main.bounds)
topWindow.rootViewController = UIViewController()
topWindow.windowLevel = UIWindowLevelAlert + 1
let alert = UIAlertController(title: "APNS", message: "received Notification", preferredStyle: .alert)
alert.addAction(UIAlertAction(title: NSLocalizedString("OK", comment: "confirm"), style: .cancel, handler: {(_ action: UIAlertAction) -> Void in
// continue your work
// important to hide the window after work completed.
// this also keeps a reference to the window until the action is invoked.
topWindow.isHidden = true
}))
topWindow.makeKeyAndVisible()
topWindow.rootViewController?.present(alert, animated: true, completion: { _ in })
哪个工作正常,但我需要添加另一个操作按钮。
非常感谢任何帮助