Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/108.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 表单-区分后台和前台应用程序中心推送通知_Ios_Xamarin.forms_Push Notification_Visual Studio App Center - Fatal编程技术网

Ios 表单-区分后台和前台应用程序中心推送通知

Ios 表单-区分后台和前台应用程序中心推送通知,ios,xamarin.forms,push-notification,visual-studio-app-center,Ios,Xamarin.forms,Push Notification,Visual Studio App Center,我们正在使用App Center推送通知,根据下面的文章,推送通知可以在App.xaml.cs中使用Push.PushNotificationReceived进行处理 这实际上是在ATM上工作,并且该方法实际上是为后台和前台通知触发的 我们需要能够区分它们。当用户点击通知(后台)时,我们会将用户导航到应用程序的特定部分,如果应用程序已经打开(前台),我们就不能这样做 我见过一些实现这一点的方法,但它们都是特定于平台的(本例中为iOS)。在PCL中有没有同样的方法?我认为Xamarin表单没有为我

我们正在使用App Center推送通知,根据下面的文章,推送通知可以在App.xaml.cs中使用Push.PushNotificationReceived进行处理

这实际上是在ATM上工作,并且该方法实际上是为后台和前台通知触发的

我们需要能够区分它们。当用户点击通知(后台)时,我们会将用户导航到应用程序的特定部分,如果应用程序已经打开(前台),我们就不能这样做


我见过一些实现这一点的方法,但它们都是特定于平台的(本例中为iOS)。在PCL中有没有同样的方法?

我认为Xamarin表单没有为我们提供获取应用程序状态的api

您可以通过在Xamarin中使用
DependencyService
来实现这一点。表单:

首先在PCL项目中定义一个接口:

public interface IGetAppState
{
    bool appIsInBackground();
}
在iOS中:

[assembly: Dependency(typeof(GETAppState_iOS))]

namespace App129.iOS
{
    public class GETAppState_iOS : IGetAppState
    {
        public bool appIsInBackground()
        {
            UIApplicationState state = UIApplication.SharedApplication.ApplicationState;
            bool result = (state == UIApplicationState.Background);

            return result;
        }
    }
}
[assembly: Dependency(typeof(GETAppState_Android))]

namespace App129.Droid
{
    public class GETAppState_Android : IGetAppState
    {
        public bool appIsInBackground()
        {
            bool isInBackground;

            RunningAppProcessInfo myProcess = new RunningAppProcessInfo();
            GetMyMemoryState(myProcess);
            isInBackground = myProcess.Importance != Importance.Foreground;

            return isInBackground;
        }
    }
}
在Android中:

[assembly: Dependency(typeof(GETAppState_iOS))]

namespace App129.iOS
{
    public class GETAppState_iOS : IGetAppState
    {
        public bool appIsInBackground()
        {
            UIApplicationState state = UIApplication.SharedApplication.ApplicationState;
            bool result = (state == UIApplicationState.Background);

            return result;
        }
    }
}
[assembly: Dependency(typeof(GETAppState_Android))]

namespace App129.Droid
{
    public class GETAppState_Android : IGetAppState
    {
        public bool appIsInBackground()
        {
            bool isInBackground;

            RunningAppProcessInfo myProcess = new RunningAppProcessInfo();
            GetMyMemoryState(myProcess);
            isInBackground = myProcess.Importance != Importance.Foreground;

            return isInBackground;
        }
    }
}
在任何你想知道你的应用是在后台还是前台的地方,你都可以使用:

bool isInBackground = DependencyService.Get<IGetAppState>().appIsInBackground();
bool-isInBackground=DependencyService.Get().appIsInBackground();

谢谢你的回答,杰克,但不幸的是,这没有帮助。一旦收到通知,我们点击它,应用程序就会打开,appIsInBackground()总是返回为false。@DAlvarenga您是如何解决这个问题的?你是对的,这将永远是一个前台处理。我想你在寻找一些可能有帮助的东西: