2017-09-26 54 views
0

我创建了一个地理位置应用程序,并且我想要自动启动每次向我发送坐标的服务的启动。已关闭的应用程序应用程序触发事件:didReceiveRemoteNotification:

从互联网上看,我从技术上说它不可能在ios上,但有一些方法可以解决这个问题。

我想采用无声通知。

我实现了该方法,并且当应用程序在backgorund中时一切正常,但是当我打开设备并发送无声通知时,事件didReceiveRemoteNotification:不会触发。 甚至当我从应用程序切换器关闭应用程序,然后发送通知没有触发。

我想知道如果你能做到的话是否有什么问题。

PS:我使用的是:didReceiveRemoteNotification:没有应用:didReceiveRemoteNotification:fetchCompletionHandler:(可在此问题?)

我的代码中的AppDelegate:

我FinishedLaunching
public override async void DidReceiveRemoteNotification(UIApplication application, NSDictionary userInfo, Action<UIBackgroundFetchResult> 
    completionHandler) 
     { 

      await Manager.SendPosition(completionHandler); 
     } 

if (Convert.ToInt16(UIDevice.CurrentDevice.SystemVersion.Split('.')[0].ToString()) < 8) 
      { 
       UIRemoteNotificationType notificationTypes = UIRemoteNotificationType.Alert | UIRemoteNotificationType.Badge | 
       UIRemoteNotificationType.Sound; 
       UIApplication.SharedApplication.RegisterForRemoteNotificationTypes(notificationTypes); 
      } 
      else 
      { 
       UIUserNotificationType notificationTypes = UIUserNotificationType.Alert | UIUserNotificationType.Badge | UIUserNotificationType.Sound; 
       var settings = UIUserNotificationSettings.GetSettingsForTypes(notificationTypes, new NSSet(new string[] { })); 
       UIApplication.SharedApplication.RegisterUserNotificationSettings(settings); 
       UIApplication.SharedApplication.RegisterForRemoteNotifications(); 
      } 

,并在我的info.plist:

<key>UIBackgroundModes</key> 
     <array> 
     <string>location</string> 
     <string>remote-notification</string> 
     </array> 

UPDATE:

public async Task<bool> SendPosition(Action<UIBackgroundFetchResult> completionHandler = null) 
     { 
      StartLocationUpdates(); 
      var setting = ConnectDb()?.Table<Settings>().FirstOrDefault() ?? new Settings(); 
      _currentLong = 14.52538; 
      _currentLat = 36.82842; 
      if (_currentLong != 0 && _currentLat != 0)// && setting.TimeNotification != 0) 
      { 
       var objectSend = new 
       { 
        unique_key = setting.UniqueKey, 
        lat = _currentLat, 
        lng = _currentLong, 
        idPlayerOneSignal = setting.IdPlayerOneSignal, 
        token = "" 
       }; 

       var client = new HttpClient { BaseAddress = new Uri("http://amywebservice.com/") }; 
       var data = JsonConvert.SerializeObject(objectSend); 

       var content = new StringContent(data, Encoding.UTF8, "application/json"); 
       var response = await client.PostAsync("api/setPosition", content); 
       if (response.IsSuccessStatusCode) 
       { 
        var successResult = response.Content.ReadAsStringAsync().Result; 
        Debug.WriteLine("k", successResult); 
        if (completionHandler != null) 
        { 
         completionHandler(UIBackgroundFetchResult.NoData); 
        } 
        return true; 
       } 
       else 
       { 
        var failureResulr = response.Content.ReadAsStringAsync().Result; 
        Debug.WriteLine("f", failureResulr); 
        if (completionHandler != null) 
        { 
         completionHandler(UIBackgroundFetchResult.NoData); 
        } 
        return false; 
       } 
      } 
      return false; 
     } 

但不工作,如果我发送一个无声的通知,我只运行了第一飞且仅当该应用程序是在backgorund。

如果我打开手机并通知通知不起作用。

回答

0
  1. 据对DidReceiveRemoteNotification Xamarin文档:

    [MonoTouch.Foundation.Export( “应用:didReceiveRemoteNotification:fetchCompletionHandler:”)]

    在Xamarin DidReceiveRemoteNotification相同原生地为application:didReceiveRemoteNotification:fetchCompletionHandler:。所以,这个方法是正确的。

  2. 如果您已强制退出应用程序,那么无声通知将不会启动您的应用程序,除非您重新启动应用程序或重新启动设备。这里的Related Apple documentation

    另外,如果你启用了远程通知后台模式,系统将启动您的应用程序(或从暂停状态唤醒它),当远程通知到达所说的那样在后台状态。但是,如果用户强制退出,系统不会自动启动您的应用程序。在这种情况下,用户必须重新启动您的应用程序或在系统尝试再次自动启动您的应用程序之前重新启动设备。

  3. 我不是你对打开设备的描述很清楚(解锁设备未经公开的应用程序或其他?)这里:

    ,但是当我打开设备和根据application(_:didReceiveRemoteNotification:fetchCompletionHandler:)

    发送无声通知

    但只要你完成凝固酶原发出通知后,必须在处理程序参数中调用该块,否则应用程序将终止。您的应用程序有多达30秒的挂钟时间来处理通知并调用指定的完成处理程序块。 实际上,一旦完成处理通知,您应该立即调用处理程序块。系统会跟踪您的应用后台下载的已用时间,用电量和数据成本。 处理推送通知时使用大量电源的应用程序可能不会总是在早期被唤醒以处理未来的通知。

    当您完成处理通知后,您必须执行completionHandler

    DidReceiveRemoteNotification在相应的地方,如if声明加入completionHandler()

    • completionHandler(UIBackgroundFetchResult.NewData)当你得到你想要的东西。
    • completionHandler(UIBackgroundFetchResult.Failed)当提取数据存在任何问题时。
    • completionHandler(UIBackgroundFetchResult.NoData)当没有数据时。

    它看起来像在代码中遗漏了,这可能会影响无声通知。

+0

感谢澄清,但现在的代码我张贴,如果我关掉手机,然后重新启动它,并发送一个无声的通知,我无法唤醒我的应用程序。它可能是为completionHandler 我该如何遵循操作completionHandler? –

+0

类似于此: '公共覆盖异步空隙DidReceiveRemoteNotification(UIApplication的应用,的NSDictionary USERINFO,操作 completionHandler) { completionHandler(UIBackgroundFetchResult.NoData); await Manager.SendPosition(); }' ??? –

+0

@BrunoMandarà,当你完成发送位置时执行'completionHandler',比如在'SendPosition()'方法的完成回调中。 –