MST

星途 面试题库

面试题:Kotlin结合Firebase Cloud Messaging实现自定义推送消息处理逻辑

假设在Kotlin应用中使用Firebase Cloud Messaging,要求实现当接收到特定类型推送消息时,跳转到应用内特定页面,并在通知栏展示自定义样式。描述实现这一功能的思路以及关键代码片段。
49.8万 热度难度
编程语言Kotlin

知识考点

AI 面试

面试题答案

一键面试

实现思路

  1. 配置Firebase Cloud Messaging:在项目中添加Firebase依赖,配置Google服务文件等,确保能正常接收推送。
  2. 自定义通知样式:创建自定义的通知布局和样式,使用NotificationCompat构建通知。
  3. 处理推送消息:在FirebaseMessagingService中接收推送消息,判断消息类型,根据类型进行页面跳转和通知展示。

关键代码片段

  1. 配置Firebase
    • 在项目级build.gradle中添加:
buildscript {
    dependencies {
        classpath 'com.google.gms:google-services:4.3.10'
    }
}
  • 在应用级build.gradle中添加:
implementation 'com.google.firebase:firebase-messaging-ktx:23.0.4'
apply plugin: 'com.google.gms.google - services'
  1. 自定义通知样式
    • 创建自定义布局custom_notification_layout.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:padding="16dp">
    <TextView
        android:id="@+id/custom_title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="20sp"
        android:textStyle="bold"/>
    <TextView
        android:id="@+id/custom_message"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="16sp"
        android:layout_marginTop="8dp"/>
</LinearLayout>
  • FirebaseMessagingService中构建通知:
import android.app.Notification
import android.app.NotificationChannel
import android.app.NotificationManager
import android.app.PendingIntent
import android.content.Context
import android.content.Intent
import android.os.Build
import androidx.core.app.NotificationCompat
import com.google.firebase.messaging.FirebaseMessagingService
import com.google.firebase.messaging.RemoteMessage

class MyFirebaseMessagingService : FirebaseMessagingService() {
    private val CHANNEL_ID = "my_channel_id"
    private val NOTIFICATION_ID = 1

    override fun onMessageReceived(remoteMessage: RemoteMessage) {
        super.onMessageReceived(remoteMessage)
        val messageType = remoteMessage.data["type"]
        if ("specific_type" == messageType) {
            val title = remoteMessage.notification?.title
            val body = remoteMessage.notification?.body
            showCustomNotification(title, body)
            navigateToSpecificPage()
        }
    }

    private fun showCustomNotification(title: String?, body: String?) {
        val notificationManager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            val channel = NotificationChannel(CHANNEL_ID, "My Channel", NotificationManager.IMPORTANCE_DEFAULT)
            notificationManager.createNotificationChannel(channel)
        }
        val inflater = layoutInflater
        val customView = inflater.inflate(R.layout.custom_notification_layout, null)
        customView.findViewById<TextView>(R.id.custom_title).text = title
        customView.findViewById<TextView>(R.id.custom_message).text = body
        val notificationBuilder = NotificationCompat.Builder(this, CHANNEL_ID)
           .setCustomContentView(customView)
           .setSmallIcon(R.drawable.ic_notification)
           .setPriority(NotificationCompat.PRIORITY_DEFAULT)
        val notification: Notification = notificationBuilder.build()
        notificationManager.notify(NOTIFICATION_ID, notification)
    }

    private fun navigateToSpecificPage() {
        val intent = Intent(this, SpecificActivity::class.java)
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK)
        startActivity(intent)
    }
}
  1. 注册服务:在AndroidManifest.xml中注册FirebaseMessagingService
<service
    android:name=".MyFirebaseMessagingService"
    android:exported="false">
    <intent-filter>
        <action android:name="com.google.firebase.MESSAGING_EVENT" />
    </intent-filter>
</service>