Harikrishnan
Harikrishnan

Reputation: 1474

How to navigate to other pages on push notification tap without relauching app in Xamarin?

I need to navigate to a new page when tapping on the push notification. I do not want to relaunch the page. Which means, when my application is already in the foreground, I just want to navigate to a new page(I need the back icon in the navigation bar). However, currently am able to only load the application which clears my existing navigation stack and reloads the application.

Below is my code in the FirebaseMessagingService class.

public class MyFirebaseMessagingService : FirebaseMessagingService
{
    public override void OnMessageReceived(RemoteMessage message)
    {
        NotificationManager manager = (NotificationManager)GetSystemService(NotificationService);
        var push = new Intent(this, typeof(PushActivity));
        push.PutExtra("NotificationId", DateTime.Now.ToString());
        push.AddFlags(ActivityFlags.ClearTop);

        var fullScreenPendingIntent = PendingIntent.GetActivity(this, 0, push, PendingIntentFlags.CancelCurrent);
        NotificationCompat.Builder notification = new NotificationCompat.Builder(this);

        var seed = Convert.ToInt32(Regex.Match(Guid.NewGuid().ToString(), @"\d+").Value);
        int id = new Random(seed).Next(000000000, 999999999);
        notification.SetContentIntent(fullScreenPendingIntent)
            .SetContentTitle(message.GetNotification().Title)
            .SetContentText(message.GetNotification().Body);
        manager.Notify(id, notification.Build());
    }
}

Below is my MainActivity code.

public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsAppCompatActivity
{
    protected override void OnCreate(Bundle savedInstanceState)
    {
        TabLayoutResource = Resource.Layout.Tabbar;
        ToolbarResource = Resource.Layout.Toolbar;
        base.OnCreate(savedInstanceState);

        Xamarin.Essentials.Platform.Init(this, savedInstanceState);
        global::Xamarin.Forms.Forms.Init(this, savedInstanceState);
        Firebase.FirebaseApp.InitializeApp(this);

        string NotificationId = Intent.GetStringExtra("NotificationId");
        if (NotificationId == null)
            LoadApplication(new App(false));
        else
            LoadApplication(new App(true));
    }
}

Below is my App.cs file code.

public partial class App : Application
{
    private bool _isNotified;
    public App(bool isNotified)
    {
        InitializeComponent();
        this._isNotified = isNotified;
        if (_isNotified)
            MainPage = new NavigationPage(new ReceiveNotification());
        else
            MainPage = new NavigationPage(new MainPage());
        this._isNotified = false;
    }

    public static void NavigateToReceiveNotificationPage()
    {
        App.Current.MainPage.Navigation.PushAsync(new ReceiveNotification());
    } 
}

Note: I have already achieved this requirement in the iOS by calling the NavigateToReceiveNotificationPage method from the renderer. Whereas in Android since the main activity itself reloads am not sure how to achieve this requirement.

Upvotes: 0

Views: 1366

Answers (2)

Leo Zhu
Leo Zhu

Reputation: 14956

you could set your MainActivity LaunchMode to SingleTop,then handle it in OnNewIntent method,

[Activity(Label = "App18", Theme = "@style/MainTheme", MainLauncher = true, ConfigurationChanges = ConfigChanges.ScreenSize,LaunchMode = LaunchMode.SingleTop )]
public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsAppCompatActivity
{
    ...
  
    protected override void OnNewIntent(Intent intent)
    {
        base.OnNewIntent(intent);
        string NotificationId = intent.Extras.GetString("NotificationId");
        //do something you want to do (according to your own needs,you also could use MessagingCenter to do what you want in forms page)
        Xamarin.Forms.Application.Current.MainPage.Navigation.PushAsync(new Page());
    }

   ...
}

MessagingCenter

you could refer to similar case

Update:

put the data to Intent :

var push = new Intent(this, typeof(PushActivity));
var bundleForPush = new Bundle();
bundleForPush.PutString("NotificationId", DateTime.Now.ToString());
push.PutExtras(bundleForPush);

Upvotes: 2

sermet
sermet

Reputation: 442

You could use Plugin.FirebasePushNotification and you can hook this in App.xaml.cs:

CrossFirebasePushNotification.Current.OnNotificationOpened += async (s, p) =>{};

Upvotes: 1

Related Questions