Reputation: 59
I am running background codes to check if there is any changes to a website and if there is a change a local notification to alert the user. However, when i run the code and try it out the local notification does not arrive. This is the code in my appDelegate.m
- (void)applicationDidEnterBackground:(UIApplication *)application {
NSLog(@"Has Entered Background");
timerCountDown = [NSTimer scheduledTimerWithTimeInterval:60.0 target:self selector:@selector(backGroundParsing) userInfo:nil repeats:YES];
}
- (void)backGroundParsing {
//This is where my background checking happens. I deleted the code here to make it shorter.
if (totalNumberOfEvents > totalNumberOfEvetsRecorded) {
NSLog(@"Notificatoin sent");
NSString *finalNumberOfEventsRecorded = [NSString stringWithFormat:@"%d",totalNumberOfEvents];
NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
[userDefaults setObject:finalNumberOfEventsRecorded forKey:@"finalRegisteredNumberOfEvents"];
UILocalNotification *notifyUserAboutNewEvent = [[UILocalNotification alloc] init];
[notifyUserAboutNewEvent setFireDate:[NSDate dateWithTimeIntervalSinceNow:1]];
[notifyUserAboutNewEvent setTimeZone:[NSTimeZone defaultTimeZone]];
[notifyUserAboutNewEvent setAlertBody:@"New event has been organised"];
[[UIApplication sharedApplication] setScheduledLocalNotifications:[NSArray arrayWithObject:notifyUserAboutNewEvent]];
}
}
I do log "Notification sent" but i do not receive any notification. Is there something missing or something wrong? I do not have any other code in my other void actions.
EDIT After what @rckoenes had said i edited my code to this.
- (void)application:(UIApplication * _Nonnull)application
performFetchWithCompletionHandler:(void (^ _Nonnull)(UIBackgroundFetchResult result))completionHandler {
[self backGroundParsing];
}
- (void)backGroundParsing {
//This is where my background checking happens. I deleted the code here to make it shorter.
if (totalNumberOfEvents > totalNumberOfEvetsRecorded) {
NSLog(@"Notificatoin sent");
NSString *finalNumberOfEventsRecorded = [NSString stringWithFormat:@"%d",totalNumberOfEvents];
NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
[userDefaults setObject:finalNumberOfEventsRecorded forKey:@"finalRegisteredNumberOfEvents"];
UILocalNotification *notifyUserAboutNewEvent = [[UILocalNotification alloc] init];
[notifyUserAboutNewEvent setFireDate:[NSDate dateWithTimeIntervalSinceNow:1]];
[notifyUserAboutNewEvent setTimeZone:[NSTimeZone defaultTimeZone]];
[notifyUserAboutNewEvent setAlertBody:@"New event has been organised"];
[[UIApplication sharedApplication] setScheduledLocalNotifications:[NSArray arrayWithObject:notifyUserAboutNewEvent]];
}
});
}];
[searchEventsTask resume];
}
However, i still do not receive the notification. I am getting an warning when i simulate the background fetch. This is the warning appearing in my NSLog Warning: Application delegate received call to -application:performFetchWithCompletionHandler: but the completion handler was never called.
Is this warning normal when i simulate background fetch? My notification still does not appear. How would i fix it?
Upvotes: 1
Views: 94
Reputation: 12617
In iOS 8+, you have to user's permission for local notifications. Add the following line in your application didFinishLaunchingWithOptions
method:
[[UIApplication sharedApplication] registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeAlert|UIUserNotificationTypeBadge| UIUserNotificationTypeSound categories:nil]];
Upvotes: 1