iOS 定时发本地push 实现

//取消之前所有的本地通知
    [[UIApplication sharedApplication] cancelAllLocalNotifications];
    
    //清空 icon数量
    [UIApplication sharedApplication].applicationIconBadgeNumber = 0;
    
    
    //启动本地通知
    UILocalNotification *notification=[[UILocalNotification alloc] init];
    if (notification!=nil)
    {
        //现在的时间
        NSDate *now=[NSDate date];
        
        
        //获得系统日期
        NSCalendar  * cal=[NSCalendar  currentCalendar];
        NSUInteger  unitFlags=NSDayCalendarUnit|NSMonthCalendarUnit|NSYearCalendarUnit;
        NSDateComponents * conponent= [cal components:unitFlags fromDate:now];
        NSInteger year=[conponent year];
        NSInteger month=[conponent month];
        NSInteger day=[conponent day];
        //NSString *  nsDateString= [NSString  stringWithFormat:@"%4d年%2d月%2d日",year,month,day];
        
 
        
        //获得当天的12:00  时间
        NSString  * nsStringDate12 =  [NSString  stringWithFormat:@"%d-%d-%d-%d-%d-%d",
                            year, month,day, 12, 0, 0  ];
        
        //根据时间字符串获得NSDate
        NSDateFormatter  *dateformatter=[[NSDateFormatter alloc] init];
        [dateformatter setDateFormat:@"YYYY-MM-dd-HH-mm-ss"];
        NSDate  * todayTwelve=[dateformatter dateFromString:nsStringDate12];
        
        //然后比较  now跟  todayTwelve那个大,如果已经过了12点,那就设置明天12点
        NSComparisonResult   dateResult =  [now  compare:todayTwelve ];
        if (dateResult ==  NSOrderedDescending  )
        {
            NSDate  *  tomorrowTwelve =  [todayTwelve  dateByAddingTimeInterval: 24 * 60 * 60];
            
            notification.fireDate =  tomorrowTwelve;
        }
        else
        {
            notification.fireDate= todayTwelve;
        }
        notification.repeatInterval = kCFCalendarUnitDay;
        notification.timeZone=[NSTimeZone defaultTimeZone];
        notification.applicationIconBadgeNumber = 1;
 
        notification.alertBody=@"今天还没上线,好多奖励没领呢";
 
        notification.alertAction = @"打开";
        [[UIApplication sharedApplication]   scheduleLocalNotification:notification];
        [notification release];
    }

- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification
{
    NSLog(@"获得本地通知");
    
    
    //点击提示框的打开
    application.applicationIconBadgeNumber = 0;
    
    
}

  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值