单击通知时显示调用本地通知的内容页面 [英] Show contentpage that called local notification when notification is clicked

查看:35
本文介绍了单击通知时显示调用本地通知的内容页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm relatively new to Xamarin and have gotten to a point in my application where I want to have notifications, I'm using local notifications to show the user that they have received a message from someone in my application. While I can get the notification to show, when it clicks it either shows nothing or it "restarts" the application (takes the user back to the login page).

如何在点击通知时获取通知以显示设置页面,例如我的联系人页面?

How do I get the notification to show a set page, such as my contacts page when the notification is clicked on?

推荐答案

如何在点击通知时获取通知以显示设置页面,例如我的联系人页面?

How do I get the notification to show a set page, such as my contacts page when the notification is clicked on?

Xamarin.Forms 只有一个 Activity,无论你创建了多少个页面,它们都是在 MainActivity 上创建的.

Xamarin.Forms only has one activity, no matter how many pages you created, they are created on MainActivity.

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

But still we can have a workaround to work it out:

  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());

  • MainActivity.cs中的OnCreate中,我们使用反射来设置App的MainPage:

  • In MainActivity.cs inside OnCreate we use reflection to set the App's 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,如果您有使用此属性,请正确修改逻辑.

    Notes: this workaround modifies the MainPage of your PCL's App, if you have usage for this property, please modify the logic properly.

    这篇关于单击通知时显示调用本地通知的内容页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆