didReceiveRemoteNotification:fetchCompletionHandler not being called when app is in background and not connected to Xcode

前端 未结 10 1606
别那么骄傲
别那么骄傲 2020-11-28 04:27

I\'ve a very strange problem, I implemented:

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchComp         


        
相关标签:
10条回答
  • 2020-11-28 05:17

    To use Background Push Download in iOS application development, here are some important points which we need to follow…

    1. Enable UIBackgroundModes key with remote-notification value in info.plist file.

    2. Then implement below method in your AppDelegate file.

      application:didReceiveRemoteNotification:fetchCompletionHandler

    More Details:ios-7-true-multitasking

    0 讨论(0)
  • 2020-11-28 05:20

    Possible reason is that Background App Refresh is off on your iPhone.
    You can turn this option on/off in Settings->General->Background App Refresh.
    When Background App Refresh is off on your phone, didReceiveRemoteNotification:fetchCompletionHandler method will be called only when the phone is connected to XCode.

    0 讨论(0)
  • 2020-11-28 05:20

    Code that works fetching remote notifications, enable te remote notifications capability in background modes and i have background fetch enabled too (i don't know if it is necessary) I use this code:

    - (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult result))handler{
    DLog(@"899- didReceiveRemoteNotification userInfo: %@",userInfo);
    NSDictionary *custom=userInfo[@"custom"];
    if(custom){
        NSInteger code = [custom[@"code"] integerValue];
        NSInteger info = [custom[@"info"] integerValue];
    
        NSDictionary *messageInfo = userInfo[@"aps"];
    
        [[eInfoController singleton] remoteNotificationReceived:code info:info messageInfo:messageInfo appInBackground:[UIApplication sharedApplication].applicationState==UIApplicationStateBackground];
    
        handler(UIBackgroundFetchResultNewData);
    }
    }
    
    - (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo{
    DLog(@"899- didReceiveRemoteNotification userInfo: %@",userInfo);
    NSDictionary *custom=userInfo[@"custom"];
    if(custom){
        NSInteger code = [custom[@"code"] integerValue];
        NSInteger info = [custom[@"info"] integerValue];
    
        NSDictionary *messageInfo = userInfo[@"aps"];
    
        [[eInfoController singleton] remoteNotificationReceived:code info:info messageInfo:messageInfo appInBackground:[UIApplication sharedApplication].applicationState==UIApplicationStateBackground];
    
    }
    }
    
    - (void)application:(UIApplication*)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData*)deviceToken
    {
    //NSLog(@"My token is: %@", deviceToken);
    const unsigned *tokenBytes = (const unsigned *)[deviceToken bytes];
    NSString *hexToken = [NSString stringWithFormat:@"%08x%08x%08x%08x%08x%08x%08x%08x",
                          ntohl(tokenBytes[0]), ntohl(tokenBytes[1]), ntohl(tokenBytes[2]),
                          ntohl(tokenBytes[3]), ntohl(tokenBytes[4]), ntohl(tokenBytes[5]),
                          ntohl(tokenBytes[6]), ntohl(tokenBytes[7])];
    
    [[eInfoController singleton] setPushNotificationToken:hexToken];
    }
    
    - (void)application:(UIApplication*)application didFailToRegisterForRemoteNotificationsWithError:(NSError*)error
    {
    NSLog(@"Failed to get token, error: %@", error);
    }
    

    Code that stores the notification when it background, the key for me was to start a background download task to allow me to download the information in order to store it and then when app becomes active method is triggered i check if there is a missing notification stored to show it.

    -(void)remoteNotificationReceived:(NSInteger)code info:(NSInteger)info messageInfo:(NSDictionary*)messageInfo appInBackground:(BOOL)appInBackground{
    
    DLog(@"Notification received appInBackground: %d,pushCode: %ld, messageInfo: %@",appInBackground, (long)code,messageInfo);
    
    switch (code){
        case 0:
            break;
        case 1:
        {
            NSArray *pendingAdNotifiacations=[[NSUserDefaults standardUserDefaults] objectForKey:@"pendingAdNotifiacations"];
            NSMutableDictionary *addDictionary=[[NSMutableDictionary alloc] initWithDictionary:messageInfo copyItems:YES];
            [addDictionary setObject:[NSNumber numberWithInteger:info] forKey:@"ad_id"];
    
            if(!pendingAdNotifiacations){
                pendingAdNotifiacations=[NSArray arrayWithObject:addDictionary];
            }else{
                pendingAdNotifiacations=[pendingAdNotifiacations arrayByAddingObject:addDictionary];
            }
            [addDictionary release];
    
            [[NSUserDefaults standardUserDefaults] setObject:pendingAdNotifiacations forKey:@"pendingAdNotifiacations"];
            [[NSUserDefaults standardUserDefaults] synchronize];
            DLog(@"pendingAdNotifiacations received: %@.",pendingAdNotifiacations);
            [[UIApplication sharedApplication] setApplicationIconBadgeNumber:(pendingAdNotifiacations)?[pendingAdNotifiacations count]:0];
            DLog(@"783- pendingAdNotifiacations: %lu.",(unsigned long)((pendingAdNotifiacations)?[pendingAdNotifiacations count]:0));
            if(appInBackground){
    
                [AdManager requestAndStoreAd:info];
            }else{
                [AdManager requestAndShowAd:info];
            }
        }
            break;
        default:
            break;
    }
    
    }
    

    This is the relevant code to download the info in the background using a background task:

    -(void)requestAdinBackgroundMode:(NSInteger)adId{
    DLog(@"744- requestAdinBackgroundMode begin");
    if(_backgroundTask==UIBackgroundTaskInvalid){
        DLog(@"744- requestAdinBackgroundMode begin dispatcher");
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        DLog(@"744- passed dispatcher");
        [self beginBackgroundUpdateTask];
    
        NSURL *requestURL=[self requestURL:adId];
        if(requestURL){
            NSURLRequest *request = [NSURLRequest requestWithURL:requestURL];
    
            NSURLResponse * response = nil;
            NSError  * error = nil;
            DLog(@"744- NSURLConnection url: %@",requestURL);
            NSData * responseData = [NSURLConnection sendSynchronousRequest: request returningResponse: &response error: &error];
    
            if(NSClassFromString(@"NSJSONSerialization"))
            {
                NSError *error = nil;
                id responseObject = [NSJSONSerialization
                         JSONObjectWithData:responseData
                         options:0
                         error:&error];
    
                if(error) {
                    NSLog(@"JSON reading error: %@.",[error localizedDescription]);
                    /* JSON was malformed, act appropriately here */ }
                else{
    
                if(responseObject && [responseObject isKindOfClass:[NSDictionary class]]){
                    if(responseObject && [[responseObject objectForKey:@"success"] integerValue]==1){
                        NSMutableDictionary *adDictionary=[[[NSMutableDictionary alloc] initWithDictionary:[responseObject objectForKey:@"ad"]] autorelease];
                        DLog(@"744- NSURLConnection everythig ok store: %@",adDictionary);
                        [self storeAd: adDictionary];
                    }
                }
                }
            }
        }
    
        // Do something with the result
    
        [self endBackgroundUpdateTask];
    });
    }
    } 
    
    - (void) beginBackgroundUpdateTask
    {
    DLog(@"744- requestAdinBackgroundMode begin");
    _backgroundTask = [[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:^{
        [self endBackgroundUpdateTask];
    }];
    }
    
    - (void) endBackgroundUpdateTask
    {
    DLog(@"744- End background task");
    [[UIApplication sharedApplication] endBackgroundTask: _backgroundTask];
    _backgroundTask = UIBackgroundTaskInvalid;
    }
    

    Well this is all I think, I post it because someone asked me to post an update, I hope it may help someone...

    0 讨论(0)
  • 2020-11-28 05:30

    Just want to add an updated answer.

    I am facing the same problem.

    -(void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler;
    

    Doesn't get called when the app is killed from background multitasking (double tap home button and swipe up to kill app).

    I have tested this myself using development push notification and NWPusher tool (https://github.com/noodlewerk/NWPusher)

    Outdated documentation

    This previous block of documentation which says:

    Unlike the application:didReceiveRemoteNotification: method, which is called only when your app is running, the system calls this method regardless of the state of your app. If your app is suspended or not running, the system wakes up or launches your app and puts it into the background running state before calling the method. If the user opens your app from the system-displayed alert, the system calls this method again so that you know which notification the user selected.

    Is outdated (at the time of writing this 04/06/2015).

    Updated Documentation (as at of 04/06/2015)

    I checked the documentation (at the time of writing this 04/06/2015), it says:

    Use this method to process incoming remote notifications for your app. Unlike the application:didReceiveRemoteNotification: method, which is called only when your app is running in the foreground, the system calls this method when your app is running in the foreground or background. In addition, if you enabled the remote notifications background mode, the system launches your app (or wakes it from the suspended state) and puts it in the background state when a remote notification arrives. However, the system does not automatically launch your app if the user has force-quit it. In that situation, the user must relaunch your app or restart the device before the system attempts to launch your app automatically again.

    https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIApplicationDelegate_Protocol/index.html#//apple_ref/occ/intfm/UIApplicationDelegate/application:didReceiveRemoteNotification:fetchCompletionHandler:

    If you read carefully, you'll notice it now says:

    the system calls this method when your app is running in the foreground or background.

    NOT:

    regardless of the state of your app

    So it looks like from iOS 8+ we're out of luck :(

    0 讨论(0)
提交回复
热议问题