If you want to fire local notification e.g.: after 5 second use this code:
- (void)applicationDidEnterBackground:(UIApplication *)application {
UILocalNotification* localNotification = [UILocalNotification new];
localNotification.fireDate = [[NSDate new] dateByAddingTimeInterval:5]; // Notification will be fired after 5 since now.
localNotification.alertBody = @"Your alert message";
localNotification.timeZone = [NSTimeZone defaultTimeZone];
[[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
}
NOTICE: If you want to receive notifications you have to ask user for permission e.g.:
UIUserNotificationType types = UIUserNotificationTypeBadge |
UIUserNotificationTypeSound | UIUserNotificationTypeAlert;
UIUserNotificationSettings *mySettings =
[UIUserNotificationSettings settingsForTypes:types categories:nil];
[[UIApplication sharedApplication] registerUserNotificationSettings:mySettings];
permission code listed above can be invoked in your AppDelegate.m
e.g.: in didFinishLaunchingWithOptions
method. You have to know that performSelector
method is not suitable in this case, every UILocalNotification
has fireDate
property which should be used to fire notification with given NSDate
delay.
UPDATE
Today I had the same problem, I passed as alertBody
empty string and notification didn't appears. So in my opinion your
NSLocalizedString(@"You are going to be disconnected. To continue being online, please open the application",nil);
returns empty string or nil.
Please try assign to alertBody
property any other string e.g.
localNotification.alertBody = @"test"
it should works :)
Part of Apple doc:
Declaration SWIFT
var alertBody: String?
Discussion Assign a string
or, preferably, a localized-string key (using NSLocalizedString) as
the value of the message. If the value of this property is non-nil, an
alert is displayed. The default value is nil (no alert). Printf style
escape characters are stripped from the string prior to display; to
include a percent symbol (%) in the message, use two percent symbols
(%%).