我如何获得通知以显示设置页面,例如单击通知时的“我的联系人”页面?
Xamarin.Forms
只有一个活动,无论你创建了多少个页面,它们都是在上面创建的
MainActivity
.
但我们仍然可以找到解决方法:
-
创建本地通知,以指示要导航到的页面(
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());
-
在里面
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
,如果您有此属性的用法,请正确修改逻辑。