2017-09-25 79 views
0

我有xamarin表單支持通知的應用程序,我已經在android中使用廣播接收器完成了它,現在我必須在ios中做通知! ,我的服務取決於API REST,所以我希望每隔60秒ios應用程序運行HTTP請求並獲取數據,然後將其顯示爲通知,我搜索了很多天但我無法達到我的方法? 如果這是不可能的我可以使用nuget或類似的東西在ios項目中「在xamarin形式的解決方案」或不?在http請求中創建xamarin ios的本地通知

 content = new UNMutableNotificationContent(); 
     content.Title = "Notification Title"; 
     content.Subtitle = "Notification Subtitle"; 
     content.Body = "This is the message body of the notification."; 
     content.Badge = 1; 
     content.CategoryIdentifier = "message"; 

     var trigger = UNTimeIntervalNotificationTrigger.CreateTrigger(60, true); 


     var requestID = "sampleRequest"; 
     var request = UNNotificationRequest.FromIdentifier(requestID, content, trigger); 

     UNUserNotificationCenter.Current.AddNotificationRequest(request, (err) => 
     { 
      if (err != null) 
      { 
       // Do something with error... 
      } 
     }); 

回答

0

這是我在iOS

生成本地通知代碼
var alertsAllowed = false; 
UNUserNotificationCenter.Current.GetNotificationSettings((settings) => 
{ 
    alertsAllowed = (settings.AlertSetting == UNNotificationSetting.Enabled); 
}); 

if (alertsAllowed) 
{ 
    var content = new UNMutableNotificationContent(); 
    content.Title = "Incident Recorder"; 
    content.Subtitle = "Not Synchronised"; 
    content.Body = "There are one or more new incidents that have not been synchronised to the server."; 

    var trigger = UNTimeIntervalNotificationTrigger.CreateTrigger(5, false); 

    var requestID = "sampleRequest"; 
    var request = UNNotificationRequest.FromIdentifier(requestID, content, trigger); 

    UNUserNotificationCenter.Current.AddNotificationRequest(request, (err) => 
    { 
     if (err != null) 
     { 
      Console.WriteLine(err.LocalizedFailureReason); 
     } 
    }); 
} 

在CreateTrigger第一個參數是生成通知了多久。我注意到你有60個。同時請注意,如果您的應用已預先登錄,則不會出現通知。

+0

因此,我可以打電話給我的http請求從60後自動從服務器導入數據像我一樣或5像你一樣? –

+0

60(或5)與您從服務器導入數據的頻率無關。延遲顯示通知需要多長時間。您應該使用計時器從共享(PCL)代碼調用服務器。一旦你有數據調用平臺特定的代碼來觸發通知。 –

+0

在android平臺有一個服務,運行代碼在後臺沒有計時器,我不知道是否有這樣的事情在ios中,無論如何,我認爲我不能解釋第二個問題,或者我錯過了TI的邏輯:),謝謝史蒂夫先生 –