I've implemented Firebase in my app and I'm sending push with extra data. When my app is in foreground I'm handling data properly and showing my own notification, but I have a problem with fetching data, when Firebase shows Notification "automatically" when app was "homed" (not killed). According DOCS Activity
should get new Intent
with extras fulfilled with my values, instead app just get back to front, old state is restored.
Scenario:
Notification
WITHOUT calling onMessageReceived
(according to table in docs, it should?)Intent
is fulfilled with "original" extras used for open Activity
on topI have logs in onCreate
, onNewIntent
, onResume
( Activity
) and in onMessageReceived
( Service
), only onResume
is called, in which I'm printing extras like below:
if (getIntent().getExtras() != null) {
for (String key : getIntent().getExtras().keySet()) {
Object value = getIntent().getExtras().get(key);
Log.d("Activity onResume", "Key: " + key + " Value: " + value);
}
}
Messages with both notification and data payload, both background and foreground. In this case the data payload is delivered to extras of the intent of your launcher activity. If you want to get it on some other activity, you have to define click_action on the data payload. So get the intent extra in your launcher activity.
Edit:-
From the documentation :- When in the background, apps receive the notification payload in the notification tray, and only handle the data payload when the user taps on the notification.
So check the launcher activity's oncreate() method with getIntent() extras upon click of notification.
So I was looking for this answer and read the same. It has to be some where. I know this is a year old but they still haven't really made it clear. In the pendingActivity associated with the notification ( in my case it was MainActivity ) I added the following. Please note I was working through the Google Codelabs com.example.android.eggtimernotifications not that this was in the answers.
override fun onResume() {
super.onResume()
intent?.run {
val keys = this.extras?.keySet()
if (!keys.isNullOrEmpty()) {
keys.forEach { key ->
when (this.extras!![key]) {
is Long -> println("$key = ${this.getLongExtra(key, 0L)}")
is Int -> println("$key = ${this.getIntExtra(key, 0)}")
is String -> println("$key = ${this.getStringExtra(key)}")
is Boolean -> println("$key = ${this.getBooleanExtra(key, false)}")
else -> println("unkonwn Type")
}
}
}
}
}
This resulted in the following.
The data keys were cereal, from, milk and sugar.
According to documentation Cloud messaging docs message can be delivered in 3 states:
Background handling:
For 2. onMessageReceived
will be called without UI and additional handling. Later you can show something custom.
When it comes to 3. android will show notification and hold data until there is user interaction. After click it's just resuming your existing activity. Your onNewIntent
is not triggered because your launch mode is not FLAG_ACTIVITY_SINGLE_TOP
.
You should also check your logcat output for: Google Play services out of date
.
onMessageReceived() is not called when application is on background. I have this problem few months a ago. I resolve it by override handlerIntent method. But your firebase messaging library should
implementation 'com.google.firebase:firebase-messaging:10.2.1'
after that:
@Override
public void handleIntent(Intent intent) {
try
{
if (intent.getExtras() != null)
{
RemoteMessage.Builder builder = new RemoteMessage.Builder("MessagingService");
for (String key : intent.getExtras().keySet())
{
builder.addData(key, intent.getExtras().get(key).toString());
}
onMessageReceived(builder.build());
}
else
{
super.handleIntent(intent);
}
}
catch (Exception e)
{
super.handleIntent(intent);
}
}
if you don't want to downgrade your library then
You can specify a click_action to indicate the intent that should be launched when the notification is tapped by the user. The main activity is used if no click_action is specified.
When the intent is launched you can use the
getIntent().getExtras();
to retrieve a Set that would include any data sent along with the notification message.
For more on notification message see https://firebase.google.com/docs/cloud-messaging/android/receive#sample-receive
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.