2017-09-26 117 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()'方法的完成回調中。 –