如何在不重新启动 Xamarin 中的应用程序的情况下导航到推送通知点击的其他页面?

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

我需要在点击推送通知时导航到新页面。我不想重新启动页面。这意味着,当我的应用程序已经在前台时,我只想导航到一个新页面(我需要导航栏中的后退图标)。但是,目前只能加载清除我现有导航堆栈并重新加载应用程序的应用程序。

下面是我在 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());
    }
}

下面是我的 MainActivity 代码。

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));
    }
}

下面是我的 App.cs 文件代码。

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());
    } 
}

注意:我已经在 iOS 中通过从渲染器调用 NavigateToReceiveNotificationPage 方法实现了这个要求。而在 Android 中,由于主要 activity 本身重新加载,我不确定如何实现此要求。

您可以使用 Plugin.FirebasePushNotification 并且可以在 App.xaml.cs 中挂钩:

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

您可以将 MainActivity LaunchMode 设置为 SingleTop,然后在 OnNewIntent 方法中处理它,

[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

你可以参考

更新:

将数据放入 Intent :

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