2017-07-06 54 views
0

我想從Android通知中加載Xamarin Forms頁面。我真的迷失在這個android的一面,我的大部分代碼都是從教程和博客文章拼湊而成的。然而,似乎沒有真正涵蓋這種情況。從android通知中加載Xamarin Forms頁面

我實現了一個非常簡單的接口與依賴服務來創建一個意向通知...

[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項目

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的

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。目前,我只是迷失在這個錯誤的地方?!

任何幫助將非常感激,在此先感謝。

+0

嘗試使用頁面渲染器https://developer.xamarin.com/guides/xamarin-forms/application-fundamentals/custom-renderer/contentpage/ – Pratik

+0

我不知道如何從Android加載Xamarin表單頁面,這似乎去另一種方式來加載在窗體中的Android特定頁面?除非我錯過了什麼? – Chrisco

回答

0

問題與您的代碼Intent LoadPostPage = new Intent(Forms.Context, typeof(DroidToForms));

對於XF項目的Android平臺,它只有一個Activity,即MainActivity,或XF的頁面根據此MainActivity呈現。

所以,你可能會改變這樣你的代碼:

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

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

然後:

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

最後,作爲@Pratik建議,如果你想創建一個本地頁面/視圖爲Android平臺,請使用PageRendererViewRenderer這樣做。

+0

謝謝你,這似乎是我在別處讀過的建議,我已經嘗試過了。它似乎每次啓動時都會啓動PostPage,這是不可取的。我已經在主要活動中使用OnNewIntent了。 – Chrisco

+0

@Chrisco,「它似乎每次啓動時都會啓動PostPage,這是不可取的。」問題是,對於XF或頁面基於MainActivity,您可以根據通知中的額外內容導航到其他頁面,但不建議從'MainActivity'指定新的活動。 –

+0

謝謝,我已經採取了這一點,我的最新代碼現在基於'MainActivity'。 – Chrisco

1

對於任何絆倒在這個我結束了在MainActivity使用OnNewIntent

這裏存在Processing notifications in Xamarin Forms Android

一些優秀的信息,我的代碼最終看上去像這樣。

呼叫的依賴服務獲取Android implmentation

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

其中,意圖建立通知:

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

然後在MainActivity覆蓋OnNewIntent。如果意圖沒有被我的QuoteNotification類內置然後發送-1的帖子ID

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

然後在App.Xaml.cs添加一些邏輯導航到正確的頁面

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

感謝,希望它可以幫助別人。

+0

嗨,我已經嘗試了上面的代碼它的工作'很好,但我有問題,而用戶收到多種類型的通知。把額外的只存儲最新的值例如首先我保存type1和第二次通知我保存了type2。當我點擊type1通知,但它的意圖額外值顯示在'OnNewIntent'方法的type2。請提出任何想法。 – Deepak

相關問題