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

Android中FCM两类通知跳转不同页面及双向发送异常问题

Hey there! Let's dig into this FCM notification issue you're facing. Since notifications from A to B work perfectly (delivered and opens the right Activity), but B to A gets delivered but throws an error, here are the key areas to check and fix:

Key Troubleshooting Steps & Fixes

1. Verify Intent Structure & PendingIntent Configuration

First, make sure the Intent extra data (like the identifier for which Activity to open) is being sent correctly from Cloud Functions when B sends to A. A tiny typo in the extra key or mismatched value can break the intent parsing in your app.

  • Double-check the data payload in your Cloud Functions code for B-to-A notifications. For example, if you use a key like target_activity, ensure it matches exactly what your Android app expects:
    // Cloud Functions snippet for B-to-A notifications
    const payload = {
      notification: {
        title: "New Message from B",
        body: "Check out B's message"
      },
      data: {
        target_activity: "com.yourapp.activities.ActivityA", // Match your actual Activity class path
        // Add other necessary data fields here
      }
    };
    
  • On the Android side, confirm your FirebaseMessagingService correctly extracts this data and builds the right PendingIntent. A common mistake is missing flags or incorrect component naming:
    // Inside your FirebaseMessagingService's onMessageReceived method
    val targetActivity = remoteMessage.data["target_activity"]
    val intent = Intent(this, Class.forName(targetActivity ?: ""))
    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP or Intent.FLAG_ACTIVITY_NEW_TASK)
    // Use FLAG_IMMUTABLE for Android 12+ to avoid security exceptions
    val pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_IMMUTABLE)
    

2. Check Notification Channel Compatibility

If your app targets Android 8.0+, ensure the notification channel used for B-to-A notifications is properly created and configured. A missing or misconfigured channel can cause unexpected behavior after the notification is received.

  • Verify you're creating the channel in your app's Application class or MainActivity on startup:
    val channelId = "b_to_a_notifications"
    val channelName = "Notifications from B"
    val channel = NotificationChannel(channelId, channelName, NotificationManager.IMPORTANCE_HIGH)
    val notificationManager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
    notificationManager.createNotificationChannel(channel)
    
  • Make sure the channel ID in your Cloud Functions payload matches the one created in the app:
    const payload = {
      notification: {
        title: "...",
        body: "...",
        channel_id: "b_to_a_notifications" // Match this to your Android channel ID
      },
      // ... data payload
    };
    

3. Debug the Exception Details

Since you mentioned an exception occurs but didn't specify the details, here's how to get clear error logs:

  • Use Android Studio's Logcat to filter for tags like FirebaseMessaging, Notification, or your app's package name. Look for stack traces that appear when the B-to-A notification is tapped.
  • Add try-catch blocks in your onMessageReceived method and Activity intent handling code to catch and log errors:
    try {
      // Code to parse intent data and launch the target Activity
    } catch (e: Exception) {
      Log.e("FCM_ERROR", "Error handling notification: ${e.message}", e)
    }
    

4. Confirm User Token Validity

Even though the notification is delivered, double-check that user A's FCM token is up-to-date. Tokens can change if the user reinstalls the app, clears app data, or updates their OS. In Cloud Functions, ensure you're using the latest token for A when sending notifications from B.

5. Test with a Minimal Payload

Simplify the B-to-A notification payload to only essential fields (title, body, target activity data) and see if the issue persists. Sometimes extra unnecessary data in the payload can cause parsing errors.


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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.21 04:21:10