Android Studio中FCM推送通知无显示问题求助
FCM通知无法接收的排查与修复方案
调用sendToToken传入有效FCM Token但目标设备收不到通知,以下是代码中的核心遗漏点和修复方案:
1. 消息服务类注册不匹配
AndroidManifest里注册的服务是.support.MyFirebaseMessagingService2,但实际实现的消息处理类是com.example.intercambiolibros.fragments.MyFirebaseMessagingService,类名、包路径完全不匹配,导致FCM消息无法被正确接收处理。
修复:
修改AndroidManifest.xml中的服务声明,匹配实际类的路径和名称:
<service android:name=".fragments.MyFirebaseMessagingService" android:exported="false"> <intent-filter> <action android:name="com.google.firebase.MESSAGING_EVENT" /> </intent-filter> </service>
(注:exported设为false即可,无需开放给外部应用调用)
2. 消息发送与接收逻辑不匹配
问题点:
sendToToken使用了已废弃的RemoteMessage.Builder(token + "@gcm.googleapis.com")构造方式,不符合当前FCM API规范;- 发送的是数据消息(通过
addData传入内容),但onMessageReceived里只尝试读取message.getNotification()(仅适用于通知消息),完全忽略数据消息的内容。
修复:
更新sendToToken方法为FCM推荐写法:
public void sendToToken(String token) { Message message = Message.builder() .setToken(token) .putData("title", "测试通知") .putData("body", "这是一条FCM测试消息") .build(); FirebaseMessaging.getInstance().send(message) .addOnCompleteListener(task -> { if (task.isSuccessful()) { Log.d("FCM_SEND", "消息发送成功"); } else { Log.e("FCM_SEND", "消息发送失败", task.getException()); } }); }
修改MyFirebaseMessagingService的onMessageReceived方法,兼容两种消息类型:
@Override public void onMessageReceived(RemoteMessage message) { super.onMessageReceived(message); // 区分处理通知消息和数据消息 String title; String body; if (message.getNotification() != null) { // 从控制台或带notification字段的请求发送的通知消息 title = message.getNotification().getTitle(); body = message.getNotification().getBody(); } else { // 数据消息,从getData中提取内容 title = message.getData().get("title"); body = message.getData().get("body"); } // 必须先创建通知渠道(Android 8.0+强制要求) createNotificationChannel(); // 构建通知,必须设置小图标 NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this, "channel_id") .setContentTitle(title) .setContentText(body) .setSmallIcon(R.mipmap.ic_launcher) // 替换为你的应用小图标资源 .setPriority(NotificationCompat.PRIORITY_DEFAULT) .setStyle(new NotificationCompat.BigTextStyle()) .setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION)) .setAutoCancel(true); NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); // 使用动态ID避免通知被覆盖 notificationManager.notify((int) System.currentTimeMillis(), notificationBuilder.build()); } // 添加创建通知渠道的方法 private void createNotificationChannel() { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { CharSequence name = "默认通知渠道"; String description = "接收应用通知"; int importance = NotificationManager.IMPORTANCE_DEFAULT; NotificationChannel channel = new NotificationChannel("channel_id", name, importance); channel.setDescription(description); NotificationManager notificationManager = getSystemService(NotificationManager.class); notificationManager.createNotificationChannel(channel); } }
3. 缺少通知小图标
原代码中NotificationCompat.Builder未设置setSmallIcon(),Android系统要求所有通知必须配置小图标,否则通知不会显示在状态栏。
修复:
在构建通知时添加.setSmallIcon(R.mipmap.ic_launcher)(替换为你应用的实际小图标资源)。
4. 依赖重复与版本冲突
Module的build.gradle中重复添加了firebase-messaging和firebase-analytics依赖,可能导致版本冲突:
implementation 'com.google.firebase:firebase-messaging:23.1.2' implementation 'com.google.firebase:firebase-messaging' // 重复的firebase-analytics依赖
修复:
保留Firebase BOM统一管理版本,删除重复依赖,修改后依赖片段如下:
dependencies { implementation platform('com.google.firebase:firebase-bom:31.1.0') implementation 'com.google.firebase:firebase-messaging' implementation 'com.google.firebase:firebase-analytics' implementation 'com.google.firebase:firebase-auth' implementation 'com.google.firebase:firebase-database' implementation 'com.google.firebase:firebase-storage' implementation 'com.google.firebase:firebase-firestore' implementation 'androidx.appcompat:appcompat:1.5.1' implementation 'com.google.android.material:material:1.7.0' implementation 'androidx.constraintlayout:constraintlayout:2.1.4' implementation 'androidx.recyclerview:recyclerview:1.2.1' implementation 'com.firebaseui:firebase-ui-database:8.0.2' implementation 'de.hdodenhof:circleimageview:3.1.0' implementation "com.github.bumptech.glide:glide:4.13.0" implementation 'org.ocpsoft.prettytime:prettytime:5.0.4.Final' implementation 'com.google.android.gms:play-services-nearby:18.0.2' }
5. 额外排查点
- Android 13+需确认目标设备已授予
POST_NOTIFICATIONS权限,可在应用启动时主动请求; - 验证FCM Token是否有效,目标设备网络正常,未被防火墙/代理拦截FCM服务;
- 检查Firebase控制台的
google-services.json文件是否正确配置,包名与应用一致; - 优先使用真机测试,部分模拟器存在FCM接收不稳定问题。
内容的提问来源于stack exchange,提问作者Local Host
相关产品推荐
相关产品推荐

