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
Hey that is what is also happening with my app as well. My local notifications are getting fired in below scenerios :
1. user has deleted the app
-Actually its UILocalNotification
behave, we have to cancel them else IOS retain them for at least some time(not sure how long) when app is deleted and they were scheduled by the app at deletion time and were not canceled.
So always make sure when ever your app install happens,keep a check & cancel all scheduled notifications.
like
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
if(//its a fresh install){
[[UIApplication sharedApplication] cancelAllLocalNotifications];
}
}
cancelAllLocalNotifications
will cancel all existing notifications.
Hope it will help.
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;
}
Did you check How does UIApplication cancelAllLocalNotifications work between app updates? It doesn't have definitive answers, but maybe it would be useful.
Try to find out what's so special about the customer that experience this:
Add the following code to your application:didFinishLaunchingWithOptions: method to clear all the notification.
[[UIApplication sharedApplication] setApplicationIconBadgeNumber: 1];
[[UIApplication sharedApplication] setApplicationIconBadgeNumber: 0];
[[UIApplication sharedApplication] cancelAllLocalNotifications];
Once done you can add the new notifications.