实现思路
- 配置Firebase Cloud Messaging:在项目中添加Firebase依赖,配置Google服务文件等,确保能正常接收推送。
- 自定义通知样式:创建自定义的通知布局和样式,使用
NotificationCompat
构建通知。
- 处理推送消息:在
FirebaseMessagingService
中接收推送消息,判断消息类型,根据类型进行页面跳转和通知展示。
关键代码片段
- 配置Firebase:
buildscript {
dependencies {
classpath 'com.google.gms:google-services:4.3.10'
}
}
implementation 'com.google.firebase:firebase-messaging-ktx:23.0.4'
apply plugin: 'com.google.gms.google - services'
- 自定义通知样式:
- 创建自定义布局
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)
}
}
- 注册服务:在
AndroidManifest.xml
中注册FirebaseMessagingService
:
<service
android:name=".MyFirebaseMessagingService"
android:exported="false">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT" />
</intent-filter>
</service>