本地通知
在 按鈕方法 中 添加如下 代碼
//創建本地通知對象
UILocalNotification *localNotification = [[UILocalNotification alloc] init];
//設定調度時間,即通知五秒后執行
NSDate *nowDate = [NSDate date];
[localNotification setFireDate:[nowDate dateByAddingTimeInterval:5]];
//設定時區
[localNotification setTimeZone:[NSTimeZone defaultTimeZone]];
//設置彈出消息
[localNotification setAlertBody:@"搶購時間到了"];
[localNotification setAlertAction:@"show now"];
//設置通知聲音
[localNotification setSoundName:UILocalNotificationDefaultSoundName];
//設置IconbadgeNumber
[localNotification setApplicationIconBadgeNumber:1];
//應用程序計劃執行通知
[[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
IOS9之后 推出的 通知框
//通知框 ios 9
UIAlertController *alertController=[UIAlertController alertControllerWithTitle:@"標題" message:@"UIAlertController" preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
NSLog(@"通知框");
}];
[alertController addAction:okAction];
[self presentViewController:alertController animated:YES completion:nil];
在 AppDelegate 中添加
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
//判斷當前設備的版本
if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0) {
UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeBadge |UIUserNotificationTypeSound | UIUserNotificationTypeAlert categories:nil];
[[UIApplication sharedApplication] registerUserNotificationSettings:settings];
}
return YES;
}
//接收到的本地通知
-(void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification {
// [self.window.rootViewController presentViewController:<#(nonnull UIViewController *)#> animated:<#(BOOL)#> completion:<#^(void)completion#>];
//將badge標記數減1
[UIApplication sharedApplication].applicationIconBadgeNumber--;
// self.window.rootViewController = [];
}