首页 > 解决方案 > 为什么 Intent 的额外字符串变成了另一个?

问题描述

为什么使用 Intent.putExtra trasmit 从 MyFirebaseMessagingService.kt 到 MainActivity.kt 的 imgsGroupId 变成了另一个 imgsGroupId ???

日志猫:

MyFirebaseMessagingService.kt

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)
    }
}

MainActivity.kt

class MainActivity : AppCompatActivity() {



    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        

        if (intent?.getStringExtra("imgsGroupId") != null) {
            var imgsGroupId = intent?.getStringExtra("imgsGroupId")
            Log.d(TAG, "onCreate: imgsGroupId = $imgsGroupId")
            val bundle = Bundle().apply {
                putString("imgsGroupId", imgsGroupId)
            }
            findNavController(R.id.nav_host_fragment)
                    .navigate(R.id.detailMessageRecyclerViewFragment,bundle)

        }

    }
}

标签: androidkotlinandroid-intentfirebase-cloud-messagingandroid-pendingintent

解决方案


PendingIntent 使用 FLAG_ONE_SHOT 解决问题。

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

推荐阅读