Home > OS >  Android Notification data is not passed in terminated and background state
Android Notification data is not passed in terminated and background state

Time:08-19

I am working on android push notification and notification is get in all three state and then, when i tap on notification data will get in only foreground state but not get in terminated and background.

Pending intent in service file :

val intentVid = Intent(context, MainActivity::class.java)
    intentVid.putExtra("Id", "787789")
    intentVid.flags = Intent.FLAG_ACTIVITY_SINGLE_TOP or Intent.FLAG_ACTIVITY_CLEAR_TOP
    val pendingIntent = PendingIntent.getActivity(
        context, 0,
        intentVid, PendingIntent.FLAG_UPDATE_CURRENT
    )

Below function is called in onCreate and onNewIntent function :

   private fun handleNotificationEvent(intent: Intent?) {
            if (intent!!.extras != null) {
                order_status = intent.extras!!.getString("Id", "")
                if (order_status.length == 6) {
                    Toast.makeText(
                        this,
                        "ID :- "   intent.extras!!.getString("Id", ""),
                        Toast.LENGTH_LONG
                    )
                        .show()
                }
            }
        }

CodePudding user response:

Your code is right for notification.

  1. Go to the android manifest file and find launcher activity like the below lines example.

    <activity
      android:name=".ui.splash.SplashActivity"
      <intent-filter>
         <action android:name="android.intent.action.MAIN" />
         <category android:name="android.intent.category.LAUNCHER" />
      </intent-filter>
    </activity>
    
  2. Then use the below code in launcher activity and then pass data to your activity.

    private fun handleNotificationEvent(intent: Intent?) {
    if (intent!!.extras != null) {
        order_status = intent.extras!!.getString("Id", "")
        if (order_status.length == 6) {
            Toast.makeText(
                this,
                "ID :- "   intent.extras!!.getString("Id", ""),
                Toast.LENGTH_LONG
            )
                .show()
      }
     }
    }
    
  3. It will help to get data from notifications in terminated and background situations.

  4. In my case i will get data into splash activity.

  • Related