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

Android端FCM推送API返回成功但设备未收通知求助

FCM推送API返回成功但设备收不到通知的排查与修复

问题核心

调用FCM API返回{"success":1},但目标设备无通知显示,代码存在多处关键错误,以下是逐个排查和修复方案:

1. Manifest中Intent Filter拼写错误

你的Service配置里的action是com.google.firebase.MASSAGING_EVENT,正确的FCM消息触发action是com.google.firebase.MESSAGING_EVENT(单词拼写错误:MASSAGE → MESSAGE),这个错误会导致系统无法将推送消息路由到你的自定义FirebaseMessagingService。

修复后的Manifest代码:

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

2. Payload类型与接收逻辑不匹配

你发送的是notification类型的Payload,但在onMessageReceived中仅读取data字段:

  • 当App处于后台时,notification类型的消息会由系统直接展示,不会触发你的自定义Service
  • 仅data类型的消息无论前后台都会触发onMessageReceived

两种修复方案:

  • 方案一:修改发送逻辑为data类型Payload
    // 替换原推送方法中的notificationData部分
    JSONObject dataPayload = new JSONObject();
    dataPayload.put("title", title);
    dataPayload.put("message", body);
    jsonBody.put("data", dataPayload); // 替换原有的"notification"为"data"
    
  • 方案二:在接收逻辑中同时处理notification和data字段
    @Override
    public void onMessageReceived(@NonNull RemoteMessage remoteMessage) {
        super.onMessageReceived(remoteMessage);
    
        String title;
        String message;
    
        // 优先处理notification字段
        if (remoteMessage.getNotification() != null) {
            title = remoteMessage.getNotification().getTitle();
            message = remoteMessage.getNotification().getBody();
        } else {
            // 处理data字段
            Map<String, String> data = remoteMessage.getData();
            title = data.get("title");
            message = data.get("message");
        }
    
        // 后续通知构建代码保持不变
    }
    

3. 缺失通知渠道创建(Android 8.0+)

Android 8.0(API 26)及以上系统要求必须创建通知渠道才能显示通知,你的代码中未创建CHANNEL_ID="104"对应的渠道,会导致通知被系统拦截。

添加通知渠道创建逻辑:
在FirebasePushNotification中新增onCreate和渠道创建方法:

@Override
public void onCreate() {
    super.onCreate();
    createNotificationChannel();
}

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

4. PendingIntent Flag适配Android 12+

Android 12(API 31)及以上版本要求PendingIntent必须指定FLAG_IMMUTABLE或FLAG_MUTABLE,否则会抛出异常导致通知无法构建。

修改PendingIntent创建代码:

Intent intent = new Intent(this, MainActivity.class);
int pendingIntentFlag;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
    pendingIntentFlag = PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_IMMUTABLE;
} else {
    pendingIntentFlag = PendingIntent.FLAG_UPDATE_CURRENT;
}
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, pendingIntentFlag);
builder.setContentIntent(pendingIntent);

5. 测试场景验证

测试时直接给自己发推送需注意:

  • 确保设备网络正常,能连接FCM服务器
  • 避免App被系统后台杀死(部分厂商的电池优化/后台限制会阻断Service)
  • 检查设备已开启App的通知权限

验证步骤

  1. 修复所有代码后重新编译安装App
  2. 触发测试推送,查看Logcat是否打印remoteMessage.getFrom()和消息内容
  3. 检查通知栏是否正常显示推送

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.16 21:55:17