0

General idea of what I need:

I am porting an Android app to iOS (Using Xamarin, but I can translate to C# from objective C easily enough) that relies heavily on the AlarmManager to do background checks on an HTML page on a website that I don't own. AlarmManager is essentially a task scheduler for Android. The user would set the frequency to whatever they desired.

What I've tried:

Background fetching:

app.SetMinimumBackgroundFetchInterval(240);
 UNUserNotificationCenter.Current.RequestAuthorization(UNAuthorizationOptions.Alert, (approved, err) =>
 {
 // Handle approval
 });
 UNUserNotificationCenter.Current.Delegate = new WEBSITEFUNCTIONS.UserNotificationCenterDelegate();
 return base.FinishedLaunching(app, options);


    public override void PerformFetch(UIApplication application, Action<UIBackgroundFetchResult> completionHandler)
    {

        System.Diagnostics.Debug.WriteLine("interval");
        WEBSITEFUNCTIONS kf = new WEBSITEFUNCTIONS();
        kf.doCheck();
        completionHandler(UIBackgroundFetchResult.NewData);
    }

Perform Fetch is just straight up NEVER called. I need some consistency (being one minute off is no big deal... but several hours will not do). I let it run and it just straight up never worked. I've read lots on how PerformFetch works, and I don't think it'll give me the critical response time that the user needs.

UserNotifications: New to iOS 10, is the ability to have repeating notifications. However this repeats the same notification.

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

Push Alerts:

My own server

I could setup a server that does the checking and then sends a message to the Firebase Cloud Messaging to send a message to the user about the new items. I have approximately 500 active users on the Android version, if they check 5 different pages every 5 minutes, at 90 kbs a check, that's about half a gig of bandwidth an hour.

So the cons are:

  • Excessive bandwidth usage will make my home internet a lot slower
  • I will need to secure it myself
  • Power outages can sometimes last for days, leaving end users out of the loop
  • Their server could boot off my machine at any given moment, I could get a new IP address from my ISP if that happened... assuming they allow that

Using my shared hosting, setup a cronjob every 15 minutes

I can setup a cronjob to do an alert every 15 minutes. It's not the fastest, but way better than relying on the first option (as it just straight up never gets called)

  • Once again, I'm at the mercy that their server doesn't kick me off. The app completely breaks if they do this.
  • Shared hosting might cut me off for putting too much strain on their servers (Hostgator claims unlimited bandwidth, I'm not sure if they'd like me doing that)
Evan Parsons
  • 1,139
  • 20
  • 31
  • There is no such thing as being able to schedule a task to run periodically in the background in iOS. Period. There just isn't. setMinimumBackgroundFetchInterval does not do what you are assuming it does. I don't know why you're looking at user notifications, they just post a notification to the user. If you have data on the server that has changed and you need to notify the user the only option is push notifications. There are many many many past questions asking how to do this sort of stuff, search some to get further details on the alternatives, which is just push really. – Gruntcakes Nov 08 '16 at 19:36
  • @Essenceofchickens out of curiosity, what if you push some notification from server to client every X minutes, and client (woken up by this notification) performs some task, will that work to run certain task periodically? – Evk Nov 08 '16 at 19:41
  • @Evk In theory but not exactly in practice, there's several ifs and buts. For example if the user force-quit the app it will not get launched by the push. If the device powers in to sleep mode the pushes won't get through etc. etc. In addition to this, if Apple noticed a server was sending pushes with a high regular periodicity they might block them. – Gruntcakes Nov 08 '16 at 19:48
  • The APNS QOS Queue holds a single notification per app per device, so if the user is out of service (tunnel, airplane mode, device off, ...) only the last notification will be received when the OS reconnects with APNS, Apple of course reserves the right to throttle your notifications, but sending one every few minutes for each of your users is nothing to them, they are dealing with a system that has push *trillions* of messages since they started... – SushiHangover Nov 08 '16 at 20:46
  • 1
    iOS Background Fetching, the Fetch Interval is handled by the OS and is a totally **adaptive** routine as it monitors the user's usage pattern of your app along with current battery life, etc... and schedules fetching according with the `PerformFetch` calls to your app. I have found it to work amazing well... I have an personal app that syncs a Realm db and *very rarely* is it not updated when I open the app... – SushiHangover Nov 08 '16 at 20:51
  • @Evk that idea might work quite nicely. If I send a notification every ten minutes, that shouldn't alert Apple too much. I'm looking at using Firebase messaging instead of the Apple service, not sure if that'd be better. Even if they block the occasional one, that's not too big of a deal. If they force quit the app, they might restart it. I will experiment and post my findings. – Evan Parsons Nov 08 '16 at 23:00

0 Answers0