【发布时间】:2021-05-10 07:23:46
【问题描述】:
当应用程序运行并收到推送通知时,会调用 didReceive。
func userNotificationCenter(
_ center: UNUserNotificationCenter,
didReceive response: UNNotificationResponse,
withCompletionHandler completionHandler: @escaping () -> Void
)
因此,当调用上述委托时,我会使用收到的有效负载呈现一个屏幕。这里没有问题。
当应用程序没有运行并且用户点击通知时,它应该显示与上面相同的屏幕。它不起作用,因为我没有在 didFinishLaunchingWithOptions 中添加代码。
所以,然后我添加了以下代码-
func application(
_ application: UIApplication,
didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?
) -> Bool {
......
if let userInfo = launchOptions?[UIApplication.LaunchOptionsKey.remoteNotification] as? [AnyHashable: Any] {
......
}
return true
}
但这不起作用,我无法调试,因为在调试模式下我必须从后台终止应用程序并点击通知,但在这种情况下调试器将无法工作。我尝试了替代方法,即显示警报,但警报也不起作用
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)
}
}
我应该如何解决这个问题?
【问题讨论】:
-
你有后台模式功能吗?
-
@ElTomato - 这有什么关系?因为我正在点击通知。根据苹果的说法,后台模式是-“应用程序提供的需要它在后台运行的服务”。后台没有运行任何东西。应用正在接收推送,我只需点击通知即可打开应用。
-
你写过“当应用程序没有运行并且用户点击通知时,它应该呈现与上面相同的屏幕”,但如果你这么说......
标签: ios swift push-notification appdelegate