调试 Crashlytics 崩溃报告:UserNotificationCenter:DidReceiveNotification

问题描述 投票:0回答:1

在我们的一个应用程序上,我们收到了几次崩溃,我无法确定发生了什么。 我已经在手机上重新加载了该应用程序,并尝试让它崩溃,但到目前为止它按设计和预期工作。

我附上了一份崩溃报告的副本,所以如果有人能帮助我破译这一点,我将不胜感激。 注意一点:该报告引用了一些与我们的源代码不对应的行号,所以我只能想象它引用的是编译版本,我不知道如何深入研究这些并找到问题,所以如果有这是我非常感激的一种方式。

          Crashed: com.apple.main-thread
0  Bible                          0x239cc specialized AppDelegate.userNotificationCenter(_:didReceive:withCompletionHandler:) + 147 (AppDelegate.swift:147)
1  Bible                          0x230c8 @objc AppDelegate.userNotificationCenter(_:didReceive:withCompletionHandler:) + 4332220616 (<compiler-generated>:4332220616)
2  CoreFoundation                 0x210f4 <redacted> + 148
3  CoreFoundation                 0x20144 <redacted> + 428
4  OneSignalCore                  0x8204 +[SwizzlingForwarder callSelector:onObject:withArgs:] + 228
5  OneSignal                      0x26828 +[OneSignalUNUserNotificationCenter forwardReceivedNotificationResponseWithCenter:didReceiveNotificationResponse:OneSignalCenter:withCompletionHandler:] + 236
6  OneSignal                      0x26a88 -[OneSignalUNUserNotificationCenter onesignalUserNotificationCenter:didReceiveNotificationResponse:withCompletionHandler:] + 304
7  ???                            0x1904fd57c (Missing)
8  ???                            0x18fe088a4 (Missing)
9  ???                            0x18fe063d8 (Missing)
10 ???                            0x18fe05e44 (Missing)
11 ???                            0x18fec56ac (Missing)
12 ???                            0x18fec20d4 (Missing)
13 ???                            0x18fec1d28 (Missing)
14 FrontBoardServices             0x36a20 __95-[FBSScene _callOutQueue_didCreateWithTransitionContext:alternativeCreationCallout:completion:]_block_invoke + 288
15 FrontBoardServices             0x36e10 -[FBSScene _callOutQueue_coalesceClientSettingsUpdates:] + 68
16 FrontBoardServices             0x36884 -[FBSScene _callOutQueue_didCreateWithTransitionContext:alternativeCreationCallout:completion:] + 436
17 FrontBoardServices             0x55280 __93-[FBSWorkspaceScenesClient _callOutQueue_sendDidCreateForScene:transitionContext:completion:]_block_invoke.197 + 276
18 FrontBoardServices             0x14cfc -[FBSWorkspace _calloutQueue_executeCalloutFromSource:withBlock:] + 168
19 FrontBoardServices             0x5389c -[FBSWorkspaceScenesClient _callOutQueue_sendDidCreateForScene:transitionContext:completion:] + 468
20 libdispatch.dylib              0x40d0 _dispatch_client_callout + 20
21 libdispatch.dylib              0x7b14 _dispatch_block_invoke_direct + 284
22 FrontBoardServices             0x163b8 __FBSSERIALQUEUE_IS_CALLING_OUT_TO_A_BLOCK__ + 52
23 FrontBoardServices             0x16338 -[FBSMainRunLoopSerialQueue _targetQueue_performNextIfPossible] + 240
24 FrontBoardServices             0x16210 -[FBSMainRunLoopSerialQueue _performNextFromRunLoopSource] + 28
25 CoreFoundation                 0x57088 <redacted> + 28
26 CoreFoundation                 0x5701c <redacted> + 176
27 CoreFoundation                 0x54b6c <redacted> + 344
28 CoreFoundation                 0x53d04 <redacted> + 840
29 CoreFoundation                 0x535b8 CFRunLoopRunSpecific + 572
30 GraphicsServices               0x11c4 GSEventRunModal + 164
31 ???                            0x18ffc25f0 (Missing)
32 ???                            0x19007110c (Missing)
33 Bible                          0x23134 main + 24 (AppDelegate.swift:24)
34 ???                            0x1b2c3fd34 (Missing)

这是 didReceiveNotification 的代码

extension AppDelegate: UNUserNotificationCenterDelegate {
    
    func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
        UNUserNotificationCenter.current().delegate = self
        print(response)
        let userInfo = response.notification.request.content.userInfo
        let key = userInfo["customData"] as! String
        print(key)
        // we send this key to the view controller
        inBoundParm = key
    }
}

根据 Larme 的评论,他是正确的。 我不确定我们如何不包含completionHandler,但更重要的是,传入的数据应该始终为字符串而不是nil。

我继续将其修改为以下内容:

        let key = userInfo["customData"] as? String ?? ""

这将防止数据为零,并且我们在 ViewController 中将其作为字符串正确处理。 虽然这确实解决了这个问题,但我无法从 crashlytics 中确定确切的行。 也许这只是事故发生时的调查工作。

Larme,谢谢您的帮助!

swift crashlytics unusernotificationcenter
1个回答
0
投票
let key = userInfo["customData"] as! String

这句话是在大声喊叫:我可能会造成车祸!每次使用

!
时,都是崩溃的迹象。

如果

userInfo["customData"]
nil
或者不是
String
,它将崩溃。

使用

guard let
if let
或按照您使用的
as? String ?? ""
来避免出现此问题。

此外,一旦该委托方法中的工作完成,就应该调用

completionHandler()

© www.soinside.com 2019 - 2024. All rights reserved.