Xamarin.android Xamarin在通知点击时形成开放视图模型

Xamarin.android Xamarin在通知点击时形成开放视图模型,xamarin.android,xamarin.forms,mvvmcross,Xamarin.android,Xamarin.forms,Mvvmcross,我使用此代码在通知栏中显示通知。点击通知后,将启动主活动。是否可以使用MvvmCross在Xamarin forms应用程序中启动视图模型而不是活动 Intent notificationIntent = new Intent(context,typeof(MainActivity)); notificationIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.F

我使用此代码在通知栏中显示通知。点击通知后,将启动主活动。是否可以使用MvvmCross在Xamarin forms应用程序中启动视图模型而不是活动

 Intent notificationIntent = new Intent(context,typeof(MainActivity));
    notificationIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP |                                Intent.FLAG_ACTIVITY_SINGLE_TOP);      
   PendingIntent pIntent = PendingIntent.getActivity(context, code,
            notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
    NotificationManager manager = (NotificationManager) context
            .getSystemService(Context.NOTIFICATION_SERVICE);
    NotificationCompat.Builder notify = new NotificationCompat.Builder(
            context);

    notify.setContentIntent(pIntent);
    notify.setSmallIcon(R.drawable.app_icon);
    notify.setContentTitle(“Title”);
    manager.notify(reqCode, notify.build());

我的想法是结合使用

首先,在通知栏中显示通知:

Intent intent = new Intent(Forms.Context, typeof(MainActivity));
if (openPage)
{
    intent.SetFlags(ActivityFlags.SingleTop);
    intent.PutExtra("OpenPage", "SomePage");
}

const int pendingIntentId = 0;
PendingIntent pendingIntent = PendingIntent.GetActivity(Forms.Context, pendingIntentId, intent, PendingIntentFlags.OneShot);

var nMgr = (NotificationManager)Android.App.Application.Context.GetSystemService(Context.NotificationService);
Notification.Builder notBuilder = new Notification.Builder(Android.App.Application.Context)
    .SetContentIntent(pendingIntent)
    .SetContentTitle("SomeApp")
    .SetContentText(message)
    .SetDefaults(NotificationDefaults.Sound | NotificationDefaults.Vibrate)
    .SetSmallIcon(Resource.Drawable.ic_launcher)
    .SetAutoCancel(true);

var notification = notBuilder.Build();
nMgr.Notify(0, notification);
在MainActivity.cs中检查额外的内容:

protected override void OnNewIntent(Intent intent)
{
    // Send message to the PCL (XF) if a certain page should be opened.
    if (intent.HasExtra("OpenPage"))
    {
        string pageName = intent.GetStringExtra("OpenPage") ?? "None";

        if (pageName != "None")
        {
            var message = new OpenPageMessage { PageName = pageName };
            MessagingCenter.Send(message, Message.Msg_OpenPage);
        }
    }

    base.OnNewIntent(intent);
}
您的中央导航实例(例如,
MainPage
)订阅此消息:

MessagingCenter.Subscribe<Message.OpenPageMessage>(this, Message.Msg_OpenPage, (async) message =>
{
    // Loads a certain page if a message is received

    switch (message.PageName)
    {
        case "SomePage":
            await Navigation.PushModalAsync(new SomePage(), true);
            break;
        default:
            break;
    }
});
有了这个帮助

编辑


如果一次有多个推送通知,则会出现问题,其中会覆盖通知。您可以使用不同的或使用。

您好,当用户单击推送通知时,我使用了“put extra”导航到特定页面,但某些情况下它失败了。我有两种类型的通知,即1。订单2。复习。当订单类型通知时,我需要添加额外订单,当收到审核类型通知时,我需要添加额外审核,但当我单击订单类型通知时,它的意图是显示“审核”额外订单。它保存最后一个通知类型。如何解决这个问题。我认为最好是为这个问题创建一个新的问题,在哪里更好地描述(例如,使用代码),你想要什么,你的问题在哪里。我只使用了一种通知类型,因此当前无法重现您的问题。@Deepak:您找到解决方案了吗?我认为问题在于我们的合作。您可以为
requestCode
使用或使用唯一的id。有趣的是,所有这些都使用了我的代码中显示的方法,这导致了您的问题。在这里也没有帮我。也许有人能比我更好地解释这段关系。
public class Message
{
    public const string Msg_OpenPage = "OpenPage";

    public class OpenPageMessage {
        public string PageName { get; set; }
    }
}