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

Android Firebase通知点击跳转Activity及多通知显示与数据加载异常

看起来你遇到了Firebase推送通知里两个非常典型的问题,我来帮你一步步拆解解决:

问题1:通知栏仅显示最新一条通知

核心原因

你发送的每条通知使用了相同的通知ID,Android系统会自动用新通知覆盖掉同ID的旧通知,导致通知栏只能看到最新的那条。

解决方案:为每条通知分配唯一ID

你需要给每个推送生成一个独一无二的通知ID,推荐两种方式:

  1. 让后端服务器在推送时附带一个全局唯一的notification_id字段(比如数据库自增ID、UUID)
  2. 客户端本地用时间戳生成临时唯一ID(适合后端没提供的情况)

示例代码(在FirebaseMessagingService的onMessageReceived方法中):

@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
    Map<String, String> notificationData = remoteMessage.getData();
    String title = notificationData.get("title");
    String content = notificationData.get("content");

    // 生成唯一通知ID
    int uniqueNotifyId;
    if (notificationData.containsKey("notification_id")) {
        // 优先用后端提供的唯一ID
        uniqueNotifyId = Integer.parseInt(notificationData.get("notification_id"));
    } else {
        // 后端没提供时,用当前时间戳生成
        uniqueNotifyId = (int) System.currentTimeMillis();
    }

    // 构建通知
    NotificationCompat.Builder builder = new NotificationCompat.Builder(this, "你的通知渠道ID")
            .setSmallIcon(R.drawable.ic_notification)
            .setContentTitle(title)
            .setContentText(content)
            .setPriority(NotificationCompat.PRIORITY_HIGH);

    // 发送通知时传入唯一ID
    NotificationManagerCompat.from(this).notify(uniqueNotifyId, builder.build());
}
问题2:点击任意通知都加载最早的数据

核心原因

通常是两个问题导致的:

  1. 打开Activity的PendingIntent被系统复用,旧的Intent数据没有被更新
  2. 你把通知数据存在了全局变量中,新通知覆盖了旧值,但点击时仍读取全局变量的旧数据

解决方案:确保Intent数据唯一且被正确更新

步骤1:构建带唯一标识的PendingIntent

在创建通知的PendingIntent时,要设置正确的Flag和唯一的requestCode,避免系统复用旧Intent:

// 创建指向目标Activity的Intent,传入当前通知的完整数据
Intent intent = new Intent(this, YourTargetActivity.class);
intent.putExtras(new Bundle(notificationData));
// 设置Flag:确保Activity实例正确更新数据
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);

// 用之前的唯一通知ID作为requestCode,保证每个PendingIntent唯一
int requestCode = uniqueNotifyId;
// 使用FLAG_UPDATE_CURRENT更新Intent数据,FLAG_IMMUTABLE适配Android 12+
PendingIntent pendingIntent = PendingIntent.getActivity(
        this,
        requestCode,
        intent,
        PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_IMMUTABLE
);

// 将PendingIntent绑定到通知
builder.setContentIntent(pendingIntent);

步骤2:在Activity中处理新Intent数据

当Activity已经处于前台时,系统会调用onNewIntent而不是onCreate,所以需要在这个方法中更新数据:

public class YourTargetActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_target);
        // 处理初始启动的通知数据
        processNotificationData(getIntent());
    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        // 更新当前Activity的Intent,确保后续getIntent()拿到最新数据
        setIntent(intent);
        // 处理新点击的通知数据
        processNotificationData(intent);
    }

    private void processNotificationData(Intent intent) {
        if (intent == null || intent.getExtras() == null) return;
        // 获取当前点击的通知的专属数据
        String title = intent.getStringExtra("title");
        String content = intent.getStringExtra("content");
        // 在这里更新UI展示对应数据
        tvNotificationTitle.setText(title);
        tvNotificationContent.setText(content);
    }
}

额外注意点

  • 尽量使用**数据消息(Data Message)**而非通知消息(Notification Message),这样所有通知的处理逻辑都在onMessageReceived中,更可控(通知消息在App后台时会由系统直接展示,数据传递可能有差异)
  • 确保你已经为Android 8.0及以上版本创建了正确的通知渠道,否则通知可能无法正常显示

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.19 09:57:03