Почему дополнительный объект Intent из String стал другим? - PullRequest
0 голосов
/ 11 июля 2020

Почему imgsGroupId из MyFirebaseMessagingService.kt с использованием Intent.putExtra trasmit в MainActivity.kt стал другим imgsGroupId ???

logcat:

  • D / MyFirebaseMessagingService: onMessageReceived: imgsGroupId = ad5191d8-56e4-4170-912b-9cd89e554d1e

  • D / MainActivity: onCreate: imgsGroupId = 1a33db0a-ef1b-4007-9bdb-2194cb11

MyFirebaseMessagingService.kt

21
class MyFirebaseMessagingService : FirebaseMessagingService() {
    private val TAG: String? = MyFirebaseMessagingService::class.java.simpleName

    @RequiresApi(Build.VERSION_CODES.O)
    override fun onMessageReceived(remoteMessage: RemoteMessage) {
        super.onMessageReceived(remoteMessage)

        Log.d(TAG, "onMessageReceived: remoteMessage = $remoteMessage")
        val imgsGroupId = remoteMessage.data["imgsGroupId"]
        Log.d(TAG, "onMessageReceived: imgsGroupId = $imgsGroupId")
        val notificationContent = remoteMessage.data["messageContent"]
        val userName = remoteMessage.data["userName"]
        Log.d(TAG, "onMessageReceived: userName = $userName")
        val userAvatarUrl = remoteMessage.data["userAvatarUrl"]

        val inputStream = URL(userAvatarUrl).openStream()
        val userAvatarBitmap = BitmapFactory.decodeStream(inputStream)

        createNotificationChannel()


        val intent = Intent(this, MainActivity::class.java).apply {
            flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK
            putExtra("imgsGroupId",imgsGroupId)
        }

         val pendingIntent = PendingIntent.getActivity(
             this, 0,
             intent,
             0
         )
        val notification = NotificationCompat.Builder(this, MESSAGE_NOTIFICATION_CHANNEL_ID)
            .setSmallIcon(R.drawable.ic_baseline_message_24)
            .setContentTitle(userName)
            .setContentText(notificationContent)
            .setLargeIcon(userAvatarBitmap)
            .setContentIntent(pendingIntent)
            .setAutoCancel(true)
            .build()

        with(NotificationManagerCompat.from(applicationContext)) {
            notify(MESSAGE_NOTIFICATION_ID, notification)
        }
    }

    private fun createNotificationChannel() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {

            val notificationChannel = NotificationChannel(
                MESSAGE_NOTIFICATION_CHANNEL_ID,
                MESSAGE_CHANNEL_NAME,
                NotificationManager.IMPORTANCE_HIGH
            ).apply {
                enableLights(true)
                lightColor = Color.CYAN

            }
            val notificationManager =
                getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
            notificationManager.createNotificationChannel(notificationChannel)
        }

    }

    override fun onNewToken(token: String) {
        super.onNewToken(token)
    }
}
MainActiveService.kt *1019* 21 *1020* 1023 *

1 Ответ

0 голосов
/ 11 июля 2020

PendingIntent, используйте FLAG_ONE_SHOT для решения проблемы.

val pendingIntent = PendingIntent.getActivity(
             this, 0,
             intent,
             FLAG_ONE_SHOT
         )
...