简体   繁体   中英

Unable to Cancel NSLocalNotification and call NSLocalNotification within the app

I have the following code, but after the notification comes out, i can't cancel the Icon Badge. What code should i add in order to launch the application, cancel the notification and reset the icon badge?

Another question is why is the notification only appear at home screen of the phone? Within the application the notification won't show up. Thanks.

display.text=[NSString stringWithFormat:@"%@A",display.text];
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Utilities" message:@"Alarm added" delegate:self cancelButtonTitle:@"Done" otherButtonTitles:nil];
    // optional - add more buttons:

    [alert show];

    UILocalNotification *localNotification = [[UILocalNotification alloc] init]; //Create the localNotification object

    NSDate *date = [NSDate date];

    // Add one minute to the current time
    NSDate *dateToFire = [date dateByAddingTimeInterval:20];

    // Set the fire date/time
    [localNotification setFireDate:dateToFire];
    [localNotification setTimeZone:[NSTimeZone defaultTimeZone]];


    [localNotification setAlertAction:@"Done"]; 
    [localNotification setAlertBody:@"(A)"]; 
    [localNotification setHasAction: YES]; 
    [localNotification setSoundName:UILocalNotificationDefaultSoundName];

    [localNotification setApplicationIconBadgeNumber:[[UIApplication sharedApplication] applicationIconBadgeNumber]+1]; 

    [[UIApplication sharedApplication] scheduleLocalNotification:localNotification]; 

To remove the badge number you can use this

[UIApplication sharedApplication].applicationIconBadgeNumber = 0;

&

to Cancel all local notifications with this code:

[[UIApplication sharedApplication] cancelAllLocalNotifications];

Cancel one local notification with this line of code:

[[UIApplication sharedApplication] cancelLocalNotification:theNotification];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM