Forum Xamarin.Android
We are excited to announce that the Xamarin Forums are moving to the new Microsoft Q&A experience. Q&A is the home for technical questions and answers at across all products at Microsoft now including Xamarin!

We encourage you to head over to Microsoft Q&A for .NET for posting new questions and get involved today.

Viewing Push Notification on Android app restarts app

Hello folks,

I've been using Xamarin.Android to develop an app. Have been able to solve most issues but this 1 has got me and I could do with some help please.

I have push notifications working with AppCenter and Firebase Cloud Messaging. When the app is in the background a notification is received, it shows the icon at the top correctly.

However when I pull down on the icon to see the message and then tap on it, instead, bringing up the last activity (before it went into background), it shows the Login activity (ie MainLauncher = true). This is a data entry app with multiple activities and therefore this behavior will cause problems.

Is there a way to show the last activity of the app instead? I would have thought that this should have been the default behavior.

Looking around people have mentioned changing the launchmode to singleTop (I've tried all) in the manifest but that doesn't work.

<activity android:name=".DummyStartActivity" android:launchMode ="singleTop"/> 
<activity android:name=".DataEntryActivity" android:launchMode ="singleTop"/>   

Thanks

Answers

  • LeonLuLeonLu Member, Xamarin Team Xamurai

    Did you try to use following intent to open your applcation when click the notification.

    // set the package name in `GetLaunchIntentForPackage`
         Intent intent = PackageManager.GetLaunchIntentForPackage("com.companyname.app10");
         intent.SetFlags(ActivityFlags.NewTask | ActivityFlags.ResetTaskIfNeeded );
            //Flag indicating that this PendingIntent can be used only once.
            var pendingIntent = PendingIntent.GetActivity(this, 0
                                                         , intent, PendingIntentFlags.UpdateCurrent);
    

    Here is running gif.

  • cal67cal67 Member ✭✭

    Hi LeonLu

    Many thanks for your reply.

    I have tried your suggestion but it still starts on the 1st page when the app is in background. I think that is because OnMessageReceived is not called when in background.

    I am the example in

    hxxps://docs.microsoft.com/en-us/xamarin/android/data-cloud/google-messaging/remote-notifications-with-fcm?tabs=windows

    but have added a dummy start activity which you click to get onto the next page. Then I background the app and then on FCM send a test message.

    Are you able to send me the code or a link to it please so I can see what I may be missing?

    Thanks

  • LeonLuLeonLu Member, Xamarin Team Xamurai
    edited December 2019

    @cal67
    If you want to open the current activity when you click the Notification, you can achieve it by following code.

     ActivityManager activityManager = context.GetSystemService(Context.ActivityService) as ActivityManager;
     activityManager.MoveTaskToFront(MainActivity.MYTaskId, MoveTaskFlags.WithHome);
    

    Above code need persmission.

        <uses-permission android:name="android.permission.REORDER_TASKS" />
    

    Here is running GIF.

    If the reply is helpful, please donot forget to mark it as answer, it will help others who have similar issue.

    Here is my demo.

  • cal67cal67 Member ✭✭

    Hi LeonLu

    Many thanks for the trouble to do the sample app. I made a slight change in that I put the dummy page 1st and then you clicked onto the 2nd page to fire off the notification). That all works fine but that is for local notifications. I am receiving a notification from AppCenter (which may explain diferent behavior)

    So the event I have is Push.NotificationReceived and it correctly goes into this when I click on the notification. I have added the code you kindly did (see below) but unfortunately it simply starts on the 1st page (dummy start page) even though the MoveTaskToFront executes without any error.

    ActivityManager activityManager = this.GetSystemService(Context.ActivityService) as ActivityManager;

    activityManager.MoveTaskToFront(MainActivity.MYTaskId, MoveTaskFlags.WithHome);

    Thanks

  • deftbdeftb Member
    edited June 22

    HI @LeonLu , ifor the MainActivity.MYTaskId, this is not recognised by my compiler. Has something changed/ do you know how I can get the Id for my current Task?

Sign In or Register to comment.