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

iOS应用处于后台状态时如何获取推送ID

iOS后台接收远程推送时获取专属推送ID的方案

核心方案1:自定义Payload携带专属ID

这是最可控、通用的解决方式,让后端在推送的Payload中添加一个自定义字段(比如custom_push_id),放在aps字典之外。无论应用处于前台还是后台,都能从userInfo中直接提取这个ID:

func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
    // 提取自定义推送ID
    let customPushID = userInfo["custom_push_id"] as? String
    // 执行存储或后续逻辑
    completionHandler(.noData)
}

这种方式不受系统推送回调限制,前后台逻辑统一,还能完全自定义ID规则。

核心方案2:通过用户交互回调获取系统生成的identifier

如果依赖APNs生成的request.identifier,可以利用UNUserNotificationCenterDelegate的didReceiveNotificationResponse方法——当用户点击后台推送消息时,该方法会被触发,这里能直接拿到系统分配的推送ID:

func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
    let systemPushID = response.notification.request.identifier
    // 存储或处理该ID
    completionHandler()
}

⚠️ 注意:这种方式仅适用于用户点击推送消息的场景,静默推送(content-available:1且无用户交互)无法触发该回调,此时仍需用自定义Payload的方式。

关键注意事项

  • 自定义字段必须放在aps字典外部,避免被APNs过滤或解析失败。
  • 静默推送场景下,只有自定义Payload字段是可靠的获取ID方式。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.18 12:43:30