Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

ios game crashes with Mobile Notifications - objc exception in GetScheduledNotifications

Discussion in 'iOS and tvOS' started by Eli1234, Jul 9, 2021.

  1. Eli1234

    Eli1234

    Joined:
    Jul 13, 2017
    Posts:
    24
    Hi,


    We’ve just released a new version of our game built using Unity 2019.4.11f1 and Unity’s Mobile Notifications package version 1.4.1. We’re seeing a large number of crashes coming from the Mobile Notifications package that look like they’re caused by an unhandled objective c exception when trying to query all scheduled notifications.


    The previous build of our game was using version 1.3.0 of the Mobile Notifications package, and we did not see these issues. However, we did have other problems which is why we tried upgrading.


    Has anyone else seen this problem? Is there a workaround available? Is there a stable version of Mobile Notifications that we should be using, or should we just abandon it and use a 3rd party solution from the asset store?


    The crash reports that I see have the following exception call stack:

    Code (CSharp):
    1.  
    2. Exception Type:  EXC_CRASH (SIGABRT)
    3.  
    4. Exception Codes: 0x0000000000000000, 0x0000000000000000
    5.  
    6. Exception Note:  EXC_CORPSE_NOTIFY
    7.  
    8. Triggered by Thread:  0
    9.  
    10.  
    11.  
    12. Last Exception Backtrace:
    13.  
    14. 0   CoreFoundation                    0x183461754 __exceptionPreprocess + 220 (NSException.m:199)
    15.  
    16. 1   libobjc.A.dylib                   0x197f287a8 objc_exception_throw + 60 (objc-exception.mm:565)
    17.  
    18. 2   Foundation                        0x18472086c +[NSJSONSerialization dataWithJSONObject:options:error:] + 316 (NSJSONSerialization.m:0)
    19.  
    20. 3   UnityFramework                    0x103a154ec ___ReadNSDictionary_block_invoke + 2184428 (UnityNotificationData.m:0)
    21.  
    22. 4   CoreFoundation                    0x1833c5a68 __NSDICTIONARY_IS_CALLING_OUT_TO_A_BLOCK__ + 24 (NSDictionaryHelpers.m:9)
    23.  
    24. 5   CoreFoundation                    0x18333b530 -[__NSDictionaryI enumerateKeysAndObjectsWithOptions:usingBlock:] + 184 (NSDictionaryI.m:151)
    25.  
    26. 6   UnityFramework                    0x103a153e4 _ReadNSDictionary + 2184164 (UnityNotificationData.m:217)
    27.  
    28. 7   UnityFramework                    0x1062852ac iOSNotificationsWrapper_NSDictionaryToCs_m1643FA2EF77A85CB3D33A33D000C903C6F9B8A28 + 44585644 (Unity.Notifications.iOS.cpp:6953)
    29.  
    30. 8   UnityFramework                    0x106285838 iOSNotificationCenter_NotificationDataToNotifications_mCCFE8338BE0D730A258AC3DB011FA115C61DFF54 + 44587064 (Unity.Notifications.iOS.cpp:5306)
    31.  
    32. 9   UnityFramework                    0x1062856d0 iOSNotificationCenter_GetScheduledNotifications_m2CBEBF167CB0D68E9721D7B7CE44046694483B60 + 44586704 (Unity.Notifications.iOS.cpp:5250)
    33.  

    From looking at the source code, it seems like iOSNotificationCenter is trying to create an array of IOSNotifications:


    Code (CSharp):
    1.  
    2.         private static iOSNotification[] NotificationDataToNotifications(iOSNotificationData[] notificationData)
    3.  
    4.         {
    5.  
    6.             var iOSNotifications = new iOSNotification[notificationData == null ? 0 : notificationData.Length];
    7.  
    8.  
    9.             for (int i = 0; i < iOSNotifications.Length; ++i)
    10.  
    11.                 iOSNotifications[i] = new iOSNotification(notificationData[i]);
    12.  
    13.  
    14.             return iOSNotifications;
    15.  
    16.         }
    17.  

    And the iOSNotification constructor is trying to parse some “user data” into a Dictionary<string, string>, which ends up in _ReadNSDictionary :

    Code (CSharp):
    1.  
    2.         internal iOSNotification(iOSNotificationData data)
    3.  
    4.         {
    5.  
    6.             this.data = data;
    7.  
    8.             userInfo = iOSNotificationsWrapper.NSDictionaryToCs(data.userInfo);
    9.  
    10.         }
    11.  
    12.  
    13. void _ReadNSDictionary(void* csDict, void* nsDict, void (*callback)(void* csDcit, const char*, const char*))
    14.  
    15. {
    16.  
    17.     NSDictionary* dict = (__bridge NSDictionary*)nsDict;
    18.  
    19.     [dict enumerateKeysAndObjectsUsingBlock:^(id  _Nonnull key, id  _Nonnull obj, BOOL * _Nonnull stop) {
    20.  
    21.         NSString* k = key;
    22.  
    23.         if ([obj isKindOfClass: [NSString class]])
    24.  
    25.         {
    26.  
    27.             NSString* v = obj;
    28.  
    29.             callback(csDict, k.UTF8String, v.UTF8String);
    30.  
    31.         }
    32.  
    33.         else
    34.  
    35.         {
    36.  
    37.             NSError* error;
    38.  
    39.             NSData* data = [NSJSONSerialization dataWithJSONObject: obj options: NSJSONWritingPrettyPrinted error: &error];
    40.  
    41.             if (data)
    42.  
    43.             {
    44.  
    45.                 NSString* v = [[NSString alloc] initWithData: data encoding: NSUTF8StringEncoding];
    46.  
    47.                 callback(csDict, k.UTF8String, v.UTF8String);
    48.  
    49.             }
    50.  
    51.         }
    52.  
    53.     }];
    54.  
    55. }
    56.  
    And dataWithJSONObject is throwing an exception - possibly because the json is malformed? I’ve done a bit of googling about dataWithJSONObject and standard practice seems to be validating that the text is a valid json object before trying to parse it. Is there a reason the code does not do this? If I add that validation and skip the callback if the json is invalid, would there be any side effects?


    Any help would be appreciated, thanks!
     
    MagicMiikka likes this.
  2. ROBYER1

    ROBYER1

    Joined:
    Oct 9, 2015
    Posts:
    1,444
    Seems I'm stuck with using 1.4.1 or 1.4.0 at least as before then, multiple location trigger notifications didn't work (not sure how that got through Unity QA). Now am even more annoyed this slipped through QA also - here's to being permanent Unity Beta testers when working on Production apps!
     
  3. LemanColt

    LemanColt

    Joined:
    Oct 17, 2014
    Posts:
    15
    We are seeing the same crash on one of our devices now (iphone SE, iOS 14.7.1) using Unity Mobile Notifications version 1.4.2. Did you figure out how to fix it?
     
  4. Eli1234

    Eli1234

    Joined:
    Jul 13, 2017
    Posts:
    24
    I ended up getting more info on this forum thread: https://forum.unity.com/threads/mobile-notification-package.620227/page-12#post-7313515, which pointed me to this github issue. https://github.com/Unity-Technologies/com.unity.mobile.notifications/pull/100

    They said it was a known issue that had been fixed in 1.4.2, though I haven't been able to verify that yet. That's worrying if you're still seeing it. Do you have a concrete repro? We were never able to isolate one, and only identified the issue through automated crash reporting from customers.
     
  5. LemanColt

    LemanColt

    Joined:
    Oct 17, 2014
    Posts:
    15
    Sorry for the late reply. Yes we tried updating to version 1.4.2 of the package yesterday (from 1.4.1) but that didn't help. We are using Unity version 2019.4.28 at the moment, but I would guess Unity version doesnt matter as the problem is in the package.

    We are able to 100% reproduce it on an iphone SE 1 phone which is very helpful that it is consistent, unfortunately we dont have direct access to the phone and need to debug it through test flight. Strange thing is that we have never seen it on any released version of the game, only on the latest version on test flight which contains no change in this area.

    Anyway, we are now trying to see if we can solve it by catching the exception. instead of letting it trigger a crash. For us the call trace tracks back to the CancelAllScheduledNotifications function in the IosNotificationsPlatform class calling the GetScheduledNotifications in iOSNotificationCenter. Unfortunately I cant open the iOSNotificationCenter source
     
    Last edited: Sep 2, 2021
  6. LemanColt

    LemanColt

    Joined:
    Oct 17, 2014
    Posts:
    15
    An update from our side.
    We managed to get rid of the crash by removing the call to iOSNotificationCenter.GetScheduledNotifications() from the CancelAllScheduledNotifications in the IosNotificationsPlatform class.

    I think the IosNotificationsPlatform class if part of the Unity sample project for the Unity Mobile Notification package and not the package it self, but not entirely sure.

    Anyway the getScheduledNotifications call in that function wasnt meaningfull, I think only for debug purposes so for us it was fine to remove it.
    Btw, we also tried putting a try-catch around the function call but seems the GetScheduledNotifications doesnt propagate the exception which would have been good in this case.
     
  7. jeanbaptistelai

    jeanbaptistelai

    Joined:
    Jul 17, 2017
    Posts:
    13
    Hi, I would like to confirm the crash issue is still present with Unity 2019.4.30f1 with Mobile Notification package 1.4.2
    and is caused by calling iOSNotificationCenter.GetScheduledNotifications().

    As LemanColt said, trycatching the exception thrown doesn't work.

    The only solution right now is not use that function.
     
    MagicMiikka likes this.
  8. jrosicka

    jrosicka

    Joined:
    Apr 13, 2017
    Posts:
    1
    We are also still seeing the crash in Mobile Notification package 1.4.2 on Unity 2020.3.18f1. We only see this crash when updating from a version of the app before adding Mobile Notifications to a version of the app that includes that package. It happens when calling iOSNotificationCenter.GetScheduledNotifications().
     
  9. Aurimas-Cernius

    Aurimas-Cernius

    Unity Technologies

    Joined:
    Jul 31, 2013
    Posts:
    3,637
    Do you have Firebase in your project?
    We have a bug report about this crash happening on iOS 15 when both Firebase and Unity Mobile Notifications are used.
     
    MagicMiikka likes this.
  10. MagicMiikka

    MagicMiikka

    Joined:
    Aug 23, 2014
    Posts:
    14
    We use Firebase cloud messaging and we are unable to upgrade due to this issue.
     
  11. Telgemannen

    Telgemannen

    Joined:
    Jun 10, 2015
    Posts:
    12
    Experiencing this issue on unity 2019.4.32f1, mobile notifications 1.4.2. The issue started showing after we upgraded our unity version from 2019.3.7f1 and has so far been 100% reproducible. If a user updates the app, it crashes on start, but if they reinstall, it works. We have several firebase components, but not cloud messaging.
     
  12. MagicMiikka

    MagicMiikka

    Joined:
    Aug 23, 2014
    Posts:
    14