feat: fcm 데이터 수신 방식 수정
- data-only 메시지만 수신 방식에서 notification + data로 수신 방식 변경
This commit is contained in:
@@ -3,7 +3,6 @@ package kr.co.vividnext.sodalive.fcm
|
||||
import android.app.NotificationChannel
|
||||
import android.app.NotificationManager
|
||||
import android.app.PendingIntent
|
||||
import android.content.Context
|
||||
import android.content.Intent
|
||||
import android.media.RingtoneManager
|
||||
import android.os.Build
|
||||
@@ -12,6 +11,7 @@ import androidx.localbroadcastmanager.content.LocalBroadcastManager
|
||||
import com.google.firebase.messaging.FirebaseMessagingService
|
||||
import com.google.firebase.messaging.RemoteMessage
|
||||
import kr.co.vividnext.sodalive.R
|
||||
import kr.co.vividnext.sodalive.app.SodaLiveApp
|
||||
import kr.co.vividnext.sodalive.common.Constants
|
||||
import kr.co.vividnext.sodalive.common.SharedPreferenceManager
|
||||
import kr.co.vividnext.sodalive.splash.SplashActivity
|
||||
@@ -25,8 +25,13 @@ class SodaFirebaseMessagingService : FirebaseMessagingService() {
|
||||
val intent = Intent("ACTION_POINT_GRANTED")
|
||||
intent.putExtra("message", remoteMessage.data["message"])
|
||||
LocalBroadcastManager.getInstance(this).sendBroadcast(intent)
|
||||
} else if (
|
||||
SodaLiveApp.isAppInForeground &&
|
||||
remoteMessage.notification != null
|
||||
) {
|
||||
sendNotification(remoteMessage.data, remoteMessage.notification)
|
||||
} else if (remoteMessage.data["message"]?.isNotBlank() == true) {
|
||||
sendNotification(remoteMessage.data)
|
||||
sendNotification(remoteMessage.data, remoteMessage.notification)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -37,11 +42,14 @@ class SodaFirebaseMessagingService : FirebaseMessagingService() {
|
||||
SharedPreferenceManager.pushToken = token
|
||||
}
|
||||
|
||||
private fun sendNotification(messageData: Map<String, String>) {
|
||||
private fun sendNotification(
|
||||
messageData: Map<String, String>,
|
||||
notification: RemoteMessage.Notification?
|
||||
) {
|
||||
val notificationChannelId = getString(R.string.default_notification_channel_id)
|
||||
val defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION)
|
||||
val notificationManager =
|
||||
getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
|
||||
getSystemService(NOTIFICATION_SERVICE) as NotificationManager
|
||||
|
||||
// Since android Oreo notification channel is needed.
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
|
||||
@@ -91,16 +99,19 @@ class SodaFirebaseMessagingService : FirebaseMessagingService() {
|
||||
PendingIntent.FLAG_IMMUTABLE or PendingIntent.FLAG_UPDATE_CURRENT
|
||||
)
|
||||
|
||||
val title = notification?.title ?: messageData["title"]
|
||||
val message = notification?.body ?: messageData["message"]
|
||||
|
||||
val notificationBuilder = NotificationCompat.Builder(this, notificationChannelId)
|
||||
.setSmallIcon(R.mipmap.ic_launcher)
|
||||
.setContentTitle(messageData["title"])
|
||||
.setContentText(messageData["message"])
|
||||
.setContentTitle(title)
|
||||
.setContentText(message)
|
||||
.setSound(defaultSoundUri)
|
||||
.setAutoCancel(true)
|
||||
.setContentIntent(pendingIntent)
|
||||
|
||||
val bigTextStyle = NotificationCompat.BigTextStyle(notificationBuilder)
|
||||
bigTextStyle.bigText(messageData["message"])
|
||||
bigTextStyle.bigText(message)
|
||||
|
||||
notificationManager.notify(System.currentTimeMillis().toInt(), notificationBuilder.build())
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user