cordova-plugin-firebase icon indicating copy to clipboard operation
cordova-plugin-firebase copied to clipboard

iOS didReceiveRemoteNotification not called when app in background or inactive

Open LazzeZee opened this issue 5 years ago • 3 comments

Hi,

I have some callback code running when I receive a push notification. It works perfectly fine when app is in foreground but is not called when app is running in background or being inactive. The push notification is being displayed perfectly but the code is not run in the code.

Is this a bug or is this the expected behavior?

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

NSDictionary *mutableUserInfo = [userInfo mutableCopy];

[mutableUserInfo setValue:self.applicationInBackground forKey:@"tap"];
// Print full message.
NSLog(@"%@", mutableUserInfo);

if (application.applicationState != UIApplicationStateInactive && [mutableUserInfo objectForKey:@"notificationid"] != nil)
{
    os_log(OS_LOG_DEFAULT, "Callback to server.");

    NSMutableURLRequest *urlRequest = [[NSMutableURLRequest alloc] initWithURL: [NSURL URLWithString:@"URL-GOES-HERE"]];
    
    NSString *notificationid = [mutableUserInfo objectForKey:@"notificationid"];
    NSString *uniquekey = [mutableUserInfo objectForKey:@"uniquekey"];
    NSString *body = [NSString stringWithFormat:@"notificationid=%@&uniquekey=%@", notificationid, uniquekey];
    
    [urlRequest setHTTPMethod:@"POST"];
    [urlRequest setHTTPBody: [body dataUsingEncoding:NSUTF8StringEncoding]];
    [urlRequest setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
    
    NSURLSession *session = [NSURLSession sharedSession];
    NSURLSessionDataTask *dataTask = [session dataTaskWithRequest:urlRequest
            completionHandler:^(NSData *data, NSURLResponse *response, NSError *error)
            {
                os_log(OS_LOG_DEFAULT, "Callback done.");
                if (application.applicationState != UIApplicationStateBackground)
                {
                    [FirebasePlugin.firebasePlugin sendNotification:mutableUserInfo];
                }
                completionHandler(UIBackgroundFetchResultNewData);
            }];
    [dataTask resume];
} 
else {
    if (application.applicationState != UIApplicationStateBackground)
    {
        [FirebasePlugin.firebasePlugin sendNotification:mutableUserInfo];
    }
    completionHandler(UIBackgroundFetchResultNewData);
}

}`

LazzeZee avatar Aug 22 '19 12:08 LazzeZee

Does this work for anyone?

LazzeZee avatar Aug 27 '19 06:08 LazzeZee

There's been some changes to how background push notifications getting delivered in iOS13 - see my blog post! http://www.themoyles.co.uk/2019/11/cordova-firebase-background-push-notifications-not-working-in-ios13-and-xcode-11/

andymoyle avatar Nov 25 '19 20:11 andymoyle