iOS8本地推送

作者: 郝嗨森 | 来源:发表于2016-08-29 21:24 被阅读119次

    本地推送与远程推送相比要简单的多,不需要导入任何文件和系统库,但其功能也因此而受限,只能设置固定的时间进行推送,而且推送内容也不能改.

    在Appdelegate.m中先进行注册
    UIApplication * app = [UIApplication sharedApplication];
    //UIUserNotificationTypeAlert  收到推送时会有弹窗
    //UIUserNotificationTypeBadge  APP图标出显示消息数量(99+)
    //UIUserNotificationTypeSound    收到推送时会有声音提示
    UIUserNotificationSettings * settings = [UIUserNotificationSettings  settingsForTypes:UIUserNotificationTypeAlert | UIUserNotificationTypeBadge | UIUserNotificationTypeSound categories:nil];
    //注册本地推送的属性设置
    [app registerUserNotificationSettings:settings];
    
    //在进行推送的地方进行设置
    //创建通知对象
    UILocalNotification * localNotification = [[UILocalNotification alloc] init];
    //设置属性
    //发送的时间
    localNotification.fireDate = [NSDate dateWithTimeIntervalSinceNow:10];
    //通知的标题
    localNotification.alertTitle = @"通知";  //IOS_8.2
    //发送的内容
    localNotification.alertBody = @"本地通知测试";
    //通知的声音
    localNotification.soundName = UILocalNotificationDefaultSoundName;
    //设置APP图标显示的消息数量
    localNotification.applicationIconBadgeNumber = 1;
    //发送通知
    UIApplication * app = [UIApplication sharedApplication];
    [app scheduleLocalNotification:localNotification];
    

    以上就是关于本地推送的基本实现,因为通常使用本地推送都会设置为固定的时间进行推送,所以下面补充一个关于获取当前时间的各个字段的方法:

    //创建一个日历对象
    NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
    //获取当前时间
    NSDate *now = [NSDate date];
    //创建一个存储日期各要素的对象
    NSDateComponents *comps = [[NSDateComponents alloc] init];
    //用来存储要获取的日期中的各要素
    NSCalendarUnit unitFlags = NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay | NSCalendarUnitWeekday |
    NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond;
    //从当前时间中取出要得到的日期元素
    comps = [calendar components:unitFlags fromDate:now];
    //分别获取各日期元素
    NSInteger year = [comps year];
    NSInteger month = [comps month];
    NSInteger week = [comps weekday];
    NSInteger day = [comps day];
    NSInteger hour = [comps hour];
    NSInteger min = [comps minute];
    NSInteger sec = [comps second];
    

    以上仅供参考,本人能力有限,如有问题请指出,谢谢.

    相关文章

      网友评论

        本文标题:iOS8本地推送

        本文链接:https://www.haomeiwen.com/subject/iccfettx.html