question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Application reloaded on tapping notification

See original GitHub issue

When Im using splash activity, on notification tapped event my application is restarted, On my App.cs, OnLocalNotificationTapped is fired:

private void OnLocalNotificationTapped(NotificationTappedEventArgs e) { return; }

And this is my splash activity:

[Activity(Theme = "@style/MainTheme.Splash",
              MainLauncher = true,
              NoHistory = true)]
    public class SplashActivity : AppCompatActivity
    {
        // Launches the startup task
        protected override void OnResume()
        {
            base.OnResume();
        }
        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);
        }
    }

And my MainActivity:

[Activity(Theme = "@style/MainTheme", ScreenOrientation = Android.Content.PM.ScreenOrientation.Locked, LaunchMode = LaunchMode.SingleTop,
              ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation | ConfigChanges.UiMode | ConfigChanges.ScreenLayout | ConfigChanges.SmallestScreenSize)]
    public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsAppCompatActivity
    {
        Intent serviceIntent;
        private const int RequestCode = 5469;
        protected override void OnCreate(Bundle savedInstanceState)
        {
            TabLayoutResource = Resource.Layout.Tabbar;
            ToolbarResource = Resource.Layout.Toolbar;
            base.OnCreate(savedInstanceState);
            if (CheckSelfPermission(Manifest.Permission.AccessCoarseLocation) != (int)Permission.Granted)
            {
                RequestPermissions(new string[] { Manifest.Permission.AccessCoarseLocation, Manifest.Permission.AccessFineLocation }, 0);
            }
            Xamarin.FormsGoogleMaps.Init(this, savedInstanceState);
            Xamarin.Essentials.Platform.Init(this, savedInstanceState);
            FFImageLoading.Forms.Platform.CachedImageRenderer.Init(enableFastRenderer: true);
            global::Xamarin.Forms.Forms.Init(this, savedInstanceState);
            global::Rg.Plugins.Popup.Popup.Init(this, savedInstanceState);
            serviceIntent = new Intent(this, typeof(AndroidLocationService));
            SetServiceMethods();
            NotificationCenter.CreateNotificationChannel();
            LoadApplication(new App(new AndroidInitializer()));
            NotificationCenter.NotifyNotificationTapped(Intent);
            RaygunPlatform.Configure(this);
        }
        protected override void OnNewIntent(Intent intent)
        {
            NotificationCenter.NotifyNotificationTapped(intent);
            base.OnNewIntent(intent);
        }
        void SetServiceMethods()
        {
            MessagingCenter.Subscribe<StartServiceMessage>(this, "ServiceStarted", message => {
                if (!IsServiceRunning(typeof(AndroidLocationService)))
                {
                    if (Android.OS.Build.VERSION.SdkInt >= Android.OS.BuildVersionCodes.O)
                    {
                        StartForegroundService(serviceIntent);
                    }
                    else
                    {
                        StartService(serviceIntent);
                    }
                }
            });
            MessagingCenter.Subscribe<StopServiceMessage>(this, "ServiceStopped", message => {
                if (IsServiceRunning(typeof(AndroidLocationService)))
                    StopService(serviceIntent);
            });
        }
        private bool IsServiceRunning(System.Type cls)
        {
            ActivityManager manager = (ActivityManager)GetSystemService(Context.ActivityService);
            foreach (var service in manager.GetRunningServices(int.MaxValue))
            {
                if (service.Service.ClassName.Equals(Java.Lang.Class.FromType(cls).CanonicalName))
                {
                    return true;
                }
            }
            return false;
        }
        public override void OnRequestPermissionsResult(int requestCode, string[] permissions, Android.Content.PM.Permission[] grantResults)
        {
            Xamarin.Essentials.Platform.OnRequestPermissionsResult(requestCode, permissions, grantResults);

            base.OnRequestPermissionsResult(requestCode, permissions, grantResults);
        }
    }
    public class AndroidInitializer : IPlatformInitializer
    {
        public void RegisterTypes(IContainerRegistry containerRegistry)
        {
            // Register any platform specific implementations
        }
    }

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Reactions:3
  • Comments:7 (1 by maintainers)

github_iconTop GitHub Comments

2reactions
dejanbasiccommented, Apr 19, 2021

@Kalyxt use this code in your SplashActivity and it will work fine:

` protected override void OnCreate(Bundle savedInstanceState) { base.OnCreate(savedInstanceState); }

    // Launches the startup task
    protected override void OnResume()
    {
        base.OnResume();

        var mainIntent = new Intent(Application.Context, typeof(MainActivity));
        if (Intent.Extras != null)
        {
            mainIntent.PutExtras(Intent.Extras);
        }
        StartActivity(mainIntent);
    }

`

1reaction
Kalyxtcommented, Apr 19, 2021

@dejanbasic Thats it! Thank you. I pushed it also to the sample project.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Android app is reloaded when I tap on local push notification
My react-native android app is automatic reload when open local push-notification tap when app in foreground react-native :0.61.5 ...
Read more >
Tap on notification restart application and ...
I am using Plugin.LocalNotification to receive notification. I want to use NotificationCenter.Current.NotificationTapped event but on ...
Read more >
Start an Activity from a Notification
Tapping Back should take the user back through the app's normal work flow to the Home screen, and opening the Recents screen should...
Read more >
Use notifications on your iPhone or iPad
Tap a single notification to open the app that it's from. Tap a group of notifications to view all recent notifications from that...
Read more >
Control notifications on Android
On your phone, open the Settings app. · Tap Notifications and then Wireless emergency alerts. · Choose how often you want to receive...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found