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

Android应用使用Firebase无法接收推送通知问题求助

Firebase Cloud Messaging 推送通知收不到?试试这些排查方案
  • 先检查模拟器配置

    • 必须用API 26及以上的系统版本,低版本模拟器对FCM支持有限
    • 模拟器要开启Google Play服务,进入设置查看是否为最新版本,有更新就完成升级
    • 确认模拟器能正常联网,可尝试访问firebase.google.com验证网络连通性
  • 核对FCM服务配置

    • 检查项目根目录的google-services.json是否为Firebase控制台下载的对应项目文件,包名要和应用完全一致
    • 应用级build.gradle需添加FCM依赖:
      implementation 'com.google.firebase:firebase-messaging:23.4.0' // 建议使用最新稳定版本
      
    • 项目级build.gradle需包含Google服务插件:
      classpath 'com.google.gms:google-services:4.4.0' // 建议使用最新稳定版本
      
    • 确认Firebase控制台的应用注册信息(包名、SHA-1指纹等)与本地项目完全匹配
  • 补充消息接收核心逻辑
    你当前的代码仅实现了Token获取,缺少消息接收处理类,需创建继承自FirebaseMessagingService的类来处理推送:

    public class MyFirebaseMessagingService extends FirebaseMessagingService {
        private static final String TAG = "MyFirebaseMsgService";
    
        @Override
        public void onMessageReceived(RemoteMessage remoteMessage) {
            super.onMessageReceived(remoteMessage);
            Log.d(TAG, "消息来源: " + remoteMessage.getFrom());
    
            // 处理通知消息
            if (remoteMessage.getNotification() != null) {
                Log.d(TAG, "通知内容: " + remoteMessage.getNotification().getBody());
                showNotification(remoteMessage.getNotification().getTitle(), remoteMessage.getNotification().getBody());
            }
    
            // 处理数据消息
            if (remoteMessage.getData().size() > 0) {
                Log.d(TAG, "数据负载: " + remoteMessage.getData());
            }
        }
    
        private void showNotification(String title, String message) {
            Intent intent = new Intent(this, MainActivity.class);
            intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
            PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_ONE_SHOT | PendingIntent.FLAG_IMMUTABLE);
    
            String channelId = "fcm_default_channel";
            Uri soundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
            NotificationCompat.Builder builder = new NotificationCompat.Builder(this, channelId)
                    .setSmallIcon(R.drawable.ic_notify) // 替换为你的通知图标
                    .setContentTitle(title)
                    .setContentText(message)
                    .setAutoCancel(true)
                    .setSound(soundUri)
                    .setContentIntent(pendingIntent);
    
            NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    
            // Android O及以上需创建通知渠道
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                NotificationChannel channel = new NotificationChannel(channelId,
                        "默认通知渠道",
                        NotificationManager.IMPORTANCE_DEFAULT);
                manager.createNotificationChannel(channel);
            }
    
            manager.notify(0, builder.build());
        }
    }
    

    同时在AndroidManifest.xml中注册该服务:

    <service
        android:name=".MyFirebaseMessagingService"
        android:exported="false">
        <intent-filter>
            <action android:name="com.google.firebase.MESSAGING_EVENT" />
        </intent-filter>
    </service>
    
  • 注意测试消息类型

    • 在Firebase控制台发送测试消息时,选择通知消息并填写标题和内容;若仅发送数据消息,需自行在代码中处理通知展示逻辑
    • 避免发送空消息,确保消息内容完整
  • 验证Token有效性

    • 确认粘贴到Firebase控制台的Token与应用日志中打印的Token完全一致,不要包含多余空格或换行
    • 若仍有问题,可调用FirebaseMessaging.getInstance().deleteToken()删除旧Token,重新获取后再测试

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.04 02:05:18