You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

iOS锁屏状态下发UILocalNotification失败,求排查建议

Fixing Local Notification Not Showing on iOS Lock Screen

Hey there! Let's break down why your local notification isn't popping up on the lock screen—you're missing a few key steps, especially since the API you're using is outdated. Here's what you need to address:

1. You're Using a Deprecated API

UILocalNotification was deprecated starting in iOS 10. Apple now recommends using the UserNotifications framework (UNUserNotificationCenter) for all local and remote notification tasks. The old API can have unexpected behavior on newer iOS versions, including failing to display on the lock screen.

2. Missing Notification Permission Request

Even if you set up the notification correctly, you must explicitly ask the user for permission to display notifications—including allowing them to show on the lock screen. Without this permission, notifications will never appear, regardless of your code.

3. Incorrect Code Order in Your Current Implementation

In your code, you're setting soundName after calling scheduleLocalNotification(_:). This means the sound setting won't be applied to the scheduled notification. While this isn't the direct cause of the lock screen issue, it's a bug worth fixing.


Updated Working Code Using the Modern API

First, import the required framework at the top of your file:

import UserNotifications

Then, request permissions and schedule the notification properly:

// Step 1: Request notification permissions from the user
UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .sound, .badge]) { granted, error in
    if granted {
        // Step 2: Configure notification content
        let content = UNMutableNotificationContent()
        content.title = "Alert"
        content.body = "Phone Found..!!"
        content.sound = UNNotificationSound.default
        content.userInfo = ["action": "Go back to App"] // Store your action-related info here

        // Step 3: Set the trigger (1 second from now)
        let trigger = UNTimeIntervalNotificationTrigger(timeInterval: 1, repeats: false)

        // Step 4: Create and schedule the notification request
        let request = UNNotificationRequest(identifier: "PhoneFoundNotification", content: content, trigger: trigger)
        UNUserNotificationCenter.current().add(request) { error in
            if let error = error {
                print("Error scheduling notification: \(error.localizedDescription)")
            }
        }
    } else {
        print("Notification permission denied by user")
    }
}

Additional Checks to Ensure Lock Screen Display

  • Verify Device Settings: On your iOS device, go to Settings > Notifications > [Your App] and confirm:
    • "Allow Notifications" is toggled on
    • "Lock Screen" is enabled under the "Alerts" section
  • Background Mode (If Needed): If you're scheduling notifications from the background, ensure you've enabled the appropriate background modes in your Xcode project settings (though this isn't required for a simple time-interval trigger like yours).

内容的提问来源于stack exchange,提问作者ShitalM

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.22 09:36:53