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

如何在iOS通知内容扩展的Storyboard添加按钮并处理点击不关闭通知

Alright, let's get this sorted. Adding a button to your iOS Notification Content Extension that triggers an action without dismissing the notification banner is totally doable—here's a step-by-step guide with all the key details:

Implement iOS Notification Content Extension with Button (No Dismiss on Tap)

1. Set Up Your Notification Content Extension

First, make sure you have an extension ready to go:

  • Open your Xcode project → Click the project file → Hit the "+" button to add a new target
  • Select "Notification Content Extension" from the list, name it, and finish the setup.

2. Add Button to MainInterface.storyboard

Head to MainInterface.storyboard for your extension:

  • Drag a UIButton from the component library onto the notification view
  • Style it with a title, font, and colors that match your app's design
  • Add Auto Layout constraints to ensure it displays correctly across all devices (e.g., center alignment, left/right padding)

3. Connect Button to Code

Open NotificationViewController.swift and link your button:

  • Add an IBOutlet for the button inside the class:
    @IBOutlet weak var actionButton: UIButton!
    
  • Go back to the storyboard, right-click the button, and drag to connect it to this outlet in your code.

4. Handle Button Tap & Prevent Notification Dismissal

The critical part is making sure the notification stays open when the button is tapped. Add this code to your view controller:

override func viewDidLoad() {
    super.viewDidLoad()
    // Hook up the button's tap event
    actionButton.addTarget(self, action: #selector(handleButtonTap), for: .touchUpInside)
    // Prevent the tap event from bubbling up to the parent view (which would auto-dismiss the notification)
    actionButton.isExclusiveTouch = true
}

@objc private func handleButtonTap(_ sender: UIButton) {
    // Add your custom action logic here—log events, update shared data, etc.
    print("Button tapped! Executing custom action...")
    
    // If you need to communicate with your main app, use App Groups to share data
    if let sharedDefaults = UserDefaults(suiteName: "group.com.yourcompany.yourapp") {
        sharedDefaults.set("notification_button_tapped", forKey: "extension_action_triggered")
        sharedDefaults.synchronize()
    }
    
    // IMPORTANT: Don't call `extensionContext?.dismissNotificationContentExtension()`—this is what closes the notification
    // For short, time-sensitive tasks, use a background task to keep the extension alive
    var bgTask: UIBackgroundTaskIdentifier = .invalid
    bgTask = UIApplication.shared.beginBackgroundTask(withName: "NotificationActionTask") {
        UIApplication.shared.endBackgroundTask(bgTask)
        bgTask = .invalid
    }
    
    // Run your quick task here—keep it brief, extensions have strict runtime limits
    // ...
    
    // Clean up the background task once done
    if bgTask != .invalid {
        UIApplication.shared.endBackgroundTask(bgTask)
        bgTask = .invalid
    }
}

5. Key Notes to Avoid Issues

  • App Groups Configuration: If you're sharing data between the extension and main app, enable App Groups in both targets (under Signing & Capabilities) and use the same group ID for both.
  • Keep Actions Concise: Notification extensions have limited runtime—avoid long-running tasks here to prevent the system from terminating the extension early.
  • Notification Category Registration: Make sure your main app registers a UNNotificationCategory for notifications using this extension; otherwise, your custom UI won't appear.

内容的提问来源于stack exchange,提问作者Abdullah Al-Hallak

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.29 07:50:06