Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

iOS 15 app crashes on getPendingNotificationRequestsWithCompletionHandler #121

Closed
yanmasharski opened this issue Sep 29, 2021 · 13 comments
Closed

Comments

@yanmasharski
Copy link

yanmasharski commented Sep 29, 2021

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;
        }];
    }];
}
@mpeggdev
Copy link

mpeggdev commented Oct 7, 2021

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

@ddalacu
Copy link

ddalacu commented Oct 8, 2021

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

@yanmasharski
Copy link
Author

yanmasharski commented Oct 11, 2021

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;
        }];
    }
        
}

@aurimasc
Copy link
Collaborator

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

@mpeggdev
Copy link

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

@celp
Copy link

celp commented Oct 24, 2021

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,

@aurimasc
Copy link
Collaborator

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

@celp
Copy link

celp commented Oct 25, 2021

@aurimasc
OK, I will submit it.

@celp
Copy link

celp commented Oct 25, 2021

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

@celp
Copy link

celp commented Oct 27, 2021

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

@neHCuoHEpa
Copy link

neHCuoHEpa commented Nov 1, 2021

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
}

@celp
Copy link

celp commented Nov 4, 2021

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

@aurimasc
Copy link
Collaborator

Should be fixed by this: #130

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants