问题
Call UNUserNotificationCenter getPendingNotificationRequests crashes on iPhone SE using iOS 10.1.1.
This seems like an edge case - I have confirmed this works on an iPhone 5s with the same iOS version - 10.1.1. It crashed iOS on that iPhone 5s too.
The "funny" thing is not only does the app crash, it seems like springboard also crashes - we are not sent to home screen, but to lock screen (after seeing the spinning animation for a few seconds). It seems like something inside of iOS - on this iPhone SE - is broken... it is possible that we did something wrong in our app, but it seems to boil down to the exact call to UNUserNotificationCenter.current().getPendingNotificationRequests. I guess no one ran into this? As I said, we've only seen this on this one iPhone SE device so far, and we have used probably 10s or 20s of other devices with no problem.
Also, this is normally printed in the debug console before springboard crashes:
[Common] Terminating since there is no system app.
I'll continue looking for the culprit... in the end I might try updating said device to latest iOS (10.3.1 atm) and see if things work better.
Update 1: It seems to be a bug in iOS, between 10.0 and 10.1, and might be fixed in iOS 10.2. I can't just update this iPhone SE just yet, I guess I might have to workaround the problem...
Update 2: Making a new simple app that basically just calls UNUserNotificationCenter.current().getPendingNotificationRequests and running it on the iPhone SE renders NO crash... the plot thickens... There might be some circumstance where I am using UNUserNotificationCenter in my real app which makes it crash.
Update 3: For some reason, it seems to be related to the current app bundle identifier. If I change it for our current app, the problem seems to go away. This is very weird. I have yet to confirm if this really works for the current app in its full state - I have currently removed almost all other code, just testing this notification line. It crashes even with that, using our default bundle identifier. Update-within-update: Confirmed that the full-blown app WORKS if I just change the bundle identifier. STRANGE. But even if it works right now, there is nothing saying that it will start crashing later on. There should be no problem with our first bundle identifier. It's just a string, after all. This is very very likely a bug within iOS (which I knew before, but just sayin). I have yet to find a workaround. Current policy is to support iOS 9... and with that, this app will most likely have a few percent crashes for iOS 10.0~10.1 users.
Update 4: After some more testing it turned out that the iPhone 5s on iOS 10.1.1 also crashes the app and springboard in the exact same way. Not sure what happened earlier, someone else was testing on this device...
Update 5: UNUserNotificationCenter.current().add() crashes too. I tried adding a notification before looking if any existed, but the add function also crashes iOS. It was a good idea.
回答1:
After a lot of testing I found a piece of code that seem to magically fix everything. Without further ado
UNUserNotificationCenter.current.removeAllPendingNotificationRequests()
Call this at first launch of app. That's it. It will need more testing however, but seems to work for now.
来源:https://stackoverflow.com/questions/43840090/calling-unusernotificationcenter-current-getpendingnotificationrequests-crashe