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

Firebase推送通知后台点击跳转Main2Activity问题求助

Fixing Firebase Notification Redirect to Main2Activity in Background

Hey there! I’ve dealt with this exact headache before—getting Firebase notifications to open the right activity when the app’s in the background can be tricky because foreground and background handling work differently. Let me break down the fix for you:

1. Understand the Core Issue

When your app is in the foreground, your FirebaseMessagingService's onMessageReceived() method handles the notification, so you can manually set the PendingIntent to open Main2Activity. But when the app is in the background, Android’s system tray handles showing the notification directly—so you need to tell the system exactly which activity to launch when the notification is tapped.

2. Update Your Notification Payload

You need to add a click_action field to your notification payload. This tells Android which action to trigger when the notification is clicked.

If using Firebase Console:

  • Go to the Advanced options section when composing your notification.
  • Under Custom data, add a new key-value pair:
    • Key: click_action
    • Value: OPEN_MAIN2_ACTIVITY (use all caps to follow Android intent action conventions)

If using Firebase Cloud Messaging API:

Include the click_action in the notification object of your JSON payload:

{
  "notification": {
    "title": "New Update",
    "body": "Check out Main2Activity!",
    "click_action": "OPEN_MAIN2_ACTIVITY"
  },
  "to": "YOUR_DEVICE_TOKEN"
}

3. Configure AndroidManifest.xml

Next, you need to register the click_action with your Main2Activity so Android knows which activity to launch when that action is triggered. Add an <intent-filter> inside your Main2Activity’s tag:

<activity
    android:name=".Main2Activity"
    android:exported="true"> <!-- Required for Android 12+; adjust based on your target SDK -->
    <intent-filter>
        <action android:name="OPEN_MAIN2_ACTIVITY" /> <!-- Matches the click_action value -->
        <category android:name="android.intent.category.DEFAULT" /> <!-- Required for system notifications -->
    </intent-filter>
</activity>

4. (Optional) Handle Data in Main2Activity

If your notification includes extra data that you need to pass to Main2Activity, override onNewIntent() in Main2Activity to retrieve it (especially if your activity uses launchMode="singleTop" or similar):

@Override
protected void onNewIntent(Intent intent) {
    super.onNewIntent(intent);
    // Retrieve data from the intent
    String data = intent.getStringExtra("your_data_key");
    // Do something with the data
}

Quick Notes

  • If you prefer to handle all notifications (foreground and background) in onMessageReceived(), use data-only payloads instead of notification payloads. This way, you can build and show the notification manually with a PendingIntent pointing to Main2Activity every time.
  • Double-check that the click_action value matches exactly between your payload and the AndroidManifest (case-sensitive!).

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.20 07:00:09