skip to Main Content

I am working on a project. The project requirement is that, when FCM notification arrives a specific activity opens without user interaction. This means I don’t want to open the activity by notification clicking instead I want to open the activity as soon as the notification arrives. When my app is foreground it works. But When My app is in the background or kill state, it doesn’t work. I am sharing my FMC code below. What I do now, to solve this issue?

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

        val intent= Intent(this,ShowNotificationActivity::class.java)
        intent.putExtra(Helper.USERNAME,message.data["userName"])
        intent.putExtra("title",message.notification?.title)
        intent.putExtra("body",message.notification?.body)
       
        intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK
       
         applicationContext.startActivity(intent)

    }

2

Answers


  1. Chosen as BEST ANSWER

    I am searched answer on many site , like stack over flow , chatgpt , android doc. I found answer on stack over flow which topic is different but answer work in my case. I share question link How to show custom UI for firebase notification when the app is in background?

    In short , i tell you that there two type payload :-

    1).Notification - Data(from server)

    2).Data(from server)

    -> When we use first , if my app in back or kill state than onMessageReceived() method not call/listen when new notification come. onMessageRecived() call when our app in foreground. If not than not call/listen.

    ->When we use second , if my app whichever state onMessageReceived() listen upcoming notification.


  2. Open Activity When FCM notification received without user interaction

    To solve this, you can start an activity from a service but there are some restrictions. However, this practice of interrupting the user when is currently doing something else is considered a bad design, especially from something that is supposed to be operating in the background. So you should launch an Activity when the user decides that and not when a notification arrives.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search