如何在应用程序未运行时调试远程推送通知并点击推送通知? [英] How to debug remote push notification when app is not running and tap push notification?

查看:29
本文介绍了如何在应用程序未运行时调试远程推送通知并点击推送通知?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当应用程序运行并收到推送通知时,didReceive 被调用.

When app is running and it receive push notification then didReceive is called.

func userNotificationCenter(
        _ center: UNUserNotificationCenter,
        didReceive response: UNNotificationResponse,
        withCompletionHandler completionHandler: @escaping () -> Void
    )

因此,当调用上述委托时,我会使用收到的有效负载显示一个屏幕.这里没有问题.

So when above delegate is called then i present a screen using the payload i receive. There is no problem here.

当应用程序没有运行并且用户点击通知时,它应该显示与上面相同的屏幕.它不起作用,因为我没有在 didFinishLaunchingWithOptions 中添加代码.

When app is not running and user tap the notification then it should present the same screen like above. It's not working because i didn't added a code in didFinishLaunchingWithOptions.

所以,然后我添加了以下代码 -

So, then i added the following code -

func application(
        _ application: UIApplication,
        didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?
    ) -> Bool {
        
         ......        
    
            
        if let userInfo = launchOptions?[UIApplication.LaunchOptionsKey.remoteNotification] as? [AnyHashable: Any] {
         ......
        }
        
        return true
    }

但这不起作用,我无法调试,因为在调试模式下我必须从后台终止应用程序并点击通知,但在这种情况下调试器将无法工作.我尝试了替代方法,即显示警报但警报也不起作用

But this is not working and i cannot debug because when in debug mode i have to kill the app from background and tap the notification but in this case the debugger won't work. I tried alternative method i.e. showing alert but then alert is also not working

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)
                }
            }

我该如何解决这个问题?

How should i solve this problem ?

推荐答案

我通过实现 sceneDelegate willConnectTo 方法解决了这个问题.不需要在 didFinishLaunchingWithOptions 中处理

I have solved it by implementing sceneDelegate willConnectTo method. There is no need to handle it in didFinishLaunchingWithOptions

func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
//Remote notification response
   if let response = connectionOptions.notificationResponse{
        print(response.notification.request.content.userInfo)
   }

   ....
} 

这就够了

这篇关于如何在应用程序未运行时调试远程推送通知并点击推送通知?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆