通知

本地通知

按钮方法 中 添加如下 代码

//创建本地通知对象
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 = [];

}

你可能感兴趣的:(通知)