更改软件包名称后FCM无法正常工作 [英] FCM is not working after changing the package name

查看:53
本文介绍了更改软件包名称后FCM无法正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am trying to develop an application that will show notifications using Firebase. Firebase notification was working fine on my device but its not working on other devices but after changing the package name, FCM is not working anymore. I try to add same package name in Firebase Console but still the same issue

这是我的代码

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

推荐答案

转到 firebase控制台.

选择您的项目

下载 google-service.json

如何下载?

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

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.

点击google-services.json.

Click google-services.json.

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

copy google-services.json file into the app/ folder of your Android Studio project.

FCM基于 google-service.json 配置.

有关更多详细信息,请在Android上设置Firebase Cloud Messaging客户端应用

For more details Set up a Firebase Cloud Messaging client app on Android

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

Note : After all setup. Delete build folder and rebuild project.

这篇关于更改软件包名称后FCM无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆