|
@@ -1,9 +1,11 @@
|
|
|
package com.swago.app
|
|
|
|
|
|
+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
|
|
@@ -14,9 +16,9 @@ class MyFirebaseMessagingService: FirebaseMessagingService() {
|
|
|
|
|
|
override fun onMessageReceived(message: RemoteMessage) {
|
|
|
super.onMessageReceived(message)
|
|
|
- LogUtil.d("MyFirebaseMessagingService","onMessageReceived")
|
|
|
- message.notification?.body?.let {
|
|
|
- sendNotification(it)
|
|
|
+ LogUtil.d("MyFirebaseMessagingService","onMessageReceived" + message)
|
|
|
+ message.notification?.let {
|
|
|
+ sendNotification(it.title,it.body)
|
|
|
}
|
|
|
|
|
|
}
|
|
@@ -25,19 +27,29 @@ class MyFirebaseMessagingService: FirebaseMessagingService() {
|
|
|
super.onNewToken(token)
|
|
|
}
|
|
|
|
|
|
- private fun sendNotification(messageBody: String) {
|
|
|
+ private fun sendNotification(title:String?,messageBody: String?) {
|
|
|
+ LogUtil.d("FCM", "sendNotification: " + messageBody)
|
|
|
val intent = Intent(this, HomeActivity::class.java)
|
|
|
intent.flags = Intent.FLAG_ACTIVITY_CLEAR_TOP
|
|
|
val pendIntent = PendingIntent.getActivity(this,0,intent,PendingIntent.FLAG_IMMUTABLE)
|
|
|
val channelId = "default_channel_id"
|
|
|
val notificationBuilder = NotificationCompat.Builder(this, channelId)
|
|
|
.setSmallIcon(R.mipmap.ic_launcher_foreground)
|
|
|
- .setContentTitle("FCM Message")
|
|
|
+ .setContentTitle(title)
|
|
|
.setContentText(messageBody)
|
|
|
.setAutoCancel(true)
|
|
|
.setContentIntent(pendIntent)
|
|
|
|
|
|
val notificationManager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
|
|
|
+ // Android 8.0及以上需要设置通知渠道
|
|
|
+ if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
|
|
|
+ val channel = NotificationChannel(
|
|
|
+ channelId,
|
|
|
+ "Channel title",
|
|
|
+ NotificationManager.IMPORTANCE_DEFAULT
|
|
|
+ )
|
|
|
+ notificationManager.createNotificationChannel(channel)
|
|
|
+ }
|
|
|
notificationManager.notify(0, notificationBuilder.build())
|
|
|
}
|
|
|
}
|