【问题标题】:Show contentpage that called local notification when notification is clicked单击通知时显示调用本地通知的内容页面
【发布时间】:2018-05-07 16:26:22
【问题描述】:

我对 Xamarin 比较陌生,并且已经在我的应用程序中达到了我想要收到通知的地步,我正在使用本地通知向用户显示他们收到了来自我的应用程序中某人的消息。虽然我可以显示通知,但当它单击时,它要么什么都不显示,要么“重新启动”应用程序(将用户带回登录页面)。

当点击通知时如何让通知显示设置页面,例如我的联系人页面?

【问题讨论】:

    标签: c# xamarin xamarin.android portable-class-library


    【解决方案1】:

    当点击通知时如何让通知显示设置页面,例如我的联系人页面?

    Xamarin.Forms只有一个activity,不管你创建了多少页面,都是在MainActivity上创建的。

    但我们仍然可以找到解决方法:

    1. 创建本地通知,以指示您要导航到哪个页面(Page1):

      Notification.Builder builder = new Notification.Builder(Xamarin.Forms.Forms.Context);
      Intent intent = new Intent(Xamarin.Forms.Forms.Context, typeof(MainActivity));
      Bundle bundle = new Bundle();
      // if we want to navigate to Page1:
      bundle.PutString("pageName", "Page1");
      intent.PutExtras(bundle);
      PendingIntent pIntent = PendingIntent.GetActivity(Xamarin.Forms.Forms.Context, 0, intent, 0);
      builder.SetContentTitle(title)
             .SetContentText(text)
             .SetSmallIcon(Resource.Drawable.icon)
             .SetContentIntent(pIntent);
      var manager = (NotificationManager)Xamarin.Forms.Forms.Context.GetSystemService("notification");
          manager.Notify(1, builder.Build());
      
    2. MainActivity.cs里面OnCreate我们使用反射来设置App的MainPage

      protected override void OnCreate(Bundle bundle)
      {
          TabLayoutResource = Resource.Layout.Tabbar;
          ToolbarResource = Resource.Layout.Toolbar;
      
          base.OnCreate(bundle);
      
          global::Xamarin.Forms.Forms.Init(this, bundle);
          var myApp = new App();
          var mBundle = Intent.Extras;
          if (mBundle != null)
          {
              var pageName = mBundle.GetString("pageName");
              if (!string.IsNullOrEmpty(pageName))
              {
                  //get the assemblyQualifiedName of page
                  var pageAssemblyName = "Your_PCL_Name." + pageName+",Your_PCL_Name";
                  Type type = Type.GetType(pageAssemblyName);
                  if (type != null)
                  {
                      var currentPage = Activator.CreateInstance(type);
                      //set the main page
                      myApp.MainPage = (Page)currentPage;
                  }
      
              }
          }
      
          //load myApp
          LoadApplication(myApp);
      }
      

    注意:这个变通方法修改了你PCL的AppMainPage,如果你有这个属性的用法,请适当修改逻辑。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-10-25
      • 1970-01-01
      • 2015-04-11
      • 1970-01-01
      • 2023-02-06
      • 2022-01-21
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多