Forum Xamarin.Android

Why does my app lose intent data in Android when app is in background?

GarrettDeMeyerGarrettDeMeyer USUniversity ✭✭

I have a Xamarin.Forms app with Android specific code to load an intent on notification selection. When the app is open and in the foreground, tapping the notification brings all of the intent extras to the activity as desired. When the app is in the background, all of the extras are lost. Here are some samples:

In my listener service:

void SendNotification(int icon, string title, string message)
            {
                var intent = new Intent(this, typeof(MainActivity));
                intent.PutExtra("extra", "value");
                intent.AddFlags(ActivityFlags.SingleTop);
                intent.AddFlags(ActivityFlags.ClearTop);
                var pendingIntent = PendingIntent.GetActivity(this, new Random().Next(), intent, PendingIntentFlags.UpdateCurrent);

                var notificationBuilder = new Notification.Builder(this)
                    //.SetSmallIcon(Resource.Drawable.ic_stat_ic_notification)
                    .SetSmallIcon(icon)
                    .SetContentTitle(title)
                    .SetContentText(message)
                    .SetAutoCancel(true)
                    .SetContentIntent(pendingIntent);

                var notificationManager = (NotificationManager)GetSystemService(Context.NotificationService);
                notificationManager.Notify(0, notificationBuilder.Build());
            }

In my MainActivity.cs

[Activity(Label = "MyApp",  MainLauncher = true, ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation,Theme = "@android:style/Theme.Holo", LaunchMode = LaunchMode.SingleTask)]
    public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsApplicationActivity
    {
        private Intent myIntent;
        protected override void OnCreate(Bundle bundle)
        {
            string extra= "";
            if ((myIntent ?? Intent).HasExtra("extra")) extra= (myIntent ?? Intent).GetStringExtra("extra");
            //...
        }

        protected override void OnNewIntent(Intent intent)
        {
            base.OnNewIntent(intent);

            myIntent = intent;
        }
    }

I have noticed that OnNewIntent the extras are not there if the app was in the background. Additionally, OnNewIntent does not seem to fire before OnCreate, but rather instead of it. This could easily be worked around, however, if my intent had the extras I expect.

Answers

  • TamilSelvanRTamilSelvanR USMember ✭✭

    Hi Garrett DeMeyer,

    I got an same issue. Did you find any fix for this issue.

    Thanks,

  • FernandoOttFernandoOtt USMember

    Try to run this on release mode.
    For some reason, remote android notifications doesn't work on debug mode.

Sign In or Register to comment.