为什么 Intent 的额外字符串变成了另一个?



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

logcat:

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

  • D/MainActivity:onCreate:imgsGroupId=1a3db0a-ef1b-4007-9bdb-2194cb20cd61

  • D/DetailMessageRecyclerViewFragment:onViewCreated:imgsGroupId=1a3db0a-ef1b-4007-9bdb-2194cb20cd61

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

主要活动.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)
}
}
}

PendingIntent使用FLAG_ONE_SHOT解决问题。

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

相关内容

  • 没有找到相关文章

最新更新