MvvmCross - 如何从通知启动应用程序 [英] MvvmCross - How to start the application from a Notification

查看:67
本文介绍了MvvmCross - 如何从通知启动应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的是 MvvmCross 6.4.3.

I am using MvvmCross 6.4.3.

如果应用程序没有运行,并且我点击了之前从应用程序收到的通知,应用程序不会转到第一页(在我的例子中是登录页面)

If the app is not running, and I click on a notification I have received from the app previously, the app does not go to the first page (in my case the logon page)

当用户启动应用程序时,在我的自定义 MvxAppStart 中调用 NavigateToFirstViewModel,然后调用 MvxApplication.Startup.

When the app is started by the user NavigateToFirstViewModel is called in my custom MvxAppStart, it then calls MvxApplication.Startup.

但是,当我尝试从通知启动应用程序时,这些都没有被调用,因此我的第一页没有显示.

However when I attempt, to start the app from a notification, none of these are called and so my first page is not displayed.

导航到第一页的代码应该放在哪里,以便只在从通知开始的情况下调用

Where should I put the code to navigate to the first page, so that it is only called in the case of starting from a notification

推荐答案

这是我的做法.我已将其发布在 Github 上:

Here is how I did it. I've posted this on Github:

对于想要为活动指定 singleTop 启动模式的初学者:

For starters you want to specify the singleTop launch mode for your activity:

[Activity(LaunchMode = LaunchMode.SingleTop, ...)]
public class MainActivity : MvxAppCompatActivity

像这样生成通知PendingIntent:

var intent = new Intent(Context, typeof(MainActivity));
intent.AddFlags(ActivityFlags.SingleTop);
// Putting an extra in the Intent to pass data to the MainActivity
intent.PutExtra("from_notification", true);
var pendingIntent = PendingIntent.GetActivity(Context, notificationId, intent, 0);

现在有一些地方可以从 MainActivity 处理这个 Intent,同时仍然允许使用 MvvmCross 导航服务:

Now there are places to handle this Intent from MainActivity while still allowing the use of MvvmCross navigation service:

  1. OnCreate - 如果在点击通知时应用未运行,则将调用 OnCreate.
  1. OnCreate - If the app was not running while the notification was clicked then OnCreate will be called.

protected override void OnCreate(Bundle bundle)
{
    base.OnCreate(bundle);
    if (bundle == null && Intent.HasExtra("from_notification"))
    {
        // The notification was clicked while the app was not running. 
        // Calling MvxNavigationService multiple times in a row here won't always work as expected. Use a Task.Delay(), Handler.Post(), or even an MvvmCross custom presentation hint to make it work as needed.
        var mvxNavigationService = MvvmCross.Mvx.IoCProvider.Resolve<IMvxNavigationService>();
        await mvxNavigationService.Navigate<ViewModel>();
        var handler = new Handler(Looper.MainLooper);
        handler.Post(async () => await mvxNavigationService.Navigate<AnotherViewModel>();
    }
}

  1. OnNewIntent - 如果应用在点击通知时正在运行,则 OnNewIntent 将被调用.
  1. OnNewIntent - If the app was running while the notification was clicked then OnNewIntent will be called.

protected override void OnNewIntent(Intent intent)
{
    base.OnNewIntent(intent);
    if (intent.HasExtra("from_notification"))
    {
        // The notification was clicked while the app was already running.
        // Back stack is already setup.
        // Show a new fragment using MvxNavigationService.
    }
}

这篇关于MvvmCross - 如何从通知启动应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆