使用http请求在xamarin ios中创建本地通知

时间:2017-09-25 22:50:59

标签: xamarin xamarin.ios xamarin.forms

我有支持通知的xamarin表单应用程序,我已经在android中使用广播接收器完成了现在我必须在ios中做通知! ,我的服务依赖于API REST,所以我希望每60秒ios应用程序运行HTTP请求并获取数据然后将其显示为通知,我搜索了很多天但我无法达到我的方法? 如果这是不可能的,我可以在ios项目中使用nuget或类似的东西"在xamarin形式解决方案"不是吗?

        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...
            }
        });

1 个答案:

答案 0 :(得分: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个。另请注意,如果您的应用是有前途的,则不会显示通知。