本地推送

本文介绍了一个iOS应用中实现本地推送的具体方法,包括按钮点击触发通知、设置通知内容及时间,并展示了如何通过UIAlertView展示接收到的通知。


在项目中需要用上本地推送,测试了下,发现有问题,居然无法实现,检查了下,也在网上找了写资料,发现代码是没有问题的,可就是无法执行!

换了太设备测试了下,没有任何的问题,一下是正确代码

或许是设备有问题,如果那位大神知道,求回复!


- (void)viewDidLoad{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    UIButton *but = [[UIButton alloc] initWithFrame:CGRectMake(30, 100, 100, 30)];
    but.backgroundColor = [UIColor blueColor];
    [but addTarget:self action:@selector(vvvvvvvvvv) forControlEvents:UIControlEventTouchDown];
    [but setTitle:@"通知" forState:UIControlStateNormal];
    [but setTitleColor:[UIColor redColor] forState:UIControlStateHighlighted];
    [self.view addSubview:but];
}

-(void)vvvvvvvvvv{
    
    UILocalNotification *notification = [[UILocalNotification alloc] init];
    if (notification) {
        
        // 设置通知的提醒时间
        NSDate *currentDate   = [NSDate date];
        notification.timeZone = [NSTimeZone defaultTimeZone]; // 使用本地时区
        notification.fireDate = [currentDate dateByAddingTimeInterval:3];
        
        // 设置重复间隔 不重复
        notification.repeatInterval = 0;
        
        // 设置提醒的文字内容
        notification.alertBody = @"hello,boy!";
        
        // 通知提示音 使用默认的
        notification.soundName= UILocalNotificationDefaultSoundName;
        // 将通知添加到系统中
        [[UIApplication sharedApplication] scheduleLocalNotification:notification];
    }

}


// app delegate //
-(void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification{
    
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Hello"
                                                    message:notification.alertBody
                                                   delegate:nil
                                          cancelButtonTitle:@"OK"
                                          otherButtonTitles:nil, nil];
    [alert show];
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值