Unity-Technologies / com.unity.mobile.notifications

Mobile Notifications Package

Home Page:https://docs.unity3d.com/Packages/com.unity.mobile.notifications@2.1/manual/index.html

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

iOS 15 app crashes on getPendingNotificationRequestsWithCompletionHandler

yanmasharski opened this issue · comments

Hi, Unity Team!

We detected app crash because of calling getPendingNotificationRequestsWithCompletionHandler method.
We made fast fix because we can't stop development process, but this issues should be critical for all users.
Hope, you will fix this soon.

Our fix:

- (void)updateScheduledNotificationList
{
    UNUserNotificationCenter* center = [UNUserNotificationCenter currentNotificationCenter];
    static dispatch_once_t onceToken_2 = 2;
    dispatch_once(&onceToken_2, ^{
        [center getPendingNotificationRequestsWithCompletionHandler:^(NSArray<UNNotificationRequest *> * _Nonnull requests) {
            self.cachedPendingNotificationRequests = requests;
        }];
    }];
}

Thanks to the above comment we also managed to fix the issue. However, we chose to use dispatch_async rather than once so subsequent calls to the function will also work:

- (void)updateScheduledNotificationList
{
    UNUserNotificationCenter* center = [UNUserNotificationCenter currentNotificationCenter];
    dispatch_async(dispatch_get_main_queue(), ^{
        [center getPendingNotificationRequestsWithCompletionHandler:^(NSArray<UNNotificationRequest *> * _Nonnull requests) {
                self.cachedPendingNotificationRequests = requests;
        }];
    });
}

We had the same issue and the posted fix by mpdggdev fixed it for us too.

Little bit improved solution

- (void)updateScheduledNotificationList
{
    UNUserNotificationCenter* center = [UNUserNotificationCenter currentNotificationCenter];
    if (@available(iOS 15, *)) {
        dispatch_async(dispatch_get_main_queue(), ^{
            [center getPendingNotificationRequestsWithCompletionHandler:^(NSArray<UNNotificationRequest *> * _Nonnull requests) {
                    self.cachedPendingNotificationRequests = requests;
            }];
        });
    } else {
        [center getPendingNotificationRequestsWithCompletionHandler:^(NSArray<UNNotificationRequest *> * _Nonnull requests) {
                self.cachedPendingNotificationRequests = requests;
        }];
    }
        
}

Under what conditions does this occur?
I cannot reproduce this issue using latest notifications package.

We found that the issue occurs when either Firebase or Pangle (or both) are present in a project.

commented

Is there any progress?
I'm worried because I need to remember to fix it every time I build, and if I forget, the app won't start.

Thank you,

@celp could you submit a bug via Unity Editor with the project in which it reproduces?
Link this issue in the bug report.

commented

@aurimasc
OK, I will submit it.

commented

@aurimasc
I submitted a bug report from the Unity Editor with a project to reproduce.
Please check it.
Case: 1375744

commented

I found that the problem occurs with the combination of Xcode 13 and iOS 15.

commented

Full code solution. Add that script to any Editor folder, should be fix the problem

public class ApplePostProcess : MonoBehaviour
{
#if UNITY_IOS
    [PostProcessBuild(1)]
    public static void OnPostProcessBuild(BuildTarget buildTarget, string pathToXcode)
    {
        if (buildTarget == BuildTarget.iOS)
        {
            FixediOS13NotificationBug(pathToXcode);
        }
    }
    private static void FixediOS13NotificationBug(string pathToXcode)
    {
        string managerPath = pathToXcode + "/Libraries/com.unity.mobile.notifications/Runtime/iOS/Plugins/UnityNotificationManager.m";
        var text = File.ReadAllLines(managerPath).ToList();
        for (int i = 0; i < text.Count; i++)
        {
            if (text[i] == @"- (void)updateScheduledNotificationList")
            {
                text.RemoveRange(i, 7);
                text.Insert(i, @"- (void)updateScheduledNotificationList
{
    UNUserNotificationCenter* center = [UNUserNotificationCenter currentNotificationCenter];
    if (@available(iOS 15, *))
    {
        dispatch_async(dispatch_get_main_queue(), ^{
            [center getPendingNotificationRequestsWithCompletionHandler:^(NSArray < UNNotificationRequest *> *_Nonnull requests) {
                self.cachedPendingNotificationRequests = requests;
            }];
        });
    }
    else
    {
        [center getPendingNotificationRequestsWithCompletionHandler:^(NSArray < UNNotificationRequest *> *_Nonnull requests) {
            self.cachedPendingNotificationRequests = requests;
        }];
    }

}");
                break;
            }
        }
        
        File.WriteAllLines(managerPath, text.ToArray());
    }
#endif
}
commented

Hi All,
I just received a message from Unity that the issue has been added to the Issue Tracker.
I'm waiting for the fix.

https://issuetracker.unity3d.com/issues/ios-app-freezes-slash-crashes-when-both-mobile-notifications-and-firebase-are-used

Should be fixed by this: #130