Load Xamarin Forms page from android notification

前端 未结 2 1540
南方客
南方客 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().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.

提交回复
热议问题