简体   繁体   English

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

[英]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.下面是我在 FirebaseMessagingService 类中的代码。

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.下面是我的 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));
    }
}

Below is my App.cs file code.下面是我的 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());
    } 
}

Note: I have already achieved this requirement in the iOS by calling the NavigateToReceiveNotificationPage method from the renderer.注意:我已经通过从渲染器调用 NavigateToReceiveNotificationPage 方法在 iOS 中实现了这个要求。 Whereas in Android since the main activity itself reloads am not sure how to achieve this requirement.而在 Android 中,由于主要活动本身重新加载,我不确定如何实现这一要求。

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

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

you could set your MainActivity LaunchMode to SingleTop ,then handle it in OnNewIntent method,你可以设置你的MainActivity LaunchModeSingleTop,然后在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 信息中心

you could refer to similar case你可以参考类似的案例

Update :更新

put the data to Intent :将数据放入 Intent :

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM