android - 更改包名称后 FCM 不工作

标签 android firebase

我正在尝试开发一个将使用 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

How to download ?

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/

相关文章:

typescript - 如何使用 angularfire2 检查 Firebase 中的值?

android - Firebase Firestore - 移动 SDK 和 Web SDK 之间的差异

swift - 使用注册 Controller 将用户添加到 firebase

java - WebView缓存后台预加载

android studio 没有检测到错误

android - 如何加快 Firebase 身份验证过程

android - 如何等待 FirebaseAuth 完成初始化?

Android 无法绑定(bind)到服务(在 App-Billing 中)

android - 如何以编程方式连接到已在 Android 手机中设置的 VPN

android - 从选项卡 1 选择 ListView 后切换到选项卡 2