代码之家  ›  专栏  ›  技术社区  ›  Connor Huggett

单击通知时显示调用本地通知的内容页

  •  0
  • Connor Huggett  · 技术社区  · 8 年前

    我对Xamarin比较陌生,在我的应用程序中,我希望收到通知,我使用本地通知向用户显示他们收到了来自我应用程序中某个人的消息。虽然我可以让通知显示,但当它单击时,它要么什么也不显示,要么“重启”应用程序(将用户带回登录页面)。

    我如何获得通知以显示设置页面,例如单击通知时的“我的联系人”页面?

    1 回复  |  直到 8 年前
        1
  •  2
  •   Elvis Xia - MSFT    8 年前

    我如何获得通知以显示设置页面,例如单击通知时的“我的联系人”页面?

    Xamarin.Forms 只有一个活动,无论你创建了多少个页面,它们都是在上面创建的 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 我们使用反射设置应用程序的 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的 App ,如果您有此属性的用法,请正确修改逻辑。