0
votes

i have xamarin forms app that support notification, i have done it in android with broadcast receiver now i have to do notification in ios ! , my service is depending on API REST so i want every 60 second ios app run HTTP request and get data then show it as notification, i searched for many days but i can't reach to my approach ? if this is impossible can i use nuget or something like that in ios project only "in xamarin forms solution " or not ?

        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

1 Answers

0
votes

Here is my code for generating a local notification on 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);
        }
    });
}

The first parameter in CreateTrigger is how long before the notification is generated. I notice you have 60 in yours. Also bear in mind a notification will not appear if your app is foregrounded.