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

Running a code with Local notification in Xcode

首先让我给你看代码:

-(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");
}

}

}];

}

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

我希望我的问题很清楚。

谢谢

我猜你想在从服务器获得新的 post 或你在最后 5 分钟内获得的所有 post 时显示本地通知,所以简单的答案是 YES ,你可以的。

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];