I have an app with local notification at the App Store.
In the last 4 version updates to my app, there were customers who experienced something strange - the app lo
I had similar issues with notifications. My solution to actually delete was to set a specific date and only notify once. The line applicationIconBadgeNumber = 0 removes the notification from notifications list. The badge number is only set when the user entered the app. For the case when the user didn't see the message yet you can just add a check in applicationWillEnterForeground and display a proper UIAlertView with the same message as the notification, and you can get the last notification in a similar way by using notificationArray. When you need to set a new notification "isNotified" needs setValue:@"0" and be synchronized. When I use multiple notifications I save states like this:
[userDefaults setValue:@"1" forKey:[NSString stringWithFormat:@"notification-%@", @"12"]];
NSInteger number = [[userDefaults objectForKey:[NSString stringWithFormat:@"notification-%@", @"12"]] integerValue];
In MainViewController.m
NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
NSInteger alreadyNotified = [[userDefaults objectForKey:@"isNotified"] integerValue];
if (alreadyNotified == 0) {
NSInteger seconds = 60;
NSString *message = @"Just testing!";
UILocalNotification *notification = [[UILocalNotification alloc] init];
UIUserNotificationSettings *currentSettings = [[UIApplication sharedApplication] currentUserNotificationSettings];
BOOL allowNotif = (currentSettings.types != UIUserNotificationTypeNone);
BOOL allowsSound = (currentSettings.types & UIUserNotificationTypeSound) != 0;
BOOL allowsBadge = (currentSettings.types & UIUserNotificationTypeBadge) != 0;
BOOL allowsAlert = (currentSettings.types & UIUserNotificationTypeAlert) != 0;
if (notification)
{
if (allowNotif)
{
NSDate *now = [NSDate date];
if (seconds > 0) {
now = [now dateByAddingTimeInterval:seconds];
}
notification.fireDate = now;
notification.timeZone = [NSTimeZone timeZoneWithName:@"UTC"];
notification.repeatInterval = 0;
NSDictionary *userInfo = [NSDictionary dictionaryWithObjectsAndKeys:@"1", @"id", message, @"message", nil];
notification.userInfo = userInfo;
}
if (allowsAlert)
{
notification.alertBody = message;
}
if (allowsBadge)
{
notification.applicationIconBadgeNumber = 1;
}
if (allowsSound)
{
notification.soundName = UILocalNotificationDefaultSoundName;
}
[[UIApplication sharedApplication] scheduleLocalNotification:notification];
NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
[userDefaults setValue:@"1" forKey:@"isNotified"];
[userDefaults synchronize];
}
} else {
UIApplication *app = [UIApplication sharedApplication];
NSArray *notificationArray = [app scheduledLocalNotifications];
for (int i=0; i<[notificationArray count]; i++)
{
UILocalNotification *notification = [notificationArray objectAtIndex:i];
[app cancelLocalNotification:notification];
}
}
In AppDelegate.m
- (void)applicationWillEnterForeground:(UIApplication *)application {
application.applicationIconBadgeNumber = 0;
}