Skip to content

Firebase notifications did not come when I kill the app in android #1165

Closed
@ArigarasuthanRepo

Description

@ArigarasuthanRepo

I am developing an android application using kotlin. I am using firebase notifications in my app.notifications arrive when the app in foreground and background.but when I killed the app notification did not come. How to fix the issue?

I am using latest firebase version

implementation 'com.google.firebase:firebase-messaging:20.2.0'

Implementing my messaging service:

class MyFirebaseMessagingService : FirebaseMessagingService()
{
    lateinit var notificationManager: NotificationManager

    private var channelId:String = "attendance-taker.notifications"

    lateinit var builder: NotificationCompat.Builder

    var notificationId: Int = 123


    //var prefs:SharedPreferences = applicationContext.getSharedPreferences(applicationContext.getString(R.string.app_name), Context.MODE_PRIVATE)
    override fun onMessageReceived(classinformation: RemoteMessage) {
         PushNotification()

        }

        // Check if message contains a notification payload.
        if (classinformation.notification != null) {

        }

    }

    override fun onNewToken(token: String) {
        Log.d("TokenPrint", token)
    }



    private fun PushNotification() {
        notificationManager = this.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            createNotificationChannel()
        }
        val intent = Intent(this, HomeActivity::class.java).apply {
            flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK
        }
        var content:String = "Helloooooooooooooo"
        val pendingIntent: PendingIntent = PendingIntent.getActivity(this, 0, intent, 0)
        builder = NotificationCompat.Builder(this, channelId)
            .setContentTitle(getString(R.string.app_name))
            .setContentText(content)
            .setContentIntent(pendingIntent)
            .setSmallIcon(R.mipmap.ic_launcher)
            .setPriority(NotificationCompat.PRIORITY_HIGH)
            .setDefaults(NotificationCompat.DEFAULT_ALL)
            .setAutoCancel(true)
        notificationManager.notify(notificationId, builder.build())
    }


    private fun createNotificationChannel() {

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            notificationManager = this.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
            val name = getString(R.string.app_name)
            val descriptionText = ""
            val importance = NotificationManager.IMPORTANCE_HIGH
            val channel = NotificationChannel(channelId, name, importance).apply {
                description = descriptionText
                enableLights(true)
                lightColor = Color.GREEN
                enableVibration(false)
            }
            notificationManager.createNotificationChannel(channel)
        }
    }

}

Declared in AndoridManifest.xml like this

<!-- Firebase Messaging service -->
<service
    android:name=".utils.MyFirebaseMessagingService"
    android:exported="false">
    <intent-filter>
        <action android:name="com.google.firebase.MESSAGING_EVENT" />
    </intent-filter>
</service>

Only the foreground notification arrives but when I killed the app notifications not came. Help to fix the issue.

I am using Android emulator and android version is android-10

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions