arnesson / cordova-plugin-firebase

Cordova plugin for Google Firebase
http://arnesson.github.io/cordova-plugin-firebase
MIT License
1.01k stars 1.55k forks source link

iOS didReceiveRemoteNotification not called when app in background or inactive #1102

Open LazzeZee opened 4 years ago

LazzeZee commented 4 years ago

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 commented 4 years ago

Does this work for anyone?

andymoyle commented 4 years ago

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/

DeepikaAon commented 2 weeks ago
    It is not being called [Export("application:didReceiveRemoteNotification:fetchCompletionHandler:")]