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

应用未在后台运行时,如何通过通知打开Activity?Firebase代码求助

我明白你现在遇到的问题——Firebase推送通知在应用关闭或者后台运行时,没办法正常打开NotificationScreen这个Activity对吧?从你给出的代码片段来看,问题大概率出在PendingIntent的配置以及通知渠道的设置上,我来帮你一步步排查修复:

1. 补全并修正PendingIntent的核心配置

你代码里的PendingIntent.getActivity()参数没写完,而且在Android 12及以上版本,必须指定不可变/可变标记,同时要调整Intent的Flags确保应用未运行时能拉起Activity:

private void createNotification(String messageBody, String title, RemoteMessage message) {
    Intent intent = new Intent(this, NotificationScreen.class);
    intent.putExtra("type", message.getData().get("json"));
    // 新增FLAG_ACTIVITY_NEW_TASK,确保应用未启动时也能打开Activity
    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);

    // 根据Android版本选择适配的PendingIntent标记
    int pendingIntentFlags;
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
        pendingIntentFlags = PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_IMMUTABLE;
    } else {
        pendingIntentFlags = PendingIntent.FLAG_UPDATE_CURRENT;
    }

    PendingIntent resultIntent = PendingIntent.getActivity(
            this,
            0, // 如果有多种类型通知,建议用唯一的requestCode区分
            intent,
            pendingIntentFlags
    );

    // 后续通知构建逻辑往下写...
}
2. 必须配置Android 8.0+的通知渠道

Android 8.0及以上系统要求所有通知绑定渠道,而且高优先级渠道才能在后台/应用关闭时触发跳转:

// 建议在Application初始化或者FirebaseMessagingService启动时调用这个方法
private void createNotificationChannel() {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        CharSequence channelName = "推送通知渠道";
        String channelDesc = "处理Firebase推送的通知渠道";
        int importance = NotificationManager.IMPORTANCE_HIGH; // 高优先级才能弹出并触发跳转

        NotificationChannel channel = new NotificationChannel("YOUR_CHANNEL_ID", channelName, importance);
        channel.setDescription(channelDesc);

        NotificationManager notificationManager = getSystemService(NotificationManager.class);
        notificationManager.createNotificationChannel(channel);
    }
}
3. 构建通知时关联PendingIntent和渠道

把刚才配置好的PendingIntent绑定到通知上,同时指定渠道ID:

// 接上面的createNotification方法继续写
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this, "YOUR_CHANNEL_ID")
        .setSmallIcon(R.drawable.ic_notification_icon) // 必须设置小图标,否则通知不显示
        .setContentTitle(title)
        .setContentText(messageBody)
        .setAutoCancel(true)
        .setPriority(NotificationCompat.PRIORITY_HIGH)
        .setContentIntent(resultIntent);

NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0, notificationBuilder.build());
4. 区分Firebase消息类型的特殊处理
  • 如果是通知消息(从Firebase控制台发送的带标题内容的通知):当应用在后台/关闭时,是系统自动展示通知,这时候需要在控制台设置click_action字段,同时在AndroidManifest给NotificationScreen配置对应的Intent过滤器:
<activity android:name=".NotificationScreen">
    <intent-filter>
        <action android:name="com.your.package.name.NOTIFICATION_CLICK" />
        <category android:name="android.intent.category.DEFAULT" />
    </intent-filter>
</activity>

然后在Firebase控制台发送通知时,把click_action设为com.your.package.name.NOTIFICATION_CLICK,系统处理通知时就会直接打开目标Activity。

  • 如果是数据消息(只带自定义数据的消息):不管应用状态如何,都会走到onMessageReceived,用前面的代码逻辑就能正常触发跳转。

最后再检查两个关键点:

  • 确认NotificationScreen在AndroidManifest里正常注册,没有设置android:launchMode="singleInstance"这类可能阻碍跳转的启动模式;
  • 测试时不要用“强制停止”关闭应用,部分设备强制停止后会拦截推送,用返回键正常退出应用测试更准确。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.25 02:23:03