本地通知 LocalNotification
在Appdelegate中实现以下两个方法
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { // Override point for customization after application launch. //设置桌面红点内的数字 // application.applicationIconBadgeNumber = 0; //请求通知权限 UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeAlert | UIUserNotificationTypeSound | UIUserNotificationTypeBadge) categories:nil]; [application registerUserNotificationSettings:settings]; return YES; }
收到本地通知的代理方法
1.点击本地通知弹框
2.程序在前台, 收到本地通知
- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification { NSLog(@"%@", notification); }
添加一个button到ViewController中
#import "ViewController.h" @interface ViewController () - (IBAction)localNoti:(id)sender; @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } - (IBAction)localNoti:(id)sender { //创建本地通知 UILocalNotification *notification = [[UILocalNotification alloc] init]; //提示框内容 notification.alertBody = @"起床啦"; //提示框标题 notification.alertTitle = @"醒啦"; //通知声音 notification.soundName = UILocalNotificationDefaultSoundName; //触发时间 notification.fireDate = [NSDate dateWithTimeIntervalSinceNow:10]; //icon旁边的数字 notification.applicationIconBadgeNumber = [UIApplication sharedApplication].applicationIconBadgeNumber + 1; //本地通知重复 notification.repeatInterval = NSCalendarUnitMinute; //发送通知 [[UIApplication sharedApplication] scheduleLocalNotification:notification]; } @end
cmd + shift + H 退出程序, 本地通知就弹出
时间: 2024-10-20 21:31:31