How to run/call Method when applicationDidEnterBackground And Get Updates By LocalNotification In Iphone/iPad Application

H

//—————————————————————
AppDelegate.h
UIBackgroundTaskIdentifier bgTask;

//—————————————————————
AppDelegate.m
– (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
[UIApplication sharedApplication].applicationIconBadgeNumber = 0;
}

– (void)applicationDidEnterBackground:(UIApplication *)application
{
UIApplication* app = [UIApplication sharedApplication];
bgTask = [app beginBackgroundTaskWithExpirationHandler:^{
[app endBackgroundTask:bgTask];
bgTask = UIBackgroundTaskInvalid;
}];
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
NSTimer* t = [NSTimer scheduledTimerWithTimeInterval:5 target:self selector:@selector(YourMethodName) userInfo:nil repeats:YES];
[[NSRunLoop currentRunLoop] addTimer:t forMode:NSDefaultRunLoopMode];
[[NSRunLoop currentRunLoop] run];
});
}
– (void) YourMethodName
{
if (@”Satisfy Condition”)
{
[[UIApplication sharedApplication] cancelAllLocalNotifications];

NSDate *newDate = [NSDate date];

UILocalNotification *localNotif = [[UILocalNotification alloc] init];
if (localNotif == nil)
return;
localNotif.fireDate = newDate;
localNotif.alertBody = @”Request For Cab”;
localNotif.soundName = UILocalNotificationDefaultSoundName;
localNotif.applicationIconBadgeNumber = 1;
NSString *StringPass = @”AppNotifier Notification”;
NSDictionary *userdict = [NSDictionary dictionaryWithObject:StringPass forKey:@”My notify”];
localNotif.userInfo = userdict;
[[UIApplication sharedApplication] scheduleLocalNotification:localNotif]; [localNotif release];
}
}

– (void)application:(UIApplication *)app didReceiveLocalNotification:(UILocalNotification *)notif
{
[UIApplication sharedApplication].applicationIconBadgeNumber = 0;
[[UIApplication sharedApplication] cancelAllLocalNotifications];
}

About the author

abdulgafar.nurbash
By abdulgafar.nurbash

Category