0

I have a problem below:

  • When minimize app to background by press home button, create a local notification for pop-up every 5 minutes.
  • Remove app from background. -->My expected then pup-up just only show when app exist and it's discarded when remove app from background.

My issue that local notification still active and it still showing pop-up every 5 minutes after remove it from background.

How can i stop it? Please help me! Thanks in advanced.

Trinh Tran
  • 306
  • 3
  • 7
  • Why are you using local notifications when you only want them to appear when your app is running? The whole point of local notifications is that they appear at the designated time whether or not your app is running. – Stephen Darlington Jul 23 '12 at 09:04
  • Do you have a any suggestion when i want to pop-up from background.In my example it only 5 minutes and may be 10 minutes or 20 minutes. We also know a limitation about timer when using app in background. – Trinh Tran Jul 23 '12 at 09:26
  • I'm sorry, but I think you're trying to solve the wrong problem. In general, users don't know which apps are in memory and which have been terminated. Just because an app is listed in the "task manager" doesn't mean that it's running or in memory. What I'm saying is that you shouldn't be trying to adjust the behaviour depending on whether the app is in memory or not. To a user, background (in memory) and killed should look exactly the same. – Stephen Darlington Jul 23 '12 at 10:03

2 Answers2

2

Put this in application delegate. It will remove all local notifications when the application enters background.

- (void)applicationDidEnterBackground:(UIApplication *)application
{
    [[UIApplication sharedApplication] cancelAllLocalNotifications];
}
Nic
  • 2,864
  • 1
  • 16
  • 21
1

If you don't want to cancel all notifications... I've set up a unique identifier stored in the notification's userInfo dictionary. When I want to delete I fast enumerate through all notifications and pick out the correct one for deletion.

My stumbling blocks here were remembering to store the UUID I'd created for the notification and also remembering to use isEqualToString in the fast enumeration. I guess I could also have used a specific name string instead of a unique identifier. If anyone can let me know a better method than fast enumerating please let me know.

@interface myApp () {
    NSString *storedUUIDString; 
}

- (void)viewDidLoad {
    // create a unique identifier - place this anywhere but don't forget it! You need it to identify the local notification later
    storedUUIDString = [self createUUID]; // see method lower down
}

// Create the local notification
- (void)createLocalNotification {
    UILocalNotification *localNotif = [[UILocalNotification alloc] init];
    if (localNotif == nil) return;
    localNotif.fireDate = [self.timerPrototype fireDate];
    localNotif.timeZone = [NSTimeZone defaultTimeZone];
    localNotif.alertBody = @"Hello world";
    localNotif.alertAction = @"View"; // Set the action button
    localNotif.soundName = UILocalNotificationDefaultSoundName;
    NSDictionary *infoDict = [NSDictionary dictionaryWithObject:storedUUIDString forKey:@"UUID"];
    localNotif.userInfo = infoDict;

    // Schedule the notification and start the timer
    [[UIApplication sharedApplication] scheduleLocalNotification:localNotif]; 
}

// Delete the specific local notification
- (void) deleteLocalNotification { 
// Fast enumerate to pick out the local notification with the correct UUID
    for (UILocalNotification *localNotification in [[UIApplication sharedApplication] scheduledLocalNotifications]) {        
    if ([[localNotification.userInfo valueForKey:@"UUID"] isEqualToString: storedUUIDString]) {
        [[UIApplication sharedApplication] cancelLocalNotification:localNotification] ; // delete the notification from the system            
        }
    }
}

// Create a unique identifier to allow the local notification to be identified
- (NSString *)createUUID {
    CFUUIDRef theUUID = CFUUIDCreate(NULL);
    CFStringRef string = CFUUIDCreateString(NULL, theUUID);
    CFRelease(theUUID);
    return (__bridge NSString *)string;
}

Most of the above has probably been lifted from StackOverflow at sometime in the last 6months. Hope this helps

Steve
  • 988
  • 1
  • 12
  • 25
  • I think your way work if app still alive (focused or minimized). But what happen when create a notification -> minimize -> delete app from background. Does your app still showing a notification ? – Trinh Tran Jul 23 '12 at 09:31
  • Sorry, this doesn't seem to be possible as of March 2011: linkhttp://stackoverflow.com/questions/5398191/cancelalllocalnotifications-in-applicationwillterminate – Steve Jul 23 '12 at 09:45
  • - Might be different now. Workaround suggestions: Limit the number of notifications rather than continual repeat, ensure the user returns to the app at some point during these scheduled notifications and if the notifications are to continue reset them then. There ends my experience I'm afraid : ) Might be different now. Workaround suggestions: Limit the number of notifications rather than continual repeat, ensure the user returns to the app at some point during these scheduled notifications and if the notifications are to continue reset them then. There ends my experience I'm afraid : ) – Steve Jul 23 '12 at 10:02
  • Steven, Thanks for your help but i has not still understood you mean. – Trinh Tran Jul 23 '12 at 10:46
  • Hmmm, it's a good question. It isn't possible to remove the notification if the application terminates unexpectedly by the user, or low memory, and I've just discovered that it also isn't possible to set a notification to repeat for a fixed number of times. You'll have to create separate individual notifications to simulate repeating, prompt the user to return to the app before they run out to recreate that queue - http://stackoverflow.com/questions/5762517/custom-repeat-interval-for-uilocalnotification – Steve Jul 23 '12 at 12:28
  • "Legacy" notifications will be inevitable if the user removes the app but at least they won't continue forever. iOS has a 64 notification limit for each app. Interestingly that means that calendar apps for someone with a busy calendar only store 64 notifications before the user has to bring the app to the foreground to recreate that upcoming notification queue - unless push is used. Got to work now. Good luck. Please post the solution / workaround. Will do the same if I come across one. Steve – Steve Jul 23 '12 at 12:32
  • It's hard for a recognition when the user tries to delete our app from background. I don't know have any tricks when [Downcast](http://itunes.apple.com/us/app/downcast/id393858566?mt=8) has done. – Trinh Tran Jul 24 '12 at 03:55