Search code examples
androidandroid-fragmentsfirebase-cloud-messagingandroid-notificationsandroid-pendingintent

Why when I pressed notification, but PendingIntent didn't go to another Fragment?


Why when I pressed notification, but PendingIntent didn't go to DetailMessageRecyclerViewFragment?? I tried a lot but failed.

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

}

Solution

  • You cannot build an Intent to launch a Fragment. You build an Intent to launch an Activity. The Activity can then decide what Fragment to show. This code is not valid:

        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
        )