IOS本地定时推送通知

来源:互联网 发布:淘宝网买卖中心 编辑:程序博客网 时间:2024/06/05 02:08

void scheduleLocalNotification(int secondsAfter, std::stringdescription ,std::string key)
    {
  //获取本地推送数组
  NSArray *localArray =[[UIApplication sharedApplication]scheduledLocalNotifications];

  //声明本地通知对象
//  UILocalNotification*localNotification;

  if (localArray)
  {
   for(UILocalNotification *noti in localArray)
   {
    NSDictionary*dict = noti.userInfo;
    if(dict)
    {
                   NSString* strKey = [NSString stringWithCString:key.c_str()encoding:NSUTF8StringEncoding];
     NSString*inKey = [dict objectForKey:strKey];
           
     if([inKey isEqualToString:strKey])
     {
      return;
     }
    }
   }
  }
//  if (!localNotification)
  {
   NSDate*itemDate = [NSDate dateWithTimeIntervalSinceNow:0];
   UILocalNotification*localNotif = [[UILocalNotification alloc] init];
   if(localNotif == nil)
    return;
   localNotif.fireDate= [itemDate dateByAddingTimeInterval:secondsAfter];
   localNotif.timeZone= [NSTimeZone defaultTimeZone];
   localNotif.alertBody= [NSString stringWithUTF8String:description.c_str()];
   localNotif.alertAction= NSLocalizedString(@"查看", nil);
   localNotif.soundName= UILocalNotificationDefaultSoundName;
   localNotif.applicationIconBadgeNumber= 1;
           NSString* strKey = [NSString stringWithCString:key.c_str()encoding:NSUTF8StringEncoding];
   NSDictionary*infoDict = [NSDictionary dictionaryWithObject:strKeyforKey:strKey];
   localNotif.userInfo= infoDict;
   [[UIApplicationsharedApplication] scheduleLocalNotification:localNotif];
   [localNotifrelease];
  }
    }

0 0
原创粉丝点击