FirebaseMessagingService无法接收通知 求助排查问题原因
安卓FCM推送服务未触发问题排查
我在安卓应用中集成FirebaseMessagingService实现推送通知,但该服务完全未正常工作——服务里的所有日志都没触发。调用sendNotification函数时,只会出现以下日志:
24505-13086 AlarmManager com.google.android.gms.persistent I setExactAndAllowWhileIdle [name: FcmRetry type: 2 triggerAtMillis: 2525414375]
已查阅谷歌官方文档但没找到原因,求排查思路。
发送通知函数代码
private void sendNotification(String messageBody){ ApiClient.getClient().create(ApiService.class).sendMessage( Constants.getRemoteMsgHeaders(), messageBody ).enqueue(new Callback<String>() { @Override public void onResponse(@NonNull Call<String> call, @NonNull Response<String> response) { if (response.isSuccessful()){ try{ if (response.body() !=null){ JSONObject responseJson = new JSONObject(response.body()); JSONArray result = responseJson.getJSONArray("results"); if(responseJson.getInt("failure")==1){ JSONObject error = (JSONObject) result.get(0); makeToast(error.getString("error")); return; } } } catch(JSONException e){ makeToast(e.getMessage()); } makeToast("notification sent"); } else{ makeToast("error "+response.code()); } }
MessagingService服务代码
public class MessagingService extends FirebaseMessagingService { private static final String TAG = "FirebaseMessagingService"; private PreferencesManager preferencesManager; @Override public void onNewToken(@NonNull String token) { super.onNewToken(token); Log.d("FCM", "token: " + token); } @Override public void onMessageReceived( RemoteMessage message) { super.onMessageReceived(message); showNotification(message.getData().get("message")); Log.d("FCM", "message: " + message.getNotification().getBody()); Log.d(TAG, "received"); User user = new User(); user.id = message.getData().get(Constants.KEY_USER_ID); if(user.id!=null){ Log.d(TAG, "received user id: " + user.id); } else{ Log.d(TAG, "null user id"); } user.name = message.getData().get(Constants.KEY_NAME); user.token = message.getData().get(Constants.KEY_FCM_TOKEN); showNotification(user.name, "aea"); int notificationId = new Random().nextInt(); String channel_id = "chat_message"; Intent intent = new Intent(this, ChatActivity.class); intent.setFlags(Intent.FLAG_ACTIVITY_NEW_DOCUMENT | Intent.FLAG_ACTIVITY_CLEAR_TASK); intent.putExtra(Constants.KEY_USER, user); PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT); NotificationCompat.Builder builder = new NotificationCompat.Builder(this, channel_id); builder.setSmallIcon(R.drawable.ic_back); builder.setContentTitle(user.name); builder.setContentText(message.getData().get(Constants.KEY_MESSAGE)); builder.setStyle( new NotificationCompat.BigTextStyle().bigText( message.getData().get(Constants.KEY_MESSAGE) ) ); builder.setPriority(NotificationCompat.PRIORITY_DEFAULT); builder.setContentIntent(pendingIntent); builder.setAutoCancel(true); if (Build.VERSION.SDK_INT > Build.VERSION_CODES.O) { CharSequence channelName = "Chat Messages"; String channelDescription = "This Channel is used for chat message notifications"; int importance = NotificationManager.IMPORTANCE_DEFAULT; NotificationChannel channel = new NotificationChannel(channel_id, channelName, importance); channel.setDescription(channelDescription); NotificationManager notificationManager = getSystemService(NotificationManager.class); notificationManager.createNotificationChannel(channel); } NotificationManagerCompat notificationManagerCompat = NotificationManagerCompat.from(this); if (ActivityCompat.checkSelfPermission(this, Manifest.permission.POST_NOTIFICATIONS) != PackageManager.PERMISSION_GRANTED) { return; } notificationManagerCompat.notify(notificationId, builder.build()); }
Manifest配置片段
<service android:name=".activities.main_activities.MessagingService" android:enabled="true" android:permission="com.google.android.c2dm.permission.SEND" android:exported="true"> <intent-filter> <action android:name="com.google.firebase.MESSAGING_EVENT" /> <action android:name="com.google.android.c2dm.intent.RECEIVE" /> <action android:name="com.google.firebase.INSTANCE_ID_EVENT"/> </intent-filter> </service>
排查思路
修正Manifest配置
- 移除
android:permission="com.google.android.c2dm.permission.SEND":该权限属于FCM消息发送方,接收端服务无需声明,添加后会导致权限校验失败,阻止服务接收事件。 - 移除
com.google.firebase.INSTANCE_ID_EVENT:这是旧版FCM的Token更新action,新版已通过onNewToken方法处理,无需在Manifest中声明。 - 确认
android:name路径:确保声明的服务路径与实际类的包名完全一致,避免因路径错误导致系统无法找到服务。
- 移除
验证消息类型与发送格式
- 确保发送的是数据消息:如果是纯通知消息,应用后台时系统会直接展示通知,不会触发
onMessageReceived;只有数据消息或前台状态下的混合消息才会进入该方法。检查sendNotification发送的messageBody是否包含data字段。 - 确认FCM Token有效性:调用sendNotification后返回failure=1时,需检查目标Token是否为当前设备的有效Token,是否已过期或被注销。
- 确保发送的是数据消息:如果是纯通知消息,应用后台时系统会直接展示通知,不会触发
检查依赖与系统限制
- 升级Firebase依赖:确保项目中firebase-messaging等依赖为最新稳定版,避免版本兼容问题。
- 关闭电池优化:部分厂商的电池优化会限制后台服务运行,将应用加入电池优化白名单后重试。
- 确认通知权限:Android 13+需申请
POST_NOTIFICATIONS权限,虽然权限不影响onMessageReceived触发,但会影响通知展示,可提前授予权限排除干扰。
调试验证
- 在
MessagingService的onCreate方法添加日志,确认服务是否被初始化:如果onCreate日志未触发,说明服务未被系统加载,问题出在Manifest或依赖配置;如果onCreate有日志但onMessageReceived无日志,说明消息未到达或类型不符合要求。 - 使用Firebase控制台发送测试数据消息:直接通过控制台发送自定义数据消息,验证是否能触发
onMessageReceived,排除自身sendNotification函数的问题。
- 在
内容的提问来源于stack exchange,提问作者AL Z
相关产品推荐
相关产品推荐

