在Xcode中运行带有本地通知的代码

时间:2015-04-02 05:44:18

标签: ios objective-c uilocalnotification

首先让我告诉你代码:

-(void)checkIfNewTopics {

// to load the number of topics
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSString *totalQasida = [defaults objectForKey:@"totalQasida"];
NSLog(@"Qasida number after did load %@", totalQasida);


NSString *queryString = [NSString stringWithFormat:@"http://myWebSite/CountTopics.php"];
NSURLRequest *theRequest=[NSURLRequest
                              requestWithURL:[NSURL URLWithString:
                                              queryString]
                              cachePolicy:NSURLRequestUseProtocolCachePolicy
                              timeoutInterval:60.0];
[NSURLConnection sendAsynchronousRequest:theRequest queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) {
if (error) {

} else {
NSString *responseText = [[NSString alloc] initWithData:data encoding: NSASCIIStringEncoding];


if ([responseText isEqualToString:@"null"]) {


}else {


}

NSString *newLineStr = @"\n";
responseText = [responseText stringByReplacingOccurrencesOfString:@"<br />" withString:newLineStr];



NSLog(@"The new number of topics is %@", responseText);

int lastTopicNumber = [responseText intValue];
int oldTopicNumber = [totalQasida intValue];
int subtract = lastTopicNumber - oldTopicNumber;


if (lastTopicNumber > oldTopicNumber) {

UITabBarItem *tbi = (UITabBarItem*)[[[self.tabBarController tabBar] items] objectAtIndex:0];            
NSString *strFromInt = [NSString stringWithFormat:@"%d",subtract];
[tbi setBadgeValue:strFromInt];


}
else {

NSLog(@“No new topics");
}

}

}];

}

上面的代码是检查用户是否发布了新主题,我的问题是:我可以将此代码与本地通知一起使用。例如,如果找到新主题,我需要每5分钟运行一次通知。

我希望我的问题很明确。

由于

1 个答案:

答案 0 :(得分:0)

正如我猜您想要显示本地通知,因为您从服务器收到新帖子或者您在最后5分钟收到的所有帖子,所以简单的答案是,您可以这样做。

UILocalNotification *localNotification = [[UILocalNotification alloc] init];
    localNotification.fireDate = [NSDate dateWithTimeIntervalSinceNow:5];
    localNotification.alertBody = // set your post here;
    localNotification.timeZone = [NSTimeZone defaultTimeZone];
    localNotification.applicationIconBadgeNumber = [[UIApplication sharedApplication] applicationIconBadgeNumber] + 1;
    [[UIApplication sharedApplication] scheduleLocalNotification:localNotification];