【问题标题】:How to navigating to a specific page on Push Notification tap如何导航到推送通知点击的特定页面
【发布时间】:2019-04-29 07:34:37
【问题描述】:

我正在使用FirebasePushNotificationPluginXamarin.Forms 中开发firebase 推送通知。不知何故,我在Application 文件中收到了有关 android 项目的通知以及数据

CrossFirebasePushNotification.Current.OnNotificationReceived += (s, p) =>
{
    //System.Diagnostics.Debug.WriteLine("NOTIFICATION RECEIVED", p.Data);
    var data = p.Data; //Dictionary type
};

根据上述数据,除了MainPage(),我如何导航到特定的Page

这是我在共享项目中的 App.xaml.cs 文件

public App()
{
    InitializeComponent();
    MainPage = new MainPage();
}

Android项目中的MainActivity.cs类

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);
        global::Xamarin.Forms.Forms.Init(this, savedInstanceState);
        LoadApplication(new App());

        FirebasePushNotificationManager.ProcessIntent(this, Intent);
    }

    protected override void OnNewIntent(Intent intent)
    {
        base.OnNewIntent(intent);
        FirebasePushNotificationManager.ProcessIntent(this, intent);
    }
}

编辑 1: 根据@G.Hakim 回答下面的代码总是打开NotificationsPage 而不发送通知(第一次除外。只有在执行应用程序时第一次打开MainPage)。如果我点击应用程序,它总是转到NotificationsPage 而不是MainPage

MainActivity.cs

protected override void OnCreate(Bundle savedInstanceState)
{
    TabLayoutResource = Resource.Layout.Tabbar;
    ToolbarResource = Resource.Layout.Toolbar;

    base.OnCreate(savedInstanceState);
    global::Xamarin.Forms.Forms.Init(this, savedInstanceState);
    LoadApplication(new App(false,null));

    FirebasePushNotificationManager.ProcessIntent(this, Intent);
    CrossFirebasePushNotification.Current.OnNotificationOpened += (s, p) =>
    {
        System.Diagnostics.Debug.WriteLine("NOTIFICATION RECEIVED", p.Data);
        var data = p.Data;
        DependencyService.Get<IToast>().DisplayTost("opened notifications");
        LoadApplication(new App(true, p));
    };
}

App.xaml.cs

public App(bool hasNotification = false, object notificationData = null)
{
    if (hasNotification)
    {
        MainPage = new NotificationsPage();
    }
    else
    {
        MainPage = new MainPage();
    }
}

【问题讨论】:

  • 你需要自己做
  • @G.hakim - 我已经为 Xamarin.android 实现了通知,但我仍然困惑如何以某种方式做到这一点,以便它可以轻松地与 Forms 结合。如果我打开 Activity1,它与共享项目的 Page 有何关系?
  • 在 Activity1 中初始化您的 Xamarin 表单页面,然后推送到您要导航到的页面。
  • Activity初始化后如何推送页面?
  • 如果你想使用这个global::Xamarin.Forms.Forms.Init(this, savedInstanceState); LoadApplication(new App());,我可以发布一个例子!!

标签: c# firebase xamarin xamarin.forms firebase-cloud-messaging


【解决方案1】:

其实很简单,你可以用一些技巧来做到这一点:

  • 首先,从您的通知中调用 MainActivity,以便您可以使用现有的 xamarin 表单加载功能。

  • 然后在您的 App 类构造函数上维护一个 bool,如下所示:

    public App(bool hasNotification=false,object notificationData=null)
    {
       if(hasNotification)
       {//case where you receive a notification} // call your normal functionality here 
       else
       {//case where you do not recieve a notification} // notification functionality here
    }
    
  • 现在这是抢戏的技巧,当您在 MainActivity 中收到通知时,在 load app 方法中将 bool 作为 true 传递,如果通知中存在任何内容,则传递数据。像这样:

    LoadApplication(new App(true,*yourData*))
    

注意:我已经在构造函数中定义了可选参数,因此您甚至可以使用空的应用构造函数来使其工作;

更新:

private bool IsNotification=false;
private object NotificationData;
   protected override void OnCreate(Bundle savedInstanceState)
 {
   TabLayoutResource = Resource.Layout.Tabbar;
   ToolbarResource = Resource.Layout.Toolbar;

    base.OnCreate(savedInstanceState);
    global::Xamarin.Forms.Forms.Init(this, savedInstanceState);
    FirebasePushNotificationManager.ProcessIntent(this, Intent);    
    CrossFirebasePushNotification.Current.OnNotificationOpened += (s, p) =>
    {
       System.Diagnostics.Debug.WriteLine("NOTIFICATION RECEIVED", p.Data);
       NotificationData = p.Data;
       IsNotification=false;
       DependencyService.Get<IToast>().DisplayTost("opened notifications");           
    };
   if(IsNotification)
    LoadApplication(new App(IsNotification,NotificationData)); 
   else
    LoadApplication(new App());
 }

【讨论】:

  • 感谢您的回答。我会恢复的。
  • 哦,是的,还有一件事我认为您需要在加载应用程序方法之前调用此FirebasePushNotificationManager.ProcessIntent(this, Intent);
  • 当我点击应用程序图标时,您的代码总是打开 NotificationPage 而不发送通知。有关更多信息,请查看我的编辑。
  • 我已经编辑了我的答案,说明代码应该如何查看
  • 感谢您的帮助。然而这个问题与插件有关,而不是我们的代码。我已经发布了答案看看。非常感谢。
【解决方案2】:

此问题已通过从通知payload 发送color 密钥解决。

CrossFirebasePushNotification.Current.OnNotificationOpened += (s, p) =>
{
    if (p.Data.ContainsKey("color"))
    {
        Device.BeginInvokeOnMainThread(() =>
        {
            Xamarin.Forms.Application.Current.MainPage.Navigation.PushModalAsync(new NotificationsPage()
            {
                BackgroundColor = Color.FromHex($"{p.Data["color"]}")
            });
        });
    }
};

注意:以上代码在应用程序打开和后台运行时有效。不为前台工作。欲了解更多信息,请访问this ticket

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-04-07
    • 2021-11-14
    相关资源
    最近更新 更多