iOS应用前台屏蔽推送弹窗:如何定位弹窗来源?
Hey there! Let's figure out where that random alert-style push notification is coming from in your large iOS app. Since you haven't implemented willPresentNotification, we can narrow our focus to third-party SDK behaviors and hidden code hooks. Here's a step-by-step approach:
1. First, Check Third-Party SDK Default Behaviors
Most push-focused SDKs enable foreground alerts by default — let's start with the ones you mentioned:
- OneSignal: This is the most likely culprit. By default, it shows in-app alerts when push notifications arrive while your app is in the foreground. Look for your OneSignal initialization code and ensure you've disabled this setting:
You can also verify the app settings in the OneSignal dashboard to make sure no global foreground alert toggle is enabled.// Objective-C example OSNotificationSettings *settings = [OSNotificationSettings settingsWithInAppAlerts:NO]; [OneSignal initWithLaunchOptions:launchOptions appId:@"YOUR_APP_ID" settings:settings]; - Parse: Parse's push service might also trigger foreground alerts if configured to do so. Search your project for
ParsePushor remote notification registration code — check if there's a setting enabling foreground presentation that you missed. - FBSDK: This is less likely to handle push notifications directly, but it's worth a quick check. Search for any Facebook-related notification code (like
FBNotifications) to rule out edge cases.
2. Use Xcode Symbol Breakpoints to Trace the Alert
This is the fastest way to pinpoint exactly what's triggering the alert:
- Open Xcode and go to the Breakpoint Navigator (cmd + 8).
- Click the
+button at the bottom and select Symbolic Breakpoint. - For iOS versions before 10: Use the symbol
-[UIAlertView show] - For iOS 10+: Use
-[UNNotificationPresentationController presentNotificationWithCompletionHandler:]or-[UIAlertController presentViewController:animated:completion:] - When the alert would normally appear, Xcode will pause execution. Look at the Call Stack panel — this will show you exactly which method (whether it's your code, a third-party SDK, or system code) initiated the alert.
3. Search for Hidden or Forgotten Code
Even if you didn't write willPresentNotification, it might be hiding in a category or extension:
- Do a global search (cmd + shift + f) for
willPresentNotification— check if any third-party SDK has added a category toUNUserNotificationCenterDelegateorUIApplicationDelegatethat implements this method. - Search for terms like
presentNotification,showAlert, orUIAlertView/UIAlertControllerin your project to find any old code you might have forgotten about. - Double-check your
AppDelegateandSceneDelegate(if using scene-based apps) for anydidReceiveRemoteNotificationimplementations that manually create alerts.
4. Disable SDKs Temporarily to Isolate the Issue
If the above steps don't give you clarity, try commenting out the initialization code for OneSignal, Parse, etc., one by one. Test foreground push notifications after each change — if the alert disappears when you disable a specific SDK, that's your culprit. You can then dive into that SDK's documentation to turn off the foreground alert behavior.
内容的提问来源于stack exchange,提问作者Daniel Hjertholm

