Search code examples
androidkotlinfirebase-cloud-messagingandroid-notifications

Is it possible to identity notification details (or notification id) inside boardcastReceiver?


I have multiple notifications send by different receivers(vendors). I implement a reply action button to reply to messages. Currently, I store notification details locally(shared preferences). Now, the reply action button working for single receiver notifications. Is it possible to identity notification details(or notification id) inside boardcastReceiver?

Is it possible to get from here?

  override fun onReceive(context: Context, intent: Intent?) {
        val remoteInput: Bundle? = RemoteInput.getResultsFromIntent(intent)
            val contentString = remoteInput.getCharSequence("NotificationReply").toString()

Full Code

MessagingService

class MyFirebaseMessagingService : FirebaseMessagingService() {
    override fun onMessageReceived(remoteMessage: RemoteMessage) {

        if (remoteMessage.data.isNotEmpty()) {
                sendNotification(NotificationModel.from(remoteMessage.data))
        }
    }


    private fun sendNotification(notificationModel: NotificationModel) {
        val channelId = getString(R.string.default_notification_channel_id)

        val intent = Intent(this, MainActivity::class.java)

            val sharedPreferences: SharedPreferences = this.getSharedPreferences("yellochatspfile", Context.MODE_PRIVATE)

            val editor: SharedPreferences.Editor = sharedPreferences.edit()
            editor.putString("message_data", notificationModel.messageData)
            editor.apply()
            editor.commit()

        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)

            PendingIntent.getBroadcast(
                    this,
                    MainActivity.REQUEST_CODE_APPROVE,
                    Intent(this, NotificationReceiver::class.java)
                            .putExtra(MainActivity.KEY_INTENT_APPROVE, MainActivity.REQUEST_CODE_APPROVE),
                    PendingIntent.FLAG_UPDATE_CURRENT
            )

        val notificationBuilder = NotificationCompat.Builder(this, channelId)
                .setSmallIcon(R.drawable.yc_logo)
                .setContentTitle(notificationModel.title)
                .setContentText(notificationModel.body)
                .setGroup(notificationModel.groupKey)
                .setAutoCancel(true)
                .setLargeIcon(BitmapFactory.decodeResource(resources, R.drawable.yc_logo))
                .setSound(defaultSoundUri)
                .setPriority(2)
                .setPriority(1)
                .setDefaults(2)
                .setContentIntent(pendingIntent)


            val replyLabel = "Enter your reply here"
            val remoteInput: RemoteInput = RemoteInput.Builder(MainActivity.NOTIFICATION_REPLY)
                    .setLabel(replyLabel)
                    .build()
            val action: NotificationCompat.Action =
                    NotificationCompat.Action.Builder(android.R.mipmap.sym_def_app_icon,
                            "Reply", pendingIntent)
                            .addRemoteInput(remoteInput)
                            .build()

            notificationBuilder.addAction(action)

        var notificationManager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager

        notificationManager.cancel(MainActivity.TAG_VALIDATION, 1)
        MainActivity.NOTIFICATION_ID = r.nextInt()
        notificationManager.notify(MainActivity.TAG_NORMAL, MainActivity.NOTIFICATION_ID, notificationBuilder.build())
    }
}

BroadcastReceiver

class NotificationReceiver : BroadcastReceiver() {
    val db = Firebase.firestore

    override fun onReceive(context: Context, intent: Intent?) {

        val remoteInput: Bundle? = RemoteInput.getResultsFromIntent(intent)
        if (remoteInput != null) {
            val contentString = remoteInput.getCharSequence("NotificationReply").toString()
            val sharedPreferences: SharedPreferences = context.getSharedPreferences("yellochatspfile", Context.MODE_PRIVATE)

            val messageData = sharedPreferences.getString("message_data", "")

            if (messageData != null && messageData != "") {
               // some logics to send message
                }else{
                    notificationProcess(sharedPreferences, false, context)
                }
            }else{
                notificationProcess(sharedPreferences, false, context)
            }
        }
    }

    private fun notificationProcess(sharedPreferences: SharedPreferences, isSuccess: Boolean, context: Context) {
    }
}

Solution

  • I used putExtra to share data with broadcastReceiver

    MessagingService

     val i = Intent(this, NotificationReceiver::class.java)
                i.putExtra("MESSAGE_DATA", notificationModel.messageData)
                PendingIntent.getBroadcast(
                        this,
                        MainActivity.REQUEST_CODE_APPROVE,
                        i,
                        PendingIntent.FLAG_UPDATE_CURRENT
                )
    

    BroadcastReceiver

     val messageData = intent?.getStringExtra("MESSAGE_DATA")
     println("messageData is : $messageData")