Load Xamarin Forms page from android notification

前端 未结 2 1495
南方客
南方客 2021-01-24 12:37

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

相关标签:
2条回答
  • 2021-01-24 13:00

    For anyone that stumbles on this I ended up using OnNewIntent in MainActivity.

    there is some excellent information here Processing notifications in Xamarin Forms Android

    My Code ended up looking like this.

    Call the dependency service to get the android implmentation

    DependencyService.Get<IQuoteNotification>().Notify("words",SelectedQuote.FixedTitle  , SelectedQuote.Id);
    

    Which builds notification with the Intent:

      class QuoteNotification : IQuoteNotification
        {
    
    
            public void Notify(string title, string message, int postIndex)
            {
    
                //type needs to be derived from java, not xamarin.forms
                Intent LoadPostPage = new Intent(Forms.Context, typeof(MainActivity));
                LoadPostPage.PutExtra("NotificationTrue", true);
                LoadPostPage.PutExtra("PostID", postIndex);
                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);
            }
        }
    

    then Override OnNewIntent in MainActivity. If the intent is not Built by my QuoteNotification class then send -1 as PostID

            protected override void OnNewIntent(Intent intent)
        {
            if (intent.HasExtra("NotificationTrue")){
                LoadApplication(new App(intent.GetIntExtra("PostID", -1)));
                //Xamarin.Forms.Application.Current.MainPage.Navigation.PushAsync(new QuoteApp.PostPage());
            }
            base.OnNewIntent(intent);
        }
    

    Then in App.Xaml.cs add some logic to navigate to the correct page

    public partial class App : Application
    {
        public App(int PostId = -1)
        {
            InitializeComponent();
    
            if (PostId >= 0)
            {
                MainPage = new NavigationPage(new PostPage(PostId));
            }
            else
                MainPage = new NavigationPage(new MainPage());
    

    Thanks, hope it helps someone.

    0 讨论(0)
  • 2021-01-24 13:10

    Problem is with your code Intent LoadPostPage = new Intent(Forms.Context, typeof(DroidToForms));.

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

    Then in your OnCreate method of MainActivity:

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

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

    0 讨论(0)
提交回复
热议问题