分享

iOS 定时发本地push 实现

 plumbiossom 2014-09-01
  1. //取消之前所有的本地通知  
  2.     [[UIApplication sharedApplication] cancelAllLocalNotifications];  
  3.       
  4.     //清空 icon数量  
  5.     [UIApplication sharedApplication].applicationIconBadgeNumber = 0;  
  6.       
  7.       
  8.     //启动本地通知  
  9.     UILocalNotification *notification=[[UILocalNotification alloc] init];  
  10.     if (notification!=nil)  
  11.     {  
  12.         //现在的时间  
  13.         NSDate *now=[NSDate date];  
  14.           
  15.           
  16.         //获得系统日期  
  17.         NSCalendar  * cal=[NSCalendar  currentCalendar];  
  18.         NSUInteger  unitFlags=NSDayCalendarUnit|NSMonthCalendarUnit|NSYearCalendarUnit;  
  19.         NSDateComponents * conponent= [cal components:unitFlags fromDate:now];  
  20.         NSInteger year=[conponent year];  
  21.         NSInteger month=[conponent month];  
  22.         NSInteger day=[conponent day];  
  23.         //NSString *  nsDateString= [NSString  stringWithFormat:@"%4d年%2d月%2d日",year,month,day];  
  24.           
  25.    
  26.           
  27.         //获得当天的12:00  时间  
  28.         NSString  * nsStringDate12 =  [NSString  stringWithFormat:@"%d-%d-%d-%d-%d-%d",  
  29.                             year, month,day, 12, 0, 0  ];  
  30.           
  31.         //根据时间字符串获得NSDate  
  32.         NSDateFormatter  *dateformatter=[[NSDateFormatter alloc] init];  
  33.         [dateformatter setDateFormat:@"YYYY-MM-dd-HH-mm-ss"];  
  34.         NSDate  * todayTwelve=[dateformatter dateFromString:nsStringDate12];  
  35.           
  36.         //然后比较  now跟  todayTwelve那个大,如果已经过了12点,那就设置明天12点  
  37.         NSComparisonResult   dateResult =  [now  compare:todayTwelve ];  
  38.         if (dateResult ==  NSOrderedDescending  )  
  39.         {  
  40.             NSDate  *  tomorrowTwelve =  [todayTwelve  dateByAddingTimeInterval: 24 * 60 * 60];  
  41.               
  42.             notification.fireDate =  tomorrowTwelve;  
  43.         }  
  44.         else  
  45.         {  
  46.             notification.fireDate= todayTwelve;  
  47.         }  
  48.         notification.repeatInterval = kCFCalendarUnitDay;  
  49.         notification.timeZone=[NSTimeZone defaultTimeZone];  
  50.         notification.applicationIconBadgeNumber = 1;  
  51.    
  52.         notification.alertBody=@"今天还没上线,好多奖励没领呢";  
  53.    
  54.         notification.alertAction = @"打开";  
  55.         [[UIApplication sharedApplication]   scheduleLocalNotification:notification];  
  56.         [notification release];  
  57.     }  
  58.   
  59. - (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification  
  60. {  
  61.     NSLog(@"获得本地通知");  
  62.       
  63.       
  64.     //点击提示框的打开  
  65.     application.applicationIconBadgeNumber = 0;  
  66.       
  67.       
  68. }  



    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章