Android集成FCM后无法接收推送通知求助
Firebase Cloud Messaging (FCM) 集成推送接收问题排查求助
已花费一周时间尝试为应用集成FCM并发送测试推送通知,按官方文档、YouTube教程操作后仍无法解决问题——设备无法接收推送,Firebase控制台仅显示活动已创建且处于激活状态,不清楚是否存在错误,怀疑遗漏细节,恳请协助排查。
配置与代码详情
Manifest文件
<application> <service android:name=".service.MyFirebaseMessagingService" tools:ignore="Instantiatable"> <intent-filter> <action android:name="com.google.firebase.MESSAGING_EVENT" /> </intent-filter> </service> <meta-data android:name="com.google.firebase.messaging.default_notification_icon" android:resource="@mipmap/finsol_logo_round" /> <meta-data android:name="com.google.firebase.messaging.default_notification_color" android:resource="@color/colorPrimary" /> </application>
服务代码(Kotlin)
class MyFirebaseMessagingService : FirebaseMessagingService() { override fun onNewToken(token: String) { sendRegistrationToServer(token) } private fun sendRegistrationToServer(token: String?) { Firebase.messaging.token FirebaseMessaging.getInstance().token.addOnCompleteListener(OnCompleteListener { task -> if (!task.isSuccessful) { Timber.e(task.exception) return@OnCompleteListener } val token = task.result Timber.e(token) }) } override fun onMessageReceived(remoteMessage: RemoteMessage) { Timber.e(remoteMessage.messageId) if (remoteMessage.notification != null) { generateNotification(remoteMessage.notification!!.title!!, remoteMessage.notification!!.body!!) } } private fun getRemoteView(title: String, message: String): RemoteViews { val remoteView = RemoteViews("br.com.finsol.finsolmobile", R.layout.notification) remoteView.setTextViewText(R.id.title_notification, title) remoteView.setTextViewText(R.id.message_notification, message) remoteView.setImageViewResource(R.id.logo, R.mipmap.finsol_logo_foreground) return remoteView } private fun generateNotification(title: String, message: String) { val intent = Intent(this, MainActivity::class.java) intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP) val pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_ONE_SHOT) var builder: NotificationCompat.Builder = NotificationCompat.Builder(applicationContext.applicationContext, channelId) .setSmallIcon(R.mipmap.finsol_logo_foreground) .setAutoCancel(true) .setVibrate(longArrayOf(1000, 1000, 1000, 1000)) .setOnlyAlertOnce(true) .setContentIntent(pendingIntent) builder.setContent(getRemoteView(title, message)) val notificationManager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { val notificationChannel = NotificationChannel(channelId, channelName, NotificationManager.IMPORTANCE_HIGH) notificationManager.createNotificationChannel(notificationChannel) } notificationManager.notify(0, builder.build()) } }
额外信息与疑问
- 依赖与Firebase文档完全一致,当前使用Android 11系统,Tools>Firebase>Cloud Messaging菜单显示依赖配置正确。
- 怀疑是否因为是签名应用导致无法接收?已尝试添加SHA-256指纹,但不确定非上架应用是否需要该配置。
- 应用存在于Play Console项目中,Firebase是否必须与同一项目关联?这会不会是问题所在?看起来应用并未真正与Firebase关联,尽管系统显示已关联。
内容的提问来源于stack exchange,提问作者lucas_vanderlei
相关产品推荐
相关产品推荐

