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

点击FCM推送通知无法打开指定Activity及后台onMessageReceived不执行问题

FCM推送问题修复方案:点击通知不跳转+后台不触发onMessageReceived

问题1:应用后台时onMessageReceived不执行

FCM推送消息分两种类型,后台行为差异极大:

  • 纯Notification消息:后台时由系统直接接管展示通知,不会触发onMessageReceived
  • Data消息/混合消息:Data消息无论前后台都会触发onMessageReceived;混合消息(同时包含notification和data字段)后台时系统展示通知,但data数据会通过Intent传递到启动的Activity

修复方案:

  • 推送时改用Data消息格式,或者发送混合消息并兼容两种场景的逻辑
  • 如果必须用纯Notification消息,后台点击通知的行为由Manifest配置控制,但无法在onMessageReceived里处理自定义逻辑

问题2:点击通知无法打开NotificationActivity

你的代码和配置存在几个关键漏洞,逐一修复:

1. 补全通知渠道创建(Android 8.0+强制要求)

Android 8.0及以上系统必须创建通知渠道,否则通知无法正常显示。添加以下方法并在onMessageReceived开头调用:

private void createNotificationChannel() {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        CharSequence channelName = "我的推送通知";
        String channelDesc = "接收FCM推送消息";
        int importance = NotificationManager.IMPORTANCE_HIGH;
        NotificationChannel channel = new NotificationChannel(CHANNEL_ID, channelName, importance);
        channel.setDescription(channelDesc);
        NotificationManager notificationManager = getSystemService(NotificationManager.class);
        notificationManager.createNotificationChannel(channel);
    }
}

2. 完善通知内容配置

你当前的NotificationCompat.Builder缺少标题和内容设置,导致通知显示异常,补充以下代码:

NotificationCompat.Builder builder = new NotificationCompat.Builder(this, CHANNEL_ID)
        .setSmallIcon(R.drawable.ic)
        .setContentTitle(title) // 新增:设置通知标题
        .setContentText(message) // 新增:设置通知内容
        .setPriority(NotificationCompat.PRIORITY_HIGH)
        .setAutoCancel(true);

3. 修正PendingIntent配置(避免栈异常)

简化Intent配置,确保跳转逻辑正常:

Intent intent = new Intent(this, NotificationActivity.class);
intent.putExtra("title", title);
intent.putExtra("message", message);
// 标记为新任务,避免栈混乱
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent,
        PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_IMMUTABLE);

4. 统一通知渠道ID

代码中CHANNEL_ID为"my_notification",但Manifest里的默认渠道指向@string/default_notification_channel_id,必须保持一致:
要么修改代码里的CHANNEL_ID为string资源值,要么把Manifest里的meta-data值改成"my_notification"。

5. 修正Manifest中的点击行为配置

default_notification_click_action必须填写完整的Activity类名,而非相对路径:

<meta-data
    android:name="com.google.firebase.messaging.default_notification_click_action"
    android:value="com.your.package.name.NotificationActivity" />

(替换成你实际的应用包名)


修复后完整代码示例

public class MyFirebaseInstanceIDService extends FirebaseMessagingService {

    private static final String CHANNEL_ID = "my_notification";

    @Override
    public void onNewToken(String s) {
        super.onNewToken(s);
        Log.d("Token:", s);
    }

    @SuppressLint("MissingPermission")
    @Override
    public void onMessageReceived(RemoteMessage remoteMessage) {
        super.onMessageReceived(remoteMessage);

        // 先创建通知渠道
        createNotificationChannel();

        // 处理Notification类型消息
        if (remoteMessage.getNotification() != null) {
            String title = remoteMessage.getNotification().getTitle();
            String message = remoteMessage.getNotification().getBody();
            Log.d("msg:", message);

            // 构建跳转Intent
            Intent intent = new Intent(this, NotificationActivity.class);
            intent.putExtra("title", title);
            intent.putExtra("message", message);
            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
            PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent,
                    PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_IMMUTABLE);

            // 构建并发送通知
            NotificationCompat.Builder builder = new NotificationCompat.Builder(this, CHANNEL_ID)
                    .setSmallIcon(R.drawable.ic)
                    .setContentTitle(title)
                    .setContentText(message)
                    .setPriority(NotificationCompat.PRIORITY_HIGH)
                    .setAutoCancel(true)
                    .setContentIntent(pendingIntent);

            int notificationId = (int) System.currentTimeMillis();
            NotificationManagerCompat notificationManager = NotificationManagerCompat.from(this);
            notificationManager.notify(notificationId, builder.build());
        }

        // 处理Data类型消息(可选)
        if (remoteMessage.getData().size() > 0) {
            String dataTitle = remoteMessage.getData().get("title");
            String dataMessage = remoteMessage.getData().get("message");
            Log.d("data_msg:", dataMessage);
            // 可复用上面的通知逻辑展示Data消息
        }
    }

    private void createNotificationChannel() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            CharSequence channelName = "我的推送通知";
            String channelDesc = "接收FCM推送消息";
            int importance = NotificationManager.IMPORTANCE_HIGH;
            NotificationChannel channel = new NotificationChannel(CHANNEL_ID, channelName, importance);
            channel.setDescription(channelDesc);
            NotificationManager notificationManager = getSystemService(NotificationManager.class);
            notificationManager.createNotificationChannel(channel);
        }
    }
}

额外注意事项

  • 测试后台推送时,确保应用完全处于后台(按Home键退出,不要停留在调试前台)
  • 检查NotificationActivity是否在Manifest中正确注册:
<activity android:name=".NotificationActivity" />
  • Android 13及以上系统,需在Manifest中添加通知权限并在运行时请求:
<uses-permission android:name="android.permission.POST_NOTIFICATIONS" />

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.22 05:37:43