I am using Plugin.FirebasePushNotification to recieve push notifications in my xamarin cross platform application.
In android which i started to test first....
If if recieve a notification and app in foreground --> on tapping the notification the app's previous state of the app is retained and you see the last opened tabpage.
But if the app is in background and I click on the notification .... the onstart method is executed and app restarts to the login screen and the previous state of the app is lost.
The app works fine and fires onresume if it is in background and if we click on icon from home screen etc.
Why onresume is not fired and onstart is fired when my app is in background and I click on the notification to open.
I am new to xamarn so is this something very silly I am missing?
UPDATE: I just found that when mainlauncher=true is splashactivity then this happens.If mainlauncher=true for mainactivity then onnotificationopened is followed by onresume and app resumes previous state and does not start over from login.
what should I do ?
@devxus after your show the splash activity, did you finish splash activity?
Did you use this PendingIntent to open your application after clicking the notification?
var manager = (NotificationManager)context.GetSystemService(Context.NotificationService); var intent =context.PackageManager.GetLaunchIntentForPackage(context.PackageName); intent.AddFlags(ActivityFlags.ClearTop); var pendingIntent = PendingIntent.GetActivity(context, 0, intent, PendingIntentFlags.UpdateCurrent);
If so, you will open the applcation at every time, splash activity was destroyed, Android will create a splash activity to push top of the TaskRecord. the next activity is mainactivity, the default lanuchMode is standard. android will create a new mainactivity(by StartActvity(intent)).
Answers
@devxus after your show the splash activity, did you finish splash activity?
Did you use this PendingIntent to open your application after clicking the notification?
If so, you will open the applcation at every time, splash activity was destroyed, Android will create a splash activity to push top of the TaskRecord. the next activity is mainactivity, the default lanuchMode is standard. android will create a new mainactivity(by StartActvity(intent)).
I am sorry I am new to xamarin so a little lost and not able to follow.
I do not need the contents of the notification since I refresh the screen for fresh data onresume on recieving notification.
So do i need pending intent?
Also, the app resumes fine to the previous state if I put it in the background and return to it by clicking the app icon from home screen OR if I recieve the notification when app is in the foreground and then I click on the notification the onresume is fired and loads the respective tab page.
I have the onnotificationopened code in app.cs in my xamarin cross platform porject.
Splashactivity:
[Activity(Label = "Supervisor", MainLauncher =true, Theme ="@style/ThemeSplash", NoHistory =true )]
public class SplashActivity : AppCompatActivity
{
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
var mainIntent = new Intent(Application.Context, typeof(MainActivity));
if (Intent.Extras != null)
{
mainIntent.PutExtras(Intent.Extras);
}
mainIntent.SetFlags(ActivityFlags.SingleTop);
StartActivity(mainIntent);
}
Below is Mainactivity:
I managed to make the mainactivity as my mainlauncher with the theme of splash activity set and oncreate of the mainacitivity I change the theme back to the app theme .
Now the push notifications retain the state when got to foreground from background.
Thank you for your reply.
But I do want to know what you meant in your answer.