iOS10 UserNotifications框架:前台无法显示本地通知求助
Hey, I’ve dealt with this exact problem before—let’s get your notifications showing up in the foreground and triggering your target method properly.
The Core Issue
When your app is in the foreground, the willPresentNotification: method gets called, but the system won’t automatically display the alert unless you explicitly tell it to via the completion handler. That’s probably why you’re seeing the method fire but no notification view.
Step 1: Make Notifications Show in Foreground
Update your willPresentNotification:withCompletionHandler: implementation to pass the presentation options you want (alert, sound, badge) to the completion handler. Here’s how:
- (void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions options))completionHandler { // Choose which presentation options to enable UNNotificationPresentationOptions options = UNNotificationPresentationOptionAlert | UNNotificationPresentationOptionSound; completionHandler(options); }
Key note: Don’t forget to call the completion handler—this is what tells the system to actually display the notification UI in the foreground.
Step 2: Handle Notification Taps (Foreground & Background)
To make the notification trigger your specified method when tapped, use the didReceiveNotificationResponse:withCompletionHandler: delegate method. This gets called regardless of whether your app was in the foreground or background when the notification was tapped.
- (void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void (^)(void))completionHandler { // Extract the notification's user info (you can use this to pass custom data) NSDictionary *notificationData = response.notification.request.content.userInfo; // Call your target method here—use the user info to differentiate actions if needed [self yourTargetMethodWithData:notificationData]; // Always call the completion handler to let the system know you're done processing completionHandler(); }
Quick Pre-Checks
- Double-check notification permissions: Make sure you’ve requested the right permissions (alert, sound, badge) when your app launches. Add this to your
application:didFinishLaunchingWithOptions::UNUserNotificationCenter *notificationCenter = [UNUserNotificationCenter currentNotificationCenter]; notificationCenter.delegate = self; // Important—don't forget to set the delegate! [notificationCenter requestAuthorizationWithOptions:(UNAuthorizationOptionAlert | UNAuthorizationOptionSound | UNAuthorizationOptionBadge) completionHandler:^(BOOL granted, NSError * _Nullable error) { if (granted) { NSLog(@"Notification permissions granted"); } else { NSLog(@"Notification permissions denied: %@", error.localizedDescription); } }]; - iOS Version Compatibility: The UserNotifications framework is iOS 10+, so if you’re supporting older versions, you’ll need to add fallback logic with
UIUserNotificationSettings, but since you’re already using UNUserNotifications, this is likely covered.
Give these changes a shot—your notifications should now pop up in both foreground and background, and tapping them will trigger your desired method. If you run into any weird edge cases, feel free to follow up!
内容的提问来源于stack exchange,提问作者surToTheW

