Tar*_*riq 2 push-notification ios appdelegate swift
当应用程序运行并收到推送通知时,会调用 didReceive 。
func userNotificationCenter(
_ center: UNUserNotificationCenter,
didReceive response: UNNotificationResponse,
withCompletionHandler completionHandler: @escaping () -> Void
)
Run Code Online (Sandbox Code Playgroud)
因此,当调用上述委托时,我会使用收到的有效负载呈现一个屏幕。这里没有问题。
当应用程序未运行并且用户点击通知时,它应该显示与上面相同的屏幕。它不起作用,因为我没有在 didFinishLaunchingWithOptions 中添加代码。
所以,然后我添加了以下代码 -
func application(
_ application: UIApplication,
didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?
) -> Bool {
......
if let userInfo = launchOptions?[UIApplication.LaunchOptionsKey.remoteNotification] as? [AnyHashable: Any] {
......
}
return true
}
Run Code Online (Sandbox Code Playgroud)
但这不起作用,我无法调试,因为在调试模式下,我必须从后台终止应用程序并点击通知,但在这种情况下,调试器将无法工作。我尝试了替代方法,即显示警报,但警报也不起作用
let aps = remoteNotif["aps"] as? [AnyHashable: Any]
let string = "\n Custom: \(String(describing: aps))"
let string1 = "\n Custom: \(String(describing: remoteNotif))"
DispatchQueue.main.asyncAfter(deadline: .now() + 5) { [weak self] in
if var topController = application.windows.first?.rootViewController {
while let presentedViewController = topController.presentedViewController {
topController = presentedViewController
}
let ac = UIAlertController(title: string1, message: string, preferredStyle: .alert)
ac.addAction(UIAlertAction(title: "OK", style: .default))
topController.present(ac, animated: true)
}
}
Run Code Online (Sandbox Code Playgroud)
我应该如何解决这个问题?
\xe2\x80\x9c但在这种情况下调试器将无法工作\xe2\x80\x9d 不正确!即使 Xcode 没有启动它,您也可以在启动时附加调试器。
\n编辑方案,然后在“运行”操作中的“信息”下,显示“启动”,单击第二个单选按钮:\xe2\x80\x9c等待可执行文件启动。\xe2\x80\x9d 运行应用程序;它不会启动\xe2\x80\x99。现在通过推送通知启动应用程序。调试器工作了。
\n| 归档时间: |
|
| 查看次数: |
1460 次 |
| 最近记录: |