2017-09-25 124 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的逻辑:),谢谢史蒂夫先生 –