didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void(^)(UIBackgroundFetchResult))completionHandler未调用

时间:2014-11-20 18:24:09

标签: ios objective-c push-notification

我正在创建一个接收推送通知的应用程序,一旦收到推送通知,它将在应用程序处于活动状态时正常处理,或者如果它未运行则在后台处理。

最烦人的部分 - 当我处于开发模式并通过XCode运行时,该应用程序正常工作。但是,当我进行Ad Hoc构建时,应用程序仅在应用程序运行时才会收到通知。我的代码处理通知如下,有人可以对此有所了解:

-(void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler {
MySharedClass *shared = [MySharedClass sharedManager];

UIApplicationState state = [application applicationState];
if (state == UIApplicationStateActive) {
    shared.localNotif = userInfo;
    if (shared.localNotif) {
        [shared ReceivedPushNotification];
    }
} else {
    __block UIBackgroundTaskIdentifier background_task;
    background_task = [application beginBackgroundTaskWithExpirationHandler:^ {

        //Clean up code. Tell the system that we are done.
        [application endBackgroundTask: background_task];
        background_task = UIBackgroundTaskInvalid;
    }];

    //To make the code block asynchronous
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{

        //### background task starts
        NSLog(@"Running in the background\n");
        completionHandler(UIBackgroundFetchResultNewData);

        shared.localNotif = userInfo;
        if (shared.localNotif) {
            [shared ReceivedPushNotification];
        }
        while(TRUE)
        {
            NSLog(@"Background time Remaining: %f",[[UIApplication sharedApplication] backgroundTimeRemaining]);
            [NSThread sleepForTimeInterval:1]; //wait for 1 sec
        }
        //#### background task ends

        //Clean up code. Tell the system that we are done.
        [application endBackgroundTask: background_task];
        background_task = UIBackgroundTaskInvalid;
    });
}    
}

0 个答案:

没有答案