1

I have a firebase messaging service that gets notifications from a firebase function when a new document is created in firestore. Below link is the question I've asked about the function.

how to write function for a specific document in firestore?

Now, I'm getting the notification, but I have a problem.check my notification screen shot

First one you see with a date and image. It is when my app is in background. Second one you see with a notification icon and with some message. It is when my app is open. below code is for the service.

class MyFirebaseMessagingService : FirebaseMessagingService() {
private val CHANNEL_ID_DEFAULT = "default"
private val manager by lazy {
    getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
}

 override fun onMessageReceived(remoteMessage: RemoteMessage) {
    super.onMessageReceived(remoteMessage)
    val title = remoteMessage.notification!!.title
    val body = remoteMessage.notification!!.body
     val intent = Intent(this, MainActivity::class.java)
        .putExtra("Type", body)
        .addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP or Intent.FLAG_ACTIVITY_SINGLE_TOP)
   var bodymesssage=""
    var bodymesssage2=""
    if(GetorUpdateUserDataService.PremiumUser)
    {
        bodymesssage="get 10% of the money"
        bodymesssage2="get ₹10 for new install"
    } else{
        bodymesssage="get 5% of the money"
        bodymesssage2="get ₹15 for new install"
    }
        when(body)
        {
            "percent"->{
                showNotification("Your friend made a withdrawal", bodymesssage, intent)
            }
            "new_friend"->{
                showNotification("Someone entered your refer code","you can get money.", intent)
            }
            "refer"->{
                showNotification("your friend has completed 7 Day Task today", bodymesssage2, intent)
            }
            else->{
                showNotification(title!!, body!!, intent)

            }

        }
}

private fun createDefaultChannel() {
    // Since android Oreo notification channel is needed.
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        val channel = NotificationChannel(
            CHANNEL_ID_DEFAULT,
            "Default",
            NotificationManager.IMPORTANCE_DEFAULT
        )
        manager.createNotificationChannel(channel)
    }
}

private fun showNotification(titlee: String, contexttext: String, intent: Intent) {
    // Make PendingIntent for notification
    val pendingIntent = PendingIntent.getActivity(
        this, 0 /* requestCode */, intent,
        PendingIntent.FLAG_UPDATE_CURRENT
    )
    createDefaultChannel()
    val builder = NotificationCompat.Builder(this, CHANNEL_ID_DEFAULT)
        .setSmallIcon(R.drawable.ic_baseline_notifications_24)
        .setContentTitle(titlee)
        .setContentText(contexttext)
        .setAutoCancel(true)
        .setContentIntent(pendingIntent)
    manager.notify(1, builder.build())
}

override fun onNewToken(p0: String) {
    super.onNewToken(p0)
    Log.d("TAG", "onNewToken: $p0")
}
}

my problem is when my app is in background and when i clicked on notification, it is restarting the app.

when my app is opened (only main activity is opened) and i click on notification, it directly opens the main activity and does what i coded which is what i wanted.

but when my app is opened(and in another activity) it is restarting the app. i want this to open main activity even it in background or in anther activity. and also i can get notification when my app is closed in my emulator but not in my Oppo F3 (android 6). how can i receive notification when my app is not opened?

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
L T R
  • 13
  • 4
  • Hi folk you can check below StackOverflow question [Click here](https://stackoverflow.com/questions/40718155/open-specific-activity-when-notification-clicked-in-fcm) – NitsDeveloper Jan 04 '22 at 06:51

0 Answers0