android - 为什么 Intent 的额外字符串变成了另一个?
问题描述
为什么使用 Intent.putExtra trasmit 从 MyFirebaseMessagingService.kt 到 MainActivity.kt 的 imgsGroupId 变成了另一个 imgsGroupId ???
日志猫:
D/MyFirebaseMessagingService:onMessageReceived:imgsGroupId = ad5191d8-56e4-4170-912b-9cd89e554d1e
D/MainActivity: onCreate:imgsGroupId = 1a33db0a-ef1b-4007-9bdb-2194cb20cd61
D/DetailMessageRecyclerViewFragment:onViewCreated:imgsGroupId = 1a33db0a-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)
}
}
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)
}
}
}
解决方案
PendingIntent 使用 FLAG_ONE_SHOT 解决问题。
val pendingIntent = PendingIntent.getActivity(
this, 0,
intent,
FLAG_ONE_SHOT
)
推荐阅读
- python - 相同元素在迭代期间变得不可点击
- node.js - 如何将模型续集作为参数传递?
- c# - 如何防止在 blazor 中路由
- java - Java RegEx 在每三个空格后拆分
- mysql - 在 MySQL 中添加索引后如何重建现有表中的索引?
- node.js - Heroku H27 客户端请求因服务器发送事件 (SSE) GET 事件而中断
- c++ - std::vector 的 C++ 类成员初始化
- arduino - 尝试在 arduino 中声明常量变量时出现问题
- java - Firebase 实时数据库规则
- python - Crontab 作业未在 django 项目中执行