Trinh Tran
Trinh Tran

Reputation: 306

Stop local notification

I have a problem below:

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.

Upvotes: 0

Views: 3430

Answers (2)

Steve
Steve

Reputation: 988

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

Upvotes: 1

Nic
Nic

Reputation: 2864

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

- (void)applicationDidEnterBackground:(UIApplication *)application
{
    [[UIApplication sharedApplication] cancelAllLocalNotifications];
}

Upvotes: 2

Related Questions