Reputation: 21
Using FirebaseMessagingService to generate notification. After tapping on it, activity is shown, but onNewIntent() is not called. Running on 7.1.
GOAL:
After studying many posts so far i got this, what else am i missing ?
Thanks.
EDIT: added onResume() method, extras variable is not null, strings are null
Notification.Builder mBuilder =
new Notification.Builder(this)
.setSmallIcon(R.drawable.ic_icon)
.setContentTitle(title)
.setContentText(message)
.setOnlyAlertOnce(false)
.setAutoCancel(true)
.setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION))
.setVibrate(new long[] {0,400,150,400});
mBuilder.setNumber(++count);
Intent pushIntent = new Intent(this, PushMeMessageDialogActivity.class);
pushIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
pushIntent.putExtra(Constants.INTENT_TOPIC, title);
pushIntent.putExtra(Constants.INTENT_MESSAGE, message);
pushIntent.setAction(Intent.ACTION_MAIN);
pushIntent.addCategory(Intent.CATEGORY_LAUNCHER);
PendingIntent pushPendingIntent = PendingIntent.getActivity(this, count, pushIntent, PendingIntent.FLAG_UPDATE_CURRENT);
mBuilder.setContentIntent(pushPendingIntent);
NotificationManager notifyMgr = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notifyMgr.notify(count, mBuilder.build());
Activity:
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_message_dialog);
}
@Override
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
setIntent(intent);
}
@Override
protected void onResume() {
super.onResume();
Intent intent = getIntent();
Bundle extras = intent.getExtras();
if(extras == null)
return;
String strTopic = extras.getString(Constants.INTENT_TOPIC);
String strMessage = extras.getString(Constants.INTENT_MESSAGE);
}
<activity
android:name=".PushMeMessageDialogActivity"
android:launchMode="singleTop"
android:theme="@android:style/Theme.Dialog"
android:excludeFromRecents="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
minSdkVersion 22
targetSdkVersion 28
Upvotes: 1
Views: 1438
Reputation: 816
onNewIntent
will not be called every time the activity is shown, as described in the documentation. Only if the Activity
already existed it will be brought to the top of the stack and onNewIntent
will be called. If the Activity
didn't already exist the onCreate
will be called.
For most use cases this means that for singleTop
Activities
you call setIntent
in the onNewIntent
method and call getIntent
in the onResume
. That way you always process the Intent
that brought the Activity
to the top of the stack.
Upvotes: 1