Search code examples
iosobjective-cnotificationsuilocalnotification

Can I not show an UILocalNotification?


I have an objective c application. I show UILocalNotifications in a time interval. But then added this, I want not show determined notifications.

For example, I add a key and value to notifications, and if the notifications received in didReceiveLocalNotification function has the key=2 I want show, but if the notifications has the key=1 I want not show this.

Is this possible?

I'm trying this with this code, but always are showed all notifications:

UILocalNotification *notification = [[UILocalNotification alloc] init];
        //notification.fireDate = [[NSDate date] dateByAddingTimeInterval:1];
        notification.alertBody = @"Hello!";
        notification.soundName =  @"Alarm-Clock.caf";
        NSDictionary *infoDict = [NSDictionary dictionaryWithObjectsAndKeys:@"key1", @"1", nil];
        notification.userInfo = infoDict;
        notification.repeatInterval = NSCalendarUnitMinute;
        [[UIApplication sharedApplication] scheduleLocalNotification:notification];


- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification {
if([[notification.userInfo valueForKey:@"1"] isEqual:@"key1"]){
            [[UIApplication sharedApplication] cancelLocalNotification:notification];
        }

Solution

  • You might want to use the following, but you need to do it before the UILocationNotification is fired, not after.

    for (UILocalNotification *localNotification in [[UIApplication sharedApplication] scheduledLocalNotifications]) {
    
        if ([[localNotification.userInfo valueForKey:@"1"] isEqual:@"key1"]) {
    
            [[UIApplication sharedApplication] cancelLocalNotification:localNotification];
    
        }
    
    }
    

    Hope this helps.

    Cheers.