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

如何检测用户启动应用的方式:点击推送通知还是应用图标?

检测应用启动触发方式:点击推送通知 vs 应用图标

iOS 实现方案

针对应用从后台/未启动状态启动的场景:

  • 若用户点击推送通知启动应用,iOS 系统会在启动回调中传递推送相关参数:
    • iOS 13+ 场景生命周期下,在scene(_:willConnectTo:options:)方法的connectionOptions里可获取UIScene.LaunchOptionsKey.remoteNotification
    • iOS 13 及以下,在AppDelegate的didFinishLaunchingWithOptions方法中,launchOptions会包含UIApplicationLaunchOptionsRemoteNotificationKey
  • 若用户点击应用图标启动,上述两个回调中的推送参数均为空。

示例代码:

// iOS 13+ 场景生命周期实现
func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
    if connectionOptions.remoteNotification != nil {
        print("由推送通知启动应用")
    } else {
        print("由应用图标启动应用")
    }
}

// 兼容 iOS 13 以下的 AppDelegate 实现
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
    if launchOptions?[.remoteNotification] != nil {
        print("由推送通知启动应用")
    } else {
        print("由应用图标启动应用")
    }
    return true
}

注意:若应用已处于后台活跃状态,点击推送通知会触发didReceiveRemoteNotification(AppDelegate)或scene(_:didReceiveRemoteNotification:)(SceneDelegate),不属于“启动应用”范畴,需单独区分。

Android 实现方案

针对应用从后台/未启动状态启动的场景:

  • 若用户点击推送通知启动应用,需在推送 payload 中添加自定义标识(比如"launch_source": "from_push"),启动的 Activity 会通过Intent接收到该参数
  • 若用户点击应用图标启动,Intent中不会携带上述自定义推送标识。

实现步骤与示例代码:

  1. 在推送 payload 中加入自定义标识
  2. 在启动页的onCreate和onNewIntent方法中检查参数:
class SplashActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        checkLaunchTrigger(intent)
    }

    override fun onNewIntent(intent: Intent?) {
        super.onNewIntent(intent)
        checkLaunchTrigger(intent)
    }

    private fun checkLaunchTrigger(intent: Intent?) {
        intent?.let {
            val isFromPush = it.getStringExtra("launch_source") == "from_push"
            if (isFromPush) {
                println("由推送通知启动应用")
            } else {
                println("由应用图标启动应用")
            }
        }
    }
}

注意:需为启动 Activity 设置合适的launchMode(比如singleTop),避免因重复创建实例导致参数丢失;应用在后台时点击推送,可能触发onNewIntent而非onCreate,需同时处理两个方法。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.27 21:52:16