码迷,mamicode.com
首页 > 移动开发 > 详细

iOS开发本地通知

时间:2016-08-26 10:44:20      阅读:211      评论:0      收藏:0      [点我收藏+]

标签:

/*
 本地通知:不通过网络,在本地实现的通知,自己发给自己
 远程通知:必须通过网络,使用推送技术(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];
}

 

iOS开发本地通知

标签:

原文地址:http://www.cnblogs.com/WJJ-Dream/p/5809225.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!