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

Firebase通知点击无法打开应用,onMessageReceived未触发求助

Firebase onMessageReceived 未触发及通知点击无响应排查方案

你的现有配置与代码

Manifest 配置

<service android:name=".Notifications"
         android:exported="true">
     <intent-filter>
        <action android:name="com.google.firebase.MESSAGING_EVENT" />
    </intent-filter>
</service>

onMessageReceived 实现

@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
    Log.d("NotificationService", "Notification received!");
    Map<String, String> data = remoteMessage.getData();
    String deep_link = data.get("deep_link");
    String title = data.get("title");
    String body = data.get("body");
    Log.d("NotificationService", "Notification received!");
    NotificationCompat.Builder builder = new NotificationCompat.Builder(MainActivity.context.getApplicationContext(), "1");
    builder.setSmallIcon(android.R.drawable.ic_dialog_alert);
    Intent intent = new Intent(new MainActivity(), Uri.parse(deep_link));
    PendingIntent pendingIntent = PendingIntent.getActivity(MainActivity.context, 0, intent, PendingIntent.FLAG_IMMUTABLE);
    builder.setContentIntent(pendingIntent);
    builder.setLargeIcon(BitmapFactory.decodeResource(MainActivity.context.getResources(), R.mipmap.ic_launcher));
    builder.setContentTitle(title);
    builder.setContentText(body);

    NotificationManager notificationManager = (NotificationManager) MainActivity.context.getSystemService(MainActivity.context.NOTIFICATION_SERVICE);
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O)
    {
        String channelId = "1";
        NotificationChannel channel = new NotificationChannel(
                channelId,
                "1",
                NotificationManager.IMPORTANCE_HIGH);
        notificationManager.createNotificationChannel(channel);
        builder.setChannelId(channelId);
    }
    // Will display the notification in the notification bar
    notificationManager.notify(1, builder.build());
}

排查与修复步骤

1. 确认服务类继承关系

你的Notifications类必须继承FirebaseMessagingService,否则Manifest配置的服务不会被Firebase识别,自然不会触发onMessageReceived:

public class Notifications extends FirebaseMessagingService {
    // 你的onMessageReceived实现
}

2. 修正Intent创建逻辑(解决通知点击无响应)

你当前的Intent写法错误,正确的方式是先指定目标Activity,再设置DeepLink数据:

// 替换原Intent创建代码
Intent intent = new Intent(getApplicationContext(), MainActivity.class);
intent.setData(Uri.parse(deep_link));
// 添加FLAG确保后台能正常启动应用
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);

PendingIntent pendingIntent = PendingIntent.getActivity(
        getApplicationContext(), 
        0, 
        intent, 
        PendingIntent.FLAG_IMMUTABLE | PendingIntent.FLAG_UPDATE_CURRENT
);

同时要在MainActivity的Manifest配置中添加DeepLink对应的intent-filter,确保系统能解析DeepLink:

<activity android:name=".MainActivity">
    <intent-filter>
        <action android:name="android.intent.action.MAIN" />
        <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>
    <!-- 添加DeepLink过滤器 -->
    <intent-filter>
        <action android:name="android.intent.action.VIEW" />
        <category android:name="android.intent.category.DEFAULT" />
        <category android:name="android.intent.category.BROWSABLE" />
        <!-- 替换成你的DeepLink Scheme和Host -->
        <data android:scheme="your_scheme" android:host="your_host" />
    </intent-filter>
</activity>

3. 区分Firebase消息类型(解决onMessageReceived未触发)

Firebase消息分两种类型,触发逻辑不同:

  • 通知消息(包含notification字段):应用前台时会调用onMessageReceived;后台/杀死状态下由系统直接展示通知,不会进入你的服务。
  • 数据消息(仅包含data字段):无论应用处于前台/后台/杀死状态,都会调用onMessageReceived。
  • 组合消息(同时包含notification和data):后台状态下系统展示通知,点击时将data传递到启动的Activity;前台时会调用onMessageReceived。

如果需要后台也完全自定义通知逻辑,建议只发送数据消息,由你的服务自行创建通知。

4. 替换全局Context引用

避免依赖MainActivity.context这种静态Context(可能为空导致崩溃或无响应),直接使用Service的getApplicationContext():

// 替换所有MainActivity.context的引用
NotificationCompat.Builder builder = new NotificationCompat.Builder(getApplicationContext(), "1");
NotificationManager notificationManager = (NotificationManager) getApplicationContext().getSystemService(Context.NOTIFICATION_SERVICE);

5. 检查依赖与配置完整性

  • 确保app级build.gradle中添加了Firebase Messaging依赖:
    // 推荐使用Firebase BoM统一管理版本
    implementation platform('com.google.firebase:firebase-bom:32.7.0')
    implementation 'com.google.firebase:firebase-messaging'
    
  • 确认google-services.json已正确放置在app根目录,且文件中的包名与项目一致。

6. 查看日志排查错误

打开Logcat,过滤NotificationService或FirebaseMessaging关键词,查看是否有服务注册失败、初始化错误或运行时异常的日志,这些能直接定位问题。


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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.02 09:20:22