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

如何判断应用由系统托盘通知点击启动(基于Firebase Cloud Messaging)

Firebase Cloud Messaging:判断通知点击启动应用及获取通知信息

Android 端实现

判断是否由通知点击启动应用

当应用未运行或处于后台时,点击FCM通知启动应用,系统会把通知相关数据附加到启动Activity的Intent里。你可以在启动页(通常是MainActivity)的onCreate或onNewIntent方法中,通过以下方式判断:

  • 检查FCM自带的"google.sent_time"字段,只要是FCM通知触发的启动,这个字段都会存在;
  • 也可以在推送时自定义一个标识字段(比如"from_notification_click"),启动时检查该字段是否存在来判断。

获取通知相关信息

直接从启动Activity的Intent中提取数据即可:

// 在MainActivity的onCreate方法中
Intent intent = getIntent();
if (intent != null) {
    // 获取通知标题
    String notificationTitle = intent.getStringExtra("title");
    // 获取通知内容
    String notificationBody = intent.getStringExtra("body");
    // 获取自定义推送数据
    String customParam = intent.getStringExtra("your_custom_key");
    
    // 也可以遍历所有附加数据
    Bundle extras = intent.getExtras();
    if (extras != null) {
        for (String key : extras.keySet()) {
            Object value = extras.get(key);
            // 根据业务需求处理数据
        }
    }
}

注意:如果应用原本就在后台运行,点击通知会触发onNewIntent方法,这时候要先调用setIntent(intent)更新当前Intent,再提取数据,避免拿到旧的Intent内容。

iOS 端实现

判断是否由通知点击启动应用

  • 应用未运行时:点击通知启动应用,AppDelegate的didFinishLaunchingWithOptions方法会收到包含UIApplicationLaunchOptionsRemoteNotificationKey的launchOptions参数,只要这个参数存在,就说明是通知点击启动的;
  • 应用后台运行时:点击通知会触发didReceiveRemoteNotification或UNUserNotificationCenterDelegate的回调方法,通过这个回调的触发时机即可判断。

获取通知相关信息

应用未运行时的处理

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
    if let notificationData = launchOptions?[.remoteNotification] as? [String: Any] {
        // 提取通知标题、内容及自定义数据
        let title = notificationData["title"] as? String
        let body = notificationData["body"] as? String
        let customParam = notificationData["your_custom_key"] as? String
        
        // 处理拿到的通知数据
    }
    return true
}

应用后台运行时的处理(iOS 10+ 推荐统一用UNUserNotificationCenter)

import UserNotifications

// 先在AppDelegate中注册通知中心代理
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
    UNUserNotificationCenter.current().delegate = self
    // 其他初始化逻辑
    return true
}

// 实现UNUserNotificationCenterDelegate的回调
extension AppDelegate: UNUserNotificationCenterDelegate {
    func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
        // 这里的response.notification.request.content.userInfo就是通知的完整数据
        let userInfo = response.notification.request.content.userInfo
        let title = userInfo["title"] as? String
        let body = userInfo["body"] as? String
        let customParam = userInfo["your_custom_key"] as? String
        
        // 处理通知数据
        completionHandler()
    }
}

通用注意事项

  • 推送时要把需要传递的自定义信息放在data字段中,不管Android还是iOS,系统托盘处理通知时,只有data部分的内容会完整传递到应用启动的回调里;Android端的notification字段内容(标题、内容)会被系统解析后附加到Intent,但自定义数据必须在data里。
  • 测试时要覆盖应用的三种状态:完全未运行、后台挂起、前台运行,确保每种状态下的逻辑都正常。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.09 03:31:04