当应用程序处于后台时,IOS应用程序中的本地通知

时间:2019-02-12 11:08:42

标签: xamarin.forms

我想将本地通知发送到我的IOS应用程序。应用正在从我的数据库中检查时间,该时间与我的移动时间相匹配,它会发出通知,但是当我的应用处于前台时,本地通知可以正常工作,当我关闭应用时,本地通知无法正常工作的任何问题,

>

我尝试了很多事情,但无法成功

这是我在App.cs上运行的代码

private async void CheckConnection()
        {
            var httpClient = new HttpClient();


        var json = await httpClient.GetStringAsync("http://192.168.10.55:56556/api/ShowAll");

        TimeSpan time = TimeSpan.FromSeconds(1);
        DateTime dateTime = DateTime.Today.Add(time);
        var admtPatients = Newtonsoft.Json.JsonConvert.DeserializeObject<List<ContactDetail>>(json);
        ObservableCollection<ContactDetail> trends = new ObservableCollection<ContactDetail>(admtPatients);
        foreach (var item in trends)
        {
            if (item.TimeSchedule == DateTime.Now.ToString("hh:mm:ss"))
            {
                CrossLocalNotifications.Current.Show("title", "body");
            }
        }
        return;
    }
    protected override void OnStart()
    {
        var seconds = TimeSpan.FromSeconds(1);

        Device.StartTimer(TimeSpan.FromSeconds(1), () =>
        {
            CheckConnection();
            return true;
        });

        // Handle when your app starts
    }

    protected override void OnSleep()
    {
        base.OnSleep();
        var seconds = TimeSpan.FromSeconds(1);

        Device.StartTimer(TimeSpan.FromSeconds(1), () =>
        {
            CheckConnection();
            return true;
        });
    }

    protected override void OnResume()
    {
        base.OnSleep();
        var seconds = TimeSpan.FromSeconds(1);

        Device.StartTimer(TimeSpan.FromSeconds(1), () =>
        {
            CheckConnection();
            return true;
        });
    }

1 个答案:

答案 0 :(得分:0)

当用户没有积极使用您的应用程序时,系统会将其移至后台状态。对于许多应用程序而言,后台状态只是暂停应用程序的短暂停留。暂停应用程序是延长电池寿命的一种方式,它还允许系统将重要的系统资源投入到引起用户注意的新的前台应用程序中。

因此,如果您希望在应用程序过渡到后台时启动长时间运行的任务。您应该请求启动包含过期处理程序的后台任务,以防万一该任务在iOS项目中花费太长时间。

- (void)applicationDidEnterBackground:(UIApplication *)application
{
  bgTask = [application beginBackgroundTaskWithName:@"MyTask" expirationHandler:^{
    // Clean up any unfinished task business by marking where you
    // stopped or ending the task outright.
    [application endBackgroundTask:bgTask];
    bgTask = UIBackgroundTaskInvalid;
   }];

  // Start the long-running task and return immediately.
  dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{

    // Do the work associated with the task,you can use messageingcenter to told forms send notification.

    [application endBackgroundTask:bgTask];
    bgTask = UIBackgroundTaskInvalid;
  });
}
相关问题