Can UILocalNotification be used to wake up a task that is in the background

I would like to know if it is possible to somehow "wake up" a task that is in the background, quickly check something on the network. I think this can be done using UILocalNotification, however, no matter what I tried, I could not get didReceiveLocalNotification to do BELOW when the application is in the background. After starting, I immediately close the application by pressing the "Home" button (there is a 10 second delay for local notification to start). This code works PERFECTLY when the application is in the foreground and just seems to be sitting there ...

In the application delegate header file:

UILocalNotification *localNotif; 

For testing, I set up a local notification to quickly launch the appDelegate application.

 localNotif = [[UILocalNotification alloc] init]; localNotif.fireDate = [NSDate dateWithTimeIntervalSinceNow:10]; // the date you want the notification to fire. localNotif.timeZone = [NSTimeZone defaultTimeZone]; [[UIApplication sharedApplication] scheduleLocalNotification:localNotif]; NSLog(@"setup the timer for 10 seconds"); - (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification { UIApplicationState state = [application applicationState]; NSLog(@"getting kicked"); if (state == UIApplicationStateInactive) { // Application was in the background when notification was delivered. NSLog(@"INACTIVE.."); } else { NSLog(@"ACTIVE.."); } 

}

+4
source share
1 answer

The user has several options: # 1) They want to see a notification for your application. # 2) If notifications are enabled for your application, do you want them to click your notification to launch the application. If they accept notifications and open your notification when your application is in the background, application:didReceiveLocalNotification . To be clear, the user must accept the notification (for example, move the slider under the notification) ... otherwise, NOTHING is called.

 - (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification { NSLog(@"%@", notification); } 

If your application has completed application:didFinishLaunchingWithOptions: is called -

 - (BOOL)application:(UIApplication *) application didFinishLaunchingWithOptions: (NSDictionary *)launchOptions { UILocalNotification *theNotification = [launchOptions objectForKey:UIApplicationLaunchOptionsLocalNotificationKey]; NSLog(@"%@", theNotification); return YES; } 
+5
source

Source: https://habr.com/ru/post/1444981/


All Articles