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

如何通过键值对为Firebase通知添加跳转应用商店的URL?

在Firebase通知中添加跳转应用商店的自定义键值对并实现点击跳转

一、在Firebase控制台配置自定义键值对

  • 登录Firebase控制台,进入目标项目后打开「Cloud Messaging」模块
  • 创建新推送通知,完成标题、内容等基础设置后,找到「自定义数据」(Custom data)板块
  • 添加自定义键值对:键名建议设为store_url,值填写对应平台的应用商店链接——iOS填App Store应用详情页URL,Android填Google Play应用详情页URL

二、客户端处理点击跳转逻辑

iOS端

在通知回调代理中提取自定义URL并打开:

  1. 实现UNUserNotificationCenterDelegate的userNotificationCenter(_:didReceive:withCompletionHandler:)方法
  2. 从通知内容的userInfo字典中取出store_url字段
  3. 验证URL有效性后,调用系统方法打开链接

示例代码:

func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
    let userInfo = response.notification.request.content.userInfo
    guard let storeURLString = userInfo["store_url"] as? String,
          let targetURL = URL(string: storeURLString) else {
        completionHandler()
        return
    }
    UIApplication.shared.open(targetURL)
    completionHandler()
}

Android端

根据通知状态(前台/后台)处理自定义数据:

  • 前台通知:在FirebaseMessagingService的onMessageReceived方法中提取store_url,直接跳转
  • 后台通知:创建通知时将store_url传入PendingIntent的额外数据,在目标Activity中取出并跳转

前台处理示例代码:

class CustomFirebaseMessagingService : FirebaseMessagingService() {
    override fun onMessageReceived(remoteMessage: RemoteMessage) {
        remoteMessage.data["store_url"]?.let { urlString ->
            val intent = Intent(Intent.ACTION_VIEW).apply {
                data = Uri.parse(urlString)
                flags = Intent.FLAG_ACTIVITY_NEW_TASK
            }
            startActivity(intent)
        }
    }
}

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.20 18:32:07