MST

星途 面试题库

面试题:Kotlin中如何配置Firebase Cloud Messaging以接收推送通知

在Kotlin项目中,简述配置Firebase Cloud Messaging来接收推送通知的主要步骤,包括依赖添加、初始化设置以及必要的权限配置等。
45.4万 热度难度
编程语言Kotlin

知识考点

AI 面试

面试题答案

一键面试
  1. 添加依赖
    • 在项目的build.gradle文件中添加Google服务插件:
buildscript {
    dependencies {
        classpath 'com.google.gms:google-services:4.3.10'
    }
}
- 在应用模块的`build.gradle`文件中添加Firebase Cloud Messaging依赖:
implementation 'com.google.firebase:firebase-messaging-ktx:23.1.0'
- 应用插件:
apply plugin: 'com.google.gms.google-services'
  1. 初始化设置
    • AndroidManifest.xml中添加FirebaseMessagingServiceReceiver
<service
    android:name=".MyFirebaseMessagingService"
    android:exported="false">
    <intent-filter>
        <action android:name="com.google.firebase.MESSAGING_EVENT" />
    </intent-filter>
</service>
<receiver
    android:name="com.google.firebase.messaging.FirebaseMessagingReceiver"
    android:exported="true"
    android:permission="com.google.android.c2dm.permission.SEND">
    <intent-filter>
        <action android:name="com.google.android.c2dm.intent.RECEIVE" />
        <category android:name="${applicationId}" />
    </intent-filter>
</receiver>
- 创建`MyFirebaseMessagingService`类继承自`FirebaseMessagingService`,用于处理接收到的消息:
class MyFirebaseMessagingService : FirebaseMessagingService() {
    override fun onMessageReceived(remoteMessage: RemoteMessage) {
        // 处理推送消息内容
        val notification = remoteMessage.notification
        if (notification != null) {
            showNotification(notification.title, notification.body)
        }
    }

    private fun showNotification(title: String?, body: 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)

        val notificationBuilder = NotificationCompat.Builder(this, "default")
           .setSmallIcon(R.drawable.ic_notification)
           .setContentTitle(title)
           .setContentText(body)
           .setAutoCancel(true)
           .setContentIntent(pendingIntent)

        val notificationManager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            val channel = NotificationChannel("default", "Default Channel", NotificationManager.IMPORTANCE_DEFAULT)
            notificationManager.createNotificationChannel(channel)
        }
        notificationManager.notify(0, notificationBuilder.build())
    }
}
- 在`Application`类或`MainActivity`的`onCreate`方法中初始化Firebase:
FirebaseApp.initializeApp(this)
  1. 权限配置
    • AndroidManifest.xml中添加必要的权限:
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
<permission
    android:name="${applicationId}.permission.C2D_MESSAGE"
    android:protectionLevel="signature" />
<uses-permission android:name="${applicationId}.permission.C2D_MESSAGE" />