Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Scheduling a second notification if first is ignored in swift (or completing any action if notification is ignored)

So I have an app currently on the app store that schedules 10 notifications in advance assuming you miss one you will still get a second chance or ten. Now before you think I will be bothering the person, the notification is very important to the functionality of the app and really is the main purpose. The app was built for iOS 7 so at that time there was no "handleActionWithIdentifier" which can, from my understanding, complete actions for the app even if it is closed depending on the users response to the notification. This update was very helpful for the app as it eliminates part of my problem of having to open the app to respond to the notification (the notifications ask the user a question and depending on the answer, completes something).

The problem that remains is detecting if the notification was missed, how would I make another notification appear, for example the next day, if the notification is dismissed or ignored. I have searched this on google and stack overflow and from my understanding all previous questions have been asking how to detect if the notification was missed one the app is opened which I do not need.

At this point, I can properly run code if the user responds to the notification by pressing one of the options on the notification by this :

func application(application: UIApplication, handleActionWithIdentifier identifier: String?, forLocalNotification notification: UILocalNotification, withResponseInfo responseInfo: [NSObject : AnyObject], completionHandler: () -> Void) {
        var userInfo = [NSObject: AnyObject]()
        userInfo["text"] = responseInfo[UIUserNotificationActionResponseTypedTextKey]
        NSNotificationCenter.defaultCenter().postNotificationName("text", object: nil, userInfo: userInfo)

        print(userInfo)

        completionHandler()
    }

as of right now, I am just taking textfield input and printing it but I could launch a second notification if I wanted. Is there a method to detecting when a notification is missed and scheduling another notification?

There is always a chance that it is still not possible to do what I want and I would just schedule 10 notifications in advance which seems sloppy and does not let me make the response as iterative.

TLDR; how do I detect and run code if a local notification is missed WITHOUT opening the app

BTW: if you have answers, swift is the preferred language

like image 955
JCode Avatar asked Jan 12 '16 14:01

JCode


1 Answers

You can use a mixture of Background Fetch and Some kind of timestamp test.

For instance:

When you schedule a notification you can keep some logic that would let you track if that notification was ignored or not. Maybe keep some data in NSUserDefaults holding which was the last Notification sent and when it should be launched by the O.S.

One way could be to check for that timestamp:

If you launch this test after the moment in which it was supposed to launch (and maybe a bit late, just in case the user see it but is still not ready to answer) and you haven't still marked it as not ignored, then the user may have ignored or missed your notification.

This test should be able to be used in your AppDelegate.

Then, enable Background Fetch capability in Background Modes.

This will give some CPU time to your app (when iOS thinks is a good time for that) and you can seize the opportunity ;-).

For being able to do that you will need to add proper function into your AppDelegate implementation:

func application(application: UIApplication, performFetchWithCompletionHandler completionHandler: (UIBackgroundFetchResult) -> Void) {
       // your code
    }

In this function's body, run your "test for ignored notification" and schedule new notifications if needed.

Don't forget to call completionHandler as soon as you finish your test!

like image 179
Hugo Alonso Avatar answered Nov 14 '22 03:11

Hugo Alonso