推送通知并不总是触发iOS7中的方法

时间:2014-08-13 10:10:41

标签: ios xcode push-notification apple-push-notifications

我正在使用此方法接收通知:

- (void)application:(UIApplication*)application didReceiveRemoteNotification:(NSDictionary*)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler
{
    [[NSNotificationCenter defaultCenter] postNotificationName:@"reloadComments" object:nil];
}

触发此方法:

- (void) reloadComments:(NSNotification *)notification{

    NSDictionary* dict = [notification.userInfo objectForKey:@"commentNotification"];
    NSString* video_id = [[[dict objectForKey:@"aps"] objectForKey:@"custom"] objectForKey:@"data"];

    NSData* cData = [video_id dataUsingEncoding:NSUTF8StringEncoding];
    NSError *errorJson2;
    NSMutableDictionary *response = [NSJSONSerialization JSONObjectWithData:cData options:kNilOptions error:&errorJson2];

    int number = [[commentsDictionary objectForKey:[NSString stringWithFormat:@"%@", [response objectForKey:@"video_id"]]] intValue];

    number += 1;
    [commentsDictionary setObject:[NSNumber numberWithInt:number] forKey:[NSString stringWithFormat:@"%@", [response objectForKey:@"video_id"]]];
}

我正在解析结果并递增数字。当我通过XCode在我的设备中启动应用程序时,这可以正常工作。如果我发送5个推送通知,则数字为5。

如果我在没有通过XCode启动应用程序的情况下执行相同的操作,则数字未正确递增。

任何人都有这方面的经验,可以指出我正确的方向吗?

1 个答案:

答案 0 :(得分:0)

我在下面添加了completionHandler,它开始工作了。

- (void)application:(UIApplication*)application didReceiveRemoteNotification:(NSDictionary*)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler
{
    [self parsePushNotifications:userInfo];
    completionHandler(UIBackgroundFetchResultNewData);
}