我目前正在一個項目中,我收到設備上的推送通知,並且在貼上通知時應該打開一個特定的頁面。Xamarin表單PushAsync打開一個空白頁面
我想它在Android 5.1,但是當我點擊該通知,它首先打開的頁面並且之後它會打開一個空白頁,任何內部消除導航欄,我不明白爲什麼...
這裏是我的代碼:
當我收到Android上的消息我執行以下操作:
Intent intent = new Intent(this, typeof(MainActivity));
intent.PutExtra("key", "message");
PendingIntent pendingIntent = PendingIntent.GetActivity(this, 0, intent, PendingIntentFlags.OneShot);
Notification notif = new Notification.Builder(this)
.SetSmallIcon(Resource.Drawable.icon)
.SetContentTitle("Alert")
.SetContentText("alert message")
.SetAutoCancel(true)
.SetDefaults(NotificationDefaults.Sound | NotificationDefaults.Vibrate)
.SetContentIntent(pendingIntent)
.SetPriority((int)NotificationPriority.High)
.Build();
NotificationManager notificationManager = (NotificationManager)GetSystemService(Context.NotificationService);
notificationManager.Notify(0, notif);
當錄音它打開其執行以下操作在MainActivity:
protected override void OnCreate(Bundle bundle)
{
TabLayoutResource = Resource.Layout.Tabbar;
ToolbarResource = Resource.Layout.Toolbar;
base.OnCreate(bundle);
global::Xamarin.Forms.Forms.Init(this, bundle);
if (app == null)
app = new App();
LoadApplication(app);
// If the user tapped a notification
if (Intent.Extras != null)
{
Data data = JsonConvert.DeserializeObject<Data>(Intent.Extras.GetString("key"));
MessagingCenter.Send<Data>(data, "Show data");
}
}
最後我有做這樣的數據頁(套作爲應用程序的的MainPage,一個NavigationPage內):
public DataPage()
{
MessagingCenter.Subscribe<Data>(this, "Show data", (sender) =>
{
await Navigation.PushAsync(new DataDetail(sender));
});
}
我真的不明白爲什麼這不能正常工作...... 特別是因爲如果我這樣做
await Xamarin.Forms.Application.Current.MainPage.Navigation.PushModalAsync(new NavigationPage(new DataPage(data)));
而創建的數據頁的第二構造函數:
DataPage(Data data)
{
await Navigation.PushAsync(new DataDetail(data));
}
它工作正常。 (但我不喜歡這樣做,看起來很......不好聽)
哦對不起我做了一個錯字。 ..訂閱消息中心消息「顯示數據」的類是DataPage,它是項目的MainPage。 我在構造函數中犯了一個錯誤,它應該是 public DataPage() { MessagingCenter.Subscribe(...); } 而且因爲我的數據頁的IS的MainPage,裹在NavigationPage這樣 公共應用程序(){ ... =的MainPage新NavigationPage(新數據頁); .... } 它看起來像你在那裏展示的,但它不能正常工作 – Alhyoss
所以要清楚:我的MainPage是DataPage,它被包裹在一個NavigationPage中並顯示一個綁定到數據列表的ListView(但它似乎與問題無關)。然後DataDetail是一個頁面,詳細顯示了一個特定的數據,就像你說的一個模型類一樣。 因此,DataPage()中的await導航對我來說似乎是合法的。 – Alhyoss
@Simon,是的,但是你的消息中心發送消息給'Data'類,在那個類中,應該沒有'Navigation'屬性。這就是爲什麼我在'MainPage'中使用了一個'Navigation'實例。因此,您的要求是從通知中獲取數據並將其顯示在「DataPage」中,並且您的數據頁面是「NavigationPage」的根頁面,對嗎? 'DataDetail'在這裏是不相關的? –