swiftcrashlyticsunusernotificationcenter

Debugging a Crashlytics Crash Report: UserNotificationCenter: DidReceiveNotification


On one of our apps we have received several crashes and I am unable to determine what is happening. I have reloaded the app on my phone and have tried to get it to crash but so far it's working as designed and expected.

I'm including a copy of the crash report, so if someone could help me decipher this I would be most appreciative. One a note: The report is referencing some line numbers that do not correspond to our source code so I can only imagine that it is referencing a compiled version, I'm not sure how to drill down on those and find the issue so if there is a way for that I would be most appreciative.

          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)

Here is the code for the 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
    }
}

Based on Larme's comments, he is correct. I am not sure how we didn't include the completionHandler but even more important is the fact that the data coming is should always be as a string and not a nil.

I went ahead and modified it to the following:

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

This will prevent the data from being nil and we are handling it correctly in the ViewController as a string. While this does solve this issue, I was not able to determine from the crashlytics the exact line. Maybe it's just investigative work when a crash happens.

Larme, thank you for the assistance!


Solution

  • let key = userInfo["customData"] as! String
    

    This line is yelling out loud: I might cause a crash! Everytime you use !, it's a sign for a crash.

    It will crash if userInfo["customData"] is nil or if it's not a String.

    Use a guard let, if let, or as you used as? String ?? "" to avoid the issue.

    Also, completionHandler() should be called once the work has been done in that delegate method.