Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
ArigarasuthanRepo opened this issue Aug 31, 2020 · 3 comments
Closed

Comments

@ArigarasuthanRepo
Copy link

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

@google-oss-bot
Copy link

This issue does not seem to follow the issue template. Make sure you provide all the required information.

@samtstern
Copy link
Contributor

@ArigarasuthanRepo this has been discussed extensively, please see another thread such as #4 or #368

@ArigarasuthanRepo
Copy link
Author

@ArigarasuthanRepo this has been discussed extensively, please see another thread such as #4 or #368

this thread I saw already none of them solved my issue. that's why I posted the issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants