#import "AppDelegate.h"
@interface AppDelegate ()
@end
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
UIUserNotificationType type = UIUserNotificationTypeBadge | UIUserNotificationTypeSound | UIUserNotificationTypeAlert;
UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:type categories:nil];
[[UIApplication sharedApplication] registerUserNotificationSettings:settings];
return YES;
}
- (void)applicationWillEnterForeground:(UIApplication *)application
{
NSLog(@"来了");
[UIApplication sharedApplication].applicationIconBadgeNumber = 0;
}
@end
#import "ViewController.h"
#define MyUserLocalNotificationIdentifer @"MyUserLocalNotificationIdentifer"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
}
- (IBAction)btnAction:(id)sender
{
UILocalNotification *notification = [[UILocalNotification alloc] init];
notification.fireDate = [NSDate dateWithTimeIntervalSinceNow:4];
notification.alertBody = @"天气好晴朗, 该出去走走了";
notification.alertTitle = @"要吃糖";
notification.soundName = @"buyao.wav";
notification.applicationIconBadgeNumber = 6;
notification.repeatInterval = NSCalendarUnitMinute;
notification.userInfo = @{
MyUserLocalNotificationIdentifer : @"ZhaoZhao"
};
[[UIApplication sharedApplication] scheduleLocalNotification:notification];
}
- (IBAction)seeBtnAction:(id)sender
{
NSArray <UILocalNotification *> *notifications = [[UIApplication sharedApplication] scheduledLocalNotifications];
NSLog(@"%@", notifications);
}
- (IBAction)removeBtnAction:(id)sender
{
NSArray <UILocalNotification *> *notications = [[UIApplication sharedApplication] scheduledLocalNotifications];
for (UILocalNotification *notification in notications) {
[notification.userInfo[MyUserLocalNotificationIdentifer] isEqualToString:@"ZhaoZhao"];
[[UIApplication sharedApplication] cancelLocalNotification:notification];
}
}
- (IBAction)removeAllBtnAction:(id)sender
{
[[UIApplication sharedApplication] cancelAllLocalNotifications];
}
@end