Is it possible to run a Switch when the App is not running?

Asked

Viewed 143 times

1

The App would have to communicate to the user, that there was change of a value in the server, but without use of Push Notification. So I thought I’d add a Logger to iOS, and that by detecting the value change, it would trigger a local notification. And this should occur even if the App is not running.

Does anyone know where I can start?

1 answer

3


You can launch the app in the background, check the server and then trigger the notification using background fetch. The steps are as follows:

  1. In the project settings, in Capabilities, enable the section Background Modes and select Background fetch.
  2. In the Appdelegate class, in the method -(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions you configure the frequency at which the app will be called in the background, through the method: - (void)setMinimumBackgroundFetchInterval:(NSTimeInterval)minimumBackgroundFetchInterval

Example:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    //...
    [application setMinimumBackgroundFetchInterval:UIApplicationBackgroundFetchIntervalMinimum];
    return YES;
}
  1. Now add the method in the Appdelete class - (void)application:(UIApplication *)application performFetchWithCompletionHandler:(void (^)(UIBackgroundFetchResult result))completionHandler;

This method will be called whenever the app is launched in the background. At the end of the execution of operations it is always necessary to call the completionHandler, informing if the search for the data was successful. Example:

-(void)application:(UIApplication *)application performFetchWithCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler {

    /*
    ...
     */
    completionHandler(UIBackgroundFetchResultNewData);
}

To test, you configure the target to launch the app in background mode. For this, go to Product/Scheme/Manage Schemes. Now in Run/Option, marque Launch due to a background fetch Event. When you build the app will simulate the launch in the background and so you can test it without waiting for the system to do it.

  • I was reading a tutorial about it right now! Kkk thanks bro!!! Helped a lot. Only one question, which may be redundant, but it’s not clear to me. The service runs even if the App is not running, and it is possible to trigger a Local Notification to inform the user of new data available?

  • My code enters before completionHandler(UIBackgroundFetchResultNewData), correct?

  • I took a test adjusting [application setMinimumBackgroundFetchInterval:UIApplicationBackgroundFetchIntervalMinimum]; manually every 5 seconds for testing, but it only triggers the notification once. Even opening the App and closing, or putting it in the background :/ Is there a way to make this process recursive? ( call itself after each check: 1-wait X time, 2- query? notifies : does nothing, 3 - auto flame)

  • You’re welcome Tiago. Comment 1: Yes, you can launch a local notification. Comment 2: Yes, do not forget, if the operation is asynchronous (e.g., server request), to call completionHandler only at the end of the same. Comment 3: No. It’s no use calling this method all the time. The system that decides when to open the app in the background. You can’t control it. Note that the parameter is the minimum time between calls. You cannot control the maximum. Typically it will take quite a lot of time between calls (hours/days)

  • Thanks ! I did some more research here and really that’s it.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.