从 android 通知加载 Xamarin Forms 页面 [英] Load Xamarin Forms page from android notification

查看:24
本文介绍了从 android 通知加载 Xamarin Forms 页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从 Android 通知加载 Xamarin 表单页面.我真的迷失在这方面的 android 方面,我的大部分代码都是从教程和博客文章中拼凑起来的.然而,似乎没有什么能真正涵盖这种情况.

I am trying to load a Xamarin Forms page from an Android Notification. I am really lost on the android side of this, and most of my code has been pieced together from tutorials and blog posts. However nothing really seems to cover exactly this scenario.

我已经用依赖服务实现了一个非常简单的接口来创建具有意图的通知......

I have implemented a very simple interface with dependency service to create the notification with an intent...

[assembly: Xamarin.Forms.Dependency(typeof(QuoteNotification))]

namespace QuoteApp.Droid
{

    class QuoteNotification : IQuoteNotification
    {


        public void Notify(string title, string message)
        {

            //type needs to be derived from java, not xamarin.forms
            Intent LoadPostPage = new Intent(Forms.Context, typeof(DroidToForms));
            const int pendingIntentId = 0;
            PendingIntent pendingIntent =
                PendingIntent.GetActivity(Forms.Context, pendingIntentId, LoadPostPage, PendingIntentFlags.OneShot);

            Notification.Builder builder = new Notification.Builder(Forms.Context)
                .SetContentIntent(pendingIntent)
                .SetAutoCancel(true)

                     .SetContentTitle(title)
                     .SetContentText(message)
                     .SetSmallIcon(Resource.Drawable.icon);

            // Build the notification:
            Notification notification = builder.Build();

            // Get the notification manager:
            NotificationManager notificationManager =
                Forms.Context.GetSystemService(Context.NotificationService) as NotificationManager;

            // Publish the notification:
            const int notificationId = 0;
            notificationManager.Notify(notificationId, notification);
        }
    }
}

这称为活动 -

namespace QuoteApp.Droid
{
    [Activity(Label = "QuoteApp", MainLauncher = true)]
    class DroidToForms : Activity
    {
        protected override void OnCreate(Bundle bundle)
        {
            base.OnCreate(bundle);

            StartActivity(typeof(XFPostPage));
            //Xamarin.Forms.Application.Current.MainPage.Navigation.PushAsync(new QuoteApp.PostPage());

        }
    }
}

依次调用这个表单页面,这里的所有代码都在 Xamarin.Android 项目中

which in turn calls this forms page, all the code here has been in Xamarin.Android project

namespace QuoteApp.Droid
{



        /// <summary>
        /// This is a Xamarin.Forms screen. It MUST:
        /// * inherit from ANdroidActivity
        /// * call Forms.Init()
        /// * use LoadApplication()
        /// </summary>
        [Activity(Label = "XFPostPage", MainLauncher = true) ]
        public class XFPostPage : Xamarin.Forms.Platform.Android.FormsApplicationActivity
        {
            protected override void OnCreate(Bundle bundle)
            {
                base.OnCreate(bundle);

                Xamarin.Forms.Forms.Init(this, bundle);

            //LoadApplication( new App() ); //how do i send to PostPage.xaml ??

            Xamarin.Forms.Application.Current.MainPage.Navigation.PushAsync(new QuoteApp.PostPage());
        }
        }
    }

这会在 logcat 中出现以下错误

this gives the following error in logcat

07-04 17:51:44.494  7668  7668 E AndroidRuntime: Caused by: android.runtime.JavaProxyThrowable: System.NullReferenceException: Object reference not set to an instance of an object
07-04 17:51:44.494  7668  7668 E AndroidRuntime:   at QuoteApp.Droid.XFPostPage.OnCreate (Android.OS.Bundle bundle) [0x00016] in <ab64801c0fb24acb8457c1d1202cc143>:0
07-04 17:51:44.494  7668  7668 E AndroidRuntime:   at Android.App.Activity.n_OnCreate_Landroid_os_Bundle_ (System.IntPtr jnienv, System.IntPtr native__this, System.IntPtr native_savedInstanceState) [0x0000f] in <d855bac285f44dda8a0d8510b679b1e2>:0
07-04 17:51:44.494  7668  7668 E AndroidRuntime:   at (wrapper dynamic-method) System.Object:2780fa42-5931-40d3-8d14-a642f1a3025c (intptr,intptr,intptr)
07-04 17:51:44.494  7668  7668 E AndroidRuntime:        at md543d03747be5a5b03a21a2a23b8ba191a.XFPostPage.n_onCreate(Native Method)
07-04 17:51:44.494  7668  7668 E AndroidRuntime:        at md543d03747be5a5b03a21a2a23b8ba191a.XFPostPage.onCreate(XFPostPage.java:29)
07-04 17:51:44.494  7668  7668 E AndroidRuntime:        at android.app.Activity.performCreate(Activity.java:6237)
07-04 17:51:44.494  7668  7668 E AndroidRuntime:        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)
07-04 17:51:44.494  7668  7668 E AndroidRuntime:        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369)
07-04 17:51:44.494  7668  7668 E AndroidRuntime:        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
07-04 17:51:44.494  7668  7668 E AndroidRuntime:        at android.app.ActivityThread.-wrap11(ActivityThread.java)
07-04 17:51:44.494  7668  7668 E AndroidRuntime:        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
07-04 17:51:44.494  7668  7668 E AndroidRuntime:        at android.os.Handler.dispatchMessage(Handler.java:102)
07-04 17:51:44.494  7668  7668 E AndroidRuntime:        at android.os.Looper.loop(Looper.java:148)
07-04 17:51:44.494  7668  7668 E AndroidRuntime:        at android.app.ActivityThread.main(ActivityThread.java:5417)
07-04 17:51:44.494  7668  7668 E AndroidRuntime:        ... 3 more

我尝试了很多不同的想法,但都有不同的运行时错误,我对 Xamarin.Android 工作的理解非常有限.最终我将使用带有 AlarmManager 的通知,更多信息将发送到 PostPage.Xaml.目前我只是迷失在这到底是哪里出了问题?!

I have tried a bunch of different ideas all with different runtime errors, my understanding of the Xamarin.Android workings are very limited. eventually i will be using the notification with AlarmManager and further information will be sent to PostPage.Xaml. At the moment i'm just lost at exactly where this is going wrong though?!

任何帮助将不胜感激,提前致谢.

Any Help would be hugely appreciated, thanks in advance.

推荐答案

问题在于你的代码 Intent LoadPostPage = new Intent(Forms.Context, typeof(DroidToForms));.

对于XF项目的Android平台,它只有一个Activity,即MainActivity,或者基于这个MainActivity渲染XF的页面>.

For Android platform of XF project, it only has one Activity, which is MainActivity, or pages of XF are rendered based on this MainActivity.

所以你可以像这样改变你的代码:

So you may change you code like this:

Intent intent = new Intent(context, typeof(MainActivity));
intent.PutExtras(valuesForActivity);

PendingIntent resultPendingIntent = PendingIntent.GetActivity(Xamarin.Forms.Forms.Context, 0, intent, PendingIntentFlags.OneShot);

然后在 MainActivityOnCreate 方法中:

Then in your OnCreate method of MainActivity:

//navigate to the page of PCL
Xamarin.Forms.Application.Current.MainPage.Navigation.PushAsync(new PostPage());

最后,正如@Pratik 所建议的,如果您想为 Android 平台创建本机页面/视图,请使用 PageRendererViewRenderer 这样做.

At last, as @Pratik suggested, if you want to create a native page/ view for Android platform, use PageRenderer or ViewRenderer to do so.

这篇关于从 android 通知加载 Xamarin Forms 页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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