现在的位置: 首页 > 综合 > 正文

iOS 定时发本地push 实现

2017年10月04日 ⁄ 综合 ⁄ 共 1705字 ⁄ 字号 评论关闭
//取消之前所有的本地通知
    [[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;
    
    
}

抱歉!评论已关闭.