'Load Fragment on FCM notification click using intent
i'm using fcm data only message. I made some Flow to send user to destination fragment as fcm notification clicked
this is my flow
first) when the fcm message received I add pendingIntent that hold informations about destination fragment to notification builder
second) when notification clicked, it sends user to login activity to check whether user in login state or not
third) if user in login state, I send user to the destination fragment with the informations received from notification pendingIntent (MainActivity -> Fragment)
it works at first shot, but later on even though the destination is changed it only sends the user to the first destination fragment.
my log says that the login activity only receive first destination information but, why? please help me
// this is onMessageReceived, adding pendingIntent
private fun addPendingIntent(
builder: NotificationCompat.Builder, pushIdx: Int, data: Map<String, String>) {
val loginIntent = Intent({“LoginActivitys intent filter name”}).apply {
flags = Intent.FLAG_ACTIVITY_CLEAR_TASK
putExtra("pushType", data["pushType"])
// the pushType indicate destination fragment
// in this log pushType is change as push is change
Log.e("pushType", "${data["pushType"]}")
…
}
val pendingIntent = PendingIntent.getActivity(
applicationContext,
pushIdx,
loginIntent,
PendingIntent.FLAG_MUTABLE
)
builder.setContentIntent(pendingIntent)
}
// this is login activity
if(isLogin) {
val intent = Intent(applicationContext, MainActivity::class.java)
retrieveAndSetExtras(intent)
startActivity(intent)
viewModel.resetLoginState()
this.finish()
}
private fun retrieveAndSetExtras(sendingIntent: Intent) {
// this log always print first pushType
Log.e("LOGIN", "retrieveAndSetExtras(${this.intent.getStringExtra("pushType")})")
this.intent.getStringExtra("pushType")?.let { pushType ->
sendingIntent.putExtra("pushType", pushType)
this.intent.removeExtra("pushType")
…
}
}
Solution 1:[1]
it is because i didn't override onNewIntent callback
the LoginActivity should look like...
class LoginActivity: AppCompatActivity() {
...
private var newIntentFlag = false
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
// init viewBinding, viewModel...
setContentView(binding.root)
subscribeLiveData()
if (!newIntentFlag) {
viewModel.checkLoginState()
}
}
override fun onNewIntent(newIntent: Intent?) {
super.onNewIntent(newIntent)
this.intent = newIntent
newIntentFlag = true
}
override fun onResume() {
super.onResume()
if (newIntentFlag) {
viewModel.checkLoginState()
newIntentFlag = false
}
}
private fun subscribeLiveData() {
// here when login state is OK -> handle intentExtras
...
}
}
The reason I created the newIntentFlag branch in the onResume callback is because of the domain-specific characteristics. so, it is not general example
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | timothy jeong |
