Notification Service in android when app is in background


I am building an e commerce app using Android Kotlin Jetpack Compose with Admin Pannel. If admin update the status of order how my android app will know the status of order to push notification.My Notification are working when app is in awake mode and place order.I want push notification like amazon,flipkart it always show notification of order status even though app is not running in foreground.Here is my code implemented for notification.

class MyFirebaseService : FirebaseMessagingService() {
    override fun onNewToken(token: String) {
        super.onNewToken(token)
        Log.d("FCM_TOKEN", token)
    }

    override fun onMessageReceived(message: RemoteMessage) {
        super.onMessageReceived(message)

        val session = SessionManager(applicationContext)

        if (!session.isNotificationEnabled()) {
            Log.d("FCM", "Notification blocked by user setting")
            return
        }

        val title = message.data["title"] ?: "Title"
        val body = message.data["body"] ?: "Message"

        Log.d("FCM", "title: $title body: $body")

        CoroutineScope(Dispatchers.IO).launch {

            val db = Room.databaseBuilder(
                applicationContext,
                AppDatabase::class.java,
                "mysanjeevni_db"
            ).build()

            db.notificationDao().insert(
                NotificationEntity(
                    title = title,
                    body = body
                )
            )
        }

        showNotification(title, body)
    }

    private fun showNotification(title: String, body: String) {
        val manager = getSystemService(NOTIFICATION_SERVICE) as NotificationManager
        val channelId = "default"

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            val channel = NotificationChannel(
                channelId,
                "Default Channel",
                NotificationManager.IMPORTANCE_HIGH
            )
            manager.createNotificationChannel(channel)
        }
        val intent = Intent(this, MainActivity::class.java).apply {
            flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TOP
            putExtra("destination", "notification")
        }
        val pendingIntent = PendingIntent.getActivity(
            this,
            0,
            intent,
            PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_IMMUTABLE
        )
        val notification = NotificationCompat.Builder(this, channelId)
            .setContentTitle(title)
            .setContentText(body)
            .setSmallIcon(R.drawable.app_logo)
            .setAutoCancel(true)
            .setContentIntent(pendingIntent)
            .build()
        manager.notify(1, notification)
    }

}
0
Jun 30 at 12:39 PM
User AvatarApoorv Rathore
#android#kotlin#firebase-cloud-messaging

Accepted Answer

This might help Firebase onMessageReceived not called when app in background

User AvatarAjay Pandya
Jun 30 at 12:46 PM
1