iOS开发本地通知

/*
 本地通知:不通过网络,在本地实现的通知,自己发给自己
 远程通知:必须通过网络,使用推送技术(APNs),实现通知
 
 本地通知:
 1.要完成可以接收的通知形式的注册
 
 2.具体通知的设置
 
 3.发送通知
 */
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.
    //注意:iOS8.0之后才需要进行专门的设置
    [self settingLocalNotifacation];
    
    return YES;
}

//设置我可以接收的通知形式
-(void)settingLocalNotifacation{
    UIApplication *app = [UIApplication sharedApplication];
    
    UIUserNotificationSettings *setting = [UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeAlert|UIUserNotificationTypeBadge|UIUserNotificationTypeSound categories:0];
    
    [app registerUserNotificationSettings:setting];
}
@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeSystem];
    btn.frame = CGRectMake(100, 100, 120, 30);
    [btn setTitle:@"发送本地通知" forState:UIControlStateNormal];
    [btn addTarget:self action:@selector(btnClicked:) forControlEvents:UIControlEventTouchUpInside];
    
    [self.view addSubview:btn];
    // Do any additional setup after loading the view, typically from a nib.
}


-(void)btnClicked:(UIButton *)btn{
    //设置本地通知
    UILocalNotification *localnoti = [[UILocalNotification alloc]init];
    
    localnoti.alertTitle = @"放假";
    localnoti.alertBody = @"明天放假";
    
    localnoti.fireDate = [[NSDate date] dateByAddingTimeInterval:10];
    
    //发送通知
    UIApplication *app = [UIApplication sharedApplication];
    [app scheduleLocalNotification:localnoti];
}

 

posted @ 2016-08-26 10:02  J-Vijay  阅读(168)  评论(0编辑  收藏  举报