ios 本地通知

在iOS中 相对于远程通知 本地通知要简单得多 只有两个地方写点代码就能实现

本地通知(UILocalNotification) 操作流程

 1.接收通知(接收通知对象)

 -(void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification{

    //    接收通知操作

 }

 2.注册发送通知(需注意iOS8之后的改动)


- (void)pushNotification
{
//    注册发送通知的方法
//    初始化 UILocalNotification alloc init
    UILocalNotification *not = [[UILocalNotification alloc]init];
//    设置启动时间
    not.fireDate = [NSDate dateWithTimeIntervalSinceNow:10];
//    设置通知的标题
    not.alertTitle = @"时间到";
//    设置通知的内容
    not.alertBody = @"起床敲代码";
//    通过通知 传递内容
    not.userInfo = @{@"key":@"value"};
//    设置app图标上面红点显示的数字
    not.applicationIconBadgeNumber = 1;
//    注册通知
//    iOS8之前
    not.soundName = UILocalNotificationDefaultSoundName;
    
//    NSCalendarUnitEra                = kCFCalendarUnitEra,
//    NSCalendarUnitYear               = kCFCalendarUnitYear,
//    NSCalendarUnitMonth              = kCFCalendarUnitMonth,
//    NSCalendarUnitDay                = kCFCalendarUnitDay,
//    NSCalendarUnitHour               = kCFCalendarUnitHour,
//    NSCalendarUnitMinute             = kCFCalendarUnitMinute,
//    NSCalendarUnitSecond             = kCFCalendarUnitSecond,
//    NSCalendarUnitWeekday            = kCFCalendarUnitWeekday,
//    NSCalendarUnitWeekdayOrdinal     = kCFCalendarUnitWeekdayOrdinal,
    
    not.repeatInterval = NSCalendarUnitDay;
//    判断是否可以响应某个方法
//    [self respondsToSelector:<#(SEL)#>]
//    UIUserNotificationTypeBadge| 圆圈内提示的数字
//    UIUserNotificationTypeSound| 通知提示的声音
//    UIUserNotificationTypeNone|
//    UIUserNotificationTypeAlert  振动
    if ([[UIApplication sharedApplication] respondsToSelector:@selector(registerUserNotificationSettings:)]) {
        [[UIApplication sharedApplication]registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeBadge|UIUserNotificationTypeSound|UIUserNotificationTypeAlert categories:nil]];
    }
//    发送通知
    [[UIApplication sharedApplication]scheduleLocalNotification:not];
    
}

//接收本地通知
-(void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification
{
//    接收通知操作
    NSLog(@"通知内容 %@",notification.userInfo);
    UIAlertView *alert = [[UIAlertView alloc]initWithTitle:notification.alertTitle message:notification.alertBody delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
    [alert show];
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值