Xamarin-Android Mvvmcross - 点击收到的通知启动应用程序与启动或带到前面

Jon*_*rix 6 android mvvmcross xamarin

我正在尝试为以下要求创建一个简洁的解决方案:

a)当用户"点击"我的应用收到的通知并且应用程序处于打开状态和/或后台时,该应用程序将被带到该字体.

b)当用户"点击"通知并且应用程序关闭时,启动画面会显示并且应用程序将按正常情况启动.

我正在尝试,但我只能在上述任何一个选项中取得成功,而不是两个都不幸.这是我的代码:

    public void CreateNotification(string title, string desc, string pushUrl, string pushTitle)
    {
        var setupSingleton = MvxAndroidSetupSingleton.EnsureSingletonAvailable(ApplicationContext);
        setupSingleton.EnsureInitialized();

        if (!string.IsNullOrWhiteSpace(pushUrl))
        {
            var pushMessageParameterService = Mvx.Resolve<IPushMessageParameterService>();
            pushMessageParameterService.SetPushActionParameters(new PushActionParameters
            {
                UrlToShow = pushUrl,
                ViewTitle = pushTitle
            });
        }

        var intent = new Intent(this, typeof(SplashScreen));
        intent.AddFlags(ActivityFlags.NewTask);
        intent.SetAction(Intent.ActionMain);
        intent.AddCategory(Intent.CategoryLauncher);

        //var pendingIntent = PendingIntent.GetActivity(this, 0, intent, PendingIntentFlags.OneShot);
        //var pendingIntent = PendingIntent.GetActivity(this, 0, intent, 0);
        var pendingIntent = PendingIntent.GetActivity(this, 0, intent.SetFlags(ActivityFlags.BroughtToFront), PendingIntentFlags.CancelCurrent);

        Uri alarmSound = RingtoneManager.GetDefaultUri(RingtoneType.Notification);

        var notificationBuilder = new Notification.Builder(this)
            .SetSmallIcon(Resource.Drawable.Icon)
            .SetContentTitle(title)
            .SetContentText(desc)
            .SetAutoCancel(true)
            .SetSound(alarmSound)
            .SetContentIntent(pendingIntent);

        var notificationManager = (NotificationManager)GetSystemService(Context.NotificationService);
        Notification notification = notificationBuilder.Build();
        notification.Flags = NotificationFlags.ShowLights | NotificationFlags.AutoCancel;

        notificationManager.Notify(0, notification);
    }
Run Code Online (Sandbox Code Playgroud)

为了简单起见,我有两个活动:

public class SplashScreen : MvxSplashScreenActivity
Run Code Online (Sandbox Code Playgroud)

public class DashboardView : BaseMvxActivity
Run Code Online (Sandbox Code Playgroud)

如果我使用"SplashScreen"作为通知的PendingIntent,并且应用程序已经在后台启动/打开/,它会挂起在splashScreen上.MvvmCross日志记录显示"显示ViewModel DashboardViewModel"但停在那里.不调用OnCreate,Init和Start.飞溅只是停留.

如果我使用"DashboardView"作为通知的PendingIntent并且应用程序已关闭/未激活,那么我只是在启动时看到白屏并且没有启动画面.

我希望两全其美:).因此,当点击推送消息并且应用程序处于打开状态时,只需将应用程序置于前端(如果尚未安装).当应用程序关闭时,显示启动画面等.

我希望我的问题清楚.

提前谢谢了.

Ran*_*ngy 1

当我尝试使用 MvxSplashScreenActivity 来实现通知意图时,它冻结在该屏幕上。

我让它指向一个标准的 MvxActivity 并自己设置背景以及NoHistory = true属性Activity。在我的待处理活动的 OnCreate 中,我让它启动真正的意图。