我正在使用Xamarin和MvvmCross創建一個跨平臺的應用程序。MvvmCross + Android notifcation點擊獲取viewmodel爲空
在android平臺上,我需要顯示通知。
通知點擊用戶應登陸到MainView屏幕。一切都按要求運作。
當應用程序從後臺移除,然後通知到達並且用戶單擊通知,然後MainViewModel爲空時,就會出現問題。
下面是我如何顯示通知。
private void NotifyNotification(string title, string message)
{
var notificationManager = GetSystemService(Context.NotificationService) as NotificationManager;
int unique = 1;
//uiIntent
Intent uiIntent;
uiIntent = new Intent(this, typeof(MainView));
uiIntent.PutExtra("AppNotification", true);
uiIntent.AddFlags(ActivityFlags.SingleTop | ActivityFlags.ClearTop);
//pendingIntent
var pendingIntent = PendingIntent.GetActivity(this, 0, uiIntent, PendingIntentFlags.OneShot);
NotificationCompat.Builder builder = new NotificationCompat.Builder(
this);
Notification notification = builder.SetContentIntent(pendingIntent)
.SetSmallIcon(Resource.Drawable.notificationIcon).SetTicker(message)
.SetAutoCancel(true).SetContentTitle(title).SetNumber(DroidConstants.NotifyCounter)
.SetDefaults((int)NotificationDefaults.All)
.SetContentText(message).Build();
notificationManager.Notify(unique, notification);
}
下面是我的MainView類代碼
public abstract class BaseMainView : BaseView<MainViewModel>
{
}
[Activity(Label = "New Tasks",
NoHistory = false,
Icon = "@drawable/icon",
Theme = "@style/Theme.AppCompat.Light",
LaunchMode = Android.Content.PM.LaunchMode.SingleTask,//setted to single task to handle stack issue
//ScreenOrientation = Android.Content.PM.ScreenOrientation.Portrait,
UiOptions = Android.Content.PM.UiOptions.SplitActionBarWhenNarrow)]
/*,
ConfigurationChanges = Android.Content.PM.ConfigChanges.Orientation | Android.Content.PM.ConfigChanges.ScreenSize*/
public class MainView : BaseMainView
{
public new MainViewModel ViewModel
{
get { return base.ViewModel; }
set { base.ViewModel = value; }
}
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
// Set up View to be shown
SetContentView(Resource.Layout.MainView);
var set = this.CreateBindingSet<MainView, MainViewModel>();
//Code to map binding with controls
set.Apply();
}
protected override void OnNewIntent(Android.Content.Intent intent)
{
//When is in background or running then this get called and I simple perform the action required.
}
}
注: - 我已經設置好的這個活動的LaunchMode是SingleTask。
LaunchMode = Android.Content.PM.LaunchMode.SingleTask
因爲,當該應用是前景或通知點擊OnNewIntent背景被調用,並能夠在那裏執行邏輯。但是,當應用程序從後臺刪除,並通知到達然後通知單擊OnCreate方法被調用,那裏我得到的MainViewModel爲空。
請幫我解決這個問題。
感謝, Aaman