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

如何读取Firebase推送通知内容体并传递至下一视图控制器?

问题描述

我的应用通过Firebase接收推送通知,需要将收到的推送内容存入字符串变量,后续在其他视图控制器中展示该变量。尝试以下两种方法均未成功:

方法一

func userNotificationCenter(
        _ center: UNUserNotificationCenter,
        didReceive response: UNNotificationResponse,
        withCompletionHandler completionHandler: @escaping () -> Void) {
           notificationText = response.notification.request.content.body
}

方法二

let pushContent = UNNotificationContent()
notificationText = pushContent.body

问题分析
  1. 方法二的无效性:直接初始化UNNotificationContent得到的是一个空实例,它的body本身就是空字符串,完全无法获取实际推送的内容。
  2. 方法一的潜在问题:
    • notificationText作用域受限:如果是当前类的局部或实例变量,后续视图控制器无法直接访问该值;
    • 未调用completionHandler():代理方法要求必须调用完成闭包,否则会导致系统异常,也会阻断后续逻辑;
    • 未处理前台接收场景:APP在前台时,推送不会触发didReceive response方法,而是触发willPresent notification方法,未处理的话会漏掉这类推送内容。

正确实现方案

1. 统一管理推送内容(推荐单例模式)

创建单例类存储推送内容,避免作用域问题:

class PushNotificationManager {
    static let shared = PushNotificationManager()
    var latestNotificationBody: String?
    
    private init() {}
}

2. 完善通知代理逻辑

在AppDelegate或SceneDelegate中设置通知代理,并实现完整的推送处理方法:

// 在didFinishLaunchingWithOptions中设置代理
UNUserNotificationCenter.current().delegate = self

// 扩展实现通知代理方法
extension AppDelegate: UNUserNotificationCenterDelegate {
    // 处理前台接收的推送
    func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
        PushNotificationManager.shared.latestNotificationBody = notification.request.content.body
        // 设置前台展示方式,比如弹窗+声音
        completionHandler([.banner, .sound])
    }
    
    // 处理点击推送后的逻辑
    func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
        PushNotificationManager.shared.latestNotificationBody = response.notification.request.content.body
        // 必须调用完成闭包
        completionHandler()
    }
}

3. 在目标视图控制器中获取内容

直接访问单例变量即可拿到推送内容:

class DisplayViewController: UIViewController {
    override func viewDidLoad() {
        super.viewDidLoad()
        if let body = PushNotificationManager.shared.latestNotificationBody {
            // 将内容赋值给控件展示,比如UILabel
            notificationLabel.text = body
        }
    }
}

4. 处理APP未启动时的推送

如果APP完全关闭,点击推送启动APP,需要在启动时获取初始推送内容:

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
    // 解析启动参数中的推送内容
    if let notification = launchOptions?[.remoteNotification] as? [String: Any],
       let aps = notification["aps"] as? [String: Any],
       let body = aps["alert"] as? String {
        PushNotificationManager.shared.latestNotificationBody = body
    }
    return true
}

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.09 14:30:47