更改软件包名称后FCM无法正常工作

时间:2018-12-14 07:22:01

标签: android firebase

我正在尝试开发一个使用Firebase显示通知的应用程序。 Firebase通知在我的设备上运行正常,但在其他设备上不运行,但是更改软件包名称后, FCM 不再起作用。我尝试在 Firebase控制台中添加相同的程序包名称,但仍然是相同的问题

这是我的代码

Manifest.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())
}
}

1 个答案:

答案 0 :(得分:2)

转到firebase console

选择您的项目

下载google-service.json

  
    

How to download ?

         

为您的Android应用获取配置文件     要为Android应用下载配置文件,请执行以下操作:     登录到Firebase并打开您的项目。

         

单击“设置”图标,然后选择“项目设置”。     在“您的应用”卡中,从列表中选择您需要配置文件的应用的程序包名称。

         

点击google-services.json。

  

google-services.json文件复制到Android Studio项目的app /文件夹中。

FCM基于google-service.json配置。

有关更多详细信息,Set up a Firebase Cloud Messaging client app on Android

注意:完成所有设置后。 删除build文件夹并重建项目。