我正在尝试开发一个将使用 Firebase 显示通知的应用程序。 Firebase 通知在我的设备上运行良好,但在其他设备上不起作用,但在更改包名称后,FCM 不再运行。我尝试在 Firebase 控制台 中添加相同的包名称,但仍然是同样的问题
这是我的代码
list .xml
<service
android:name=".Model.Services.MyFirebaseMessagingService">
<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="@drawable/icon" />
<meta-data
android:name="com.google.firebase.messaging.default_notification_color"
android:resource="@color/colorAccent" />
<meta-data
android:name="firebase_messaging_auto_init_enabled"
android:value="false" />
<meta-data
android:name="firebase_analytics_collection_enabled"
android:value="false" />
MyFirebaseMessagingService.kt
class MyFirebaseMessagingService : FirebaseMessagingService() {
private var mNotificationManager: NotificationManager? = null
private var mBuilder: NotificationCompat.Builder? = null
var NOTIFICATION_CHANNEL_ID = "10001"
var notificationPref:SharedPreferences? = null
override fun onMessageReceived(message: RemoteMessage?) {
Log.d("FirebaseMessage", "message")
notificationPref = getSharedPreferences("Notification" , Context.MODE_PRIVATE)
message?.data?.isNotEmpty()?.let {
Log.d("FirebaseMessage", "Message data payload: " + message.data)
}
message?.notification?.let {
Log.d("FirebaseMessage", "Message Notification Body: ${it.body}")
val editor = notificationPref?.edit()
var count = notificationPref?.getInt("notification_count" , 0)
Log.d("FirebaseMessage", "Message Notification count: $count")
count = count?.plus(1)
editor?.putInt("notification_count" , count!!)
editor?.apply()
sendNotification(it.body)
}
}
private fun sendNotification(notification: String?) {
val resultIntent = Intent(this, Dashboard::class.java)
resultIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
val resultPendingIntent = PendingIntent.getActivity(
this,
0 /* Request code */, resultIntent,
PendingIntent.FLAG_UPDATE_CURRENT
)
mBuilder = NotificationCompat.Builder(this)
mBuilder!!.setSmallIcon(R.mipmap.ic_launcher)
mBuilder!!.setContentTitle("Notification")
.setContentText(notification)
.setAutoCancel(false)
.setSound(Settings.System.DEFAULT_NOTIFICATION_URI)
.setContentIntent(resultPendingIntent)
mNotificationManager = this.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
val importance = NotificationManager.IMPORTANCE_HIGH
val notificationChannel =
NotificationChannel(NOTIFICATION_CHANNEL_ID, "NOTIFICATION_CHANNEL_NAME", importance)
notificationChannel.enableLights(true)
notificationChannel.lightColor = Color.RED
notificationChannel.enableVibration(true)
notificationChannel.vibrationPattern = longArrayOf(100, 200, 300, 400, 500, 400, 300, 200, 400)
assert(mNotificationManager != null)
mBuilder!!.setChannelId(NOTIFICATION_CHANNEL_ID)
mNotificationManager!!.createNotificationChannel(notificationChannel)
}
assert(mNotificationManager != null)
mNotificationManager!!.notify(0 /* Request Code */, mBuilder!!.build())
}
}
最佳答案
转到 firebase console .
选择你的项目
下载 google-service.json
Get a config file for your Android app To download a config file for an Android app: Sign in to Firebase and open your project.
Click the Settings icon and select Project settings. In the Your apps card, select the package name of the app you need a config file for from the list.
Click google-services.json.
将 google-services.json
文件复制到您的 Android Studio 项目的 app/文件夹中。
FCM 基于 google-service.json
配置。
更多详情Set up a Firebase Cloud Messaging client app on Android
注意:完成所有设置后。
删除 build
文件夹并重建项目。
关于android - 更改包名称后 FCM 不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53775136/