为什么当我按下通知时,PendingIntent 没有转到另一个 Fragment?
Why when I pressed notification, but PendingIntent didn't go to another Fragment?
为什么我按了通知,PendingIntent
却没有去DetailMessageRecyclerViewFragment
??我尝试了很多但都失败了。
MyFirebaseMessagingService.kt
class MyFirebaseMessagingService : FirebaseMessagingService() {
private var userAvatarByteArray: Bitmap? = null
override fun onMessageReceived(remoteMessage: RemoteMessage) {
super.onMessageReceived(remoteMessage)
val imgsGroupId = remoteMessage.data["imgsGroupId"]
val notificationContent = remoteMessage.data["messageContent"]
val userAvatarUrl = remoteMessage.data["userAvatarUrl"]
val messageSenderAvatar = BitmapFactory.decodeFile(userAvatarUrl)
/*userAvatarUrl?.toByteArray().let {
userAvatarByteArray = BitmapFactory.decodeByteArray(it, 0, it!!.size)
}*/
createNotificationChannel()
val intent = Intent(this, DetailMessageRecyclerViewFragment::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)
.setContentText(notificationContent)
.setLargeIcon(messageSenderAvatar)
.setContentIntent(pendingIntent)
.setAutoCancel(true)
.build()
with(NotificationManagerCompat.from(this)) {
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)
}
}
DetailMessageRecyclerViewFragment.kt
class DetailMessageRecyclerViewFragment : Fragment(R.layout.detail_message_recycler_view_fragment) {
private var uploadedImagesFromNotification: UploadedImages? = null
private var imgsGroupId: String? = null
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
imgsGroupId = activity?.intent?.getStringExtra("imgsGroupId")
if (imgsGroupId != null) {
Firebase.firestore.collection("uploadedImages").document(imgsGroupId!!)
.get().addOnSuccessListener {
uploadedImagesFromNotification = it.toObject(UploadedImages::class.java)
}
}
//Get notification to get message
if (uploadedImagesFromNotification != null) {
detailMessageViewModel.getUploadedMessage(uploadedImagesFromNotification!!).observe(viewLifecycleOwner, Observer {
images_messages_recycyler_view.apply {
setHasFixedSize(true)
layoutManager = LinearLayoutManager(context)
adapter = DetailMessageRecyclerAdapter(it, uploadedImagesFromNotification!!).apply {
notifyDataSetChanged()
}
}
})
}
}
您无法构建 Intent
来启动 Fragment
。您构建一个 Intent
来启动一个 Activity
。然后 Activity
可以决定要显示什么 Fragment
。此代码无效:
val intent = Intent(this, DetailMessageRecyclerViewFragment::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
)
为什么我按了通知,PendingIntent
却没有去DetailMessageRecyclerViewFragment
??我尝试了很多但都失败了。
MyFirebaseMessagingService.kt
class MyFirebaseMessagingService : FirebaseMessagingService() {
private var userAvatarByteArray: Bitmap? = null
override fun onMessageReceived(remoteMessage: RemoteMessage) {
super.onMessageReceived(remoteMessage)
val imgsGroupId = remoteMessage.data["imgsGroupId"]
val notificationContent = remoteMessage.data["messageContent"]
val userAvatarUrl = remoteMessage.data["userAvatarUrl"]
val messageSenderAvatar = BitmapFactory.decodeFile(userAvatarUrl)
/*userAvatarUrl?.toByteArray().let {
userAvatarByteArray = BitmapFactory.decodeByteArray(it, 0, it!!.size)
}*/
createNotificationChannel()
val intent = Intent(this, DetailMessageRecyclerViewFragment::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)
.setContentText(notificationContent)
.setLargeIcon(messageSenderAvatar)
.setContentIntent(pendingIntent)
.setAutoCancel(true)
.build()
with(NotificationManagerCompat.from(this)) {
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)
}
}
DetailMessageRecyclerViewFragment.kt
class DetailMessageRecyclerViewFragment : Fragment(R.layout.detail_message_recycler_view_fragment) {
private var uploadedImagesFromNotification: UploadedImages? = null
private var imgsGroupId: String? = null
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
imgsGroupId = activity?.intent?.getStringExtra("imgsGroupId")
if (imgsGroupId != null) {
Firebase.firestore.collection("uploadedImages").document(imgsGroupId!!)
.get().addOnSuccessListener {
uploadedImagesFromNotification = it.toObject(UploadedImages::class.java)
}
}
//Get notification to get message
if (uploadedImagesFromNotification != null) {
detailMessageViewModel.getUploadedMessage(uploadedImagesFromNotification!!).observe(viewLifecycleOwner, Observer {
images_messages_recycyler_view.apply {
setHasFixedSize(true)
layoutManager = LinearLayoutManager(context)
adapter = DetailMessageRecyclerAdapter(it, uploadedImagesFromNotification!!).apply {
notifyDataSetChanged()
}
}
})
}
}
您无法构建 Intent
来启动 Fragment
。您构建一个 Intent
来启动一个 Activity
。然后 Activity
可以决定要显示什么 Fragment
。此代码无效:
val intent = Intent(this, DetailMessageRecyclerViewFragment::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
)