You can launch the app in the background, check the server and then trigger the notification using background fetch.
The steps are as follows:
- In the project settings, in Capabilities, enable the section Background Modes and select Background fetch.
- 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;
}
- 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?
– Tiago Amaral
My code enters before
completionHandler(UIBackgroundFetchResultNewData)
, correct?– Tiago Amaral
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)– Tiago Amaral
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)
– Rafael Leão
Thanks ! I did some more research here and really that’s it.
– Tiago Amaral