NativeScript / push-plugin

Contains the source code for the Push Plugin.
Apache License 2.0
123 stars 48 forks source link

Background notification callback not executed until app is opened on iOS #218

Closed jpakulski closed 5 years ago

jpakulski commented 6 years ago

Hi,

I'm trying to incorporate background (silent) push notifications in our app. We observe the behaviour described here on iOS.

This behaviour is reproducible using the demo app.

I would expect the callback to be executed regardless of the app running in background. Apple states that the app will run for up to 30 sec (source)

When run in debug more using xcode I get a "didReceiveNotification" console message whenever a push notification is received regardless of app state but the callback is not executed until the app is focused.

Which platform(s) does your issue occur on?

Please, provide the following version numbers that your issue occurs with:

Reproduce

  1. clone your repo
  2. cd into demo dir, run: tns prepare ios --release --forDevice --teamId XXXXX
  3. opened the app in xcode
  4. configured the demo app for Push Notifications
  5. run the app on a physical device
  6. Your app runs and provides an access token for me to use

I use the following message to trigger a silent notification (but I tried many combinations of properties):

{
  "aps":{"content-available" : 1},
  "data": "hello sir"
}

I get a "didReceiveNotification" whenever a push notification is received no matter what the app state is. I get Message received! message in your log but only when the app is focused.

Is there something anyone can hint at that can execute code while the app remains in the background?

Cheers

jpakulski commented 6 years ago

Just a small followup:

I had a look at the native code that's included and removing a check for UIApplicationStateActive in the didReceiveRemoteNotification:(NSDictionary *)userInfo method fixes my problems. This is a totally naive fix as I did not check the rest of the code and don't really understand why the check was there in the first place:

push-plugin/native-src/ios/PushPlugin/PushManager.m

- (void)my_application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {
    if (didReceiveOriginalMethod) {
        void (*originalImp)(id, SEL, UIApplication *, NSDictionary *) = didReceiveOriginalMethod;
        originalImp(self, @selector(application:didReceiveRemoteNotification:), application, userInfo);
    }
    NSLog(@"didReceiveNotification");

    UIApplicationState appState = UIApplicationStateActive;
    if ([application respondsToSelector:@selector(applicationState)]) {
        appState = application.applicationState;
    }

    [Push sharedInstance].notificationMessage = userInfo;
    [Push sharedInstance].isInline = YES;
    [[Push sharedInstance] notificationReceived];
}

Update:

In the above code change

[Push sharedInstance].isInline = YES;

To

[Push sharedInstance].isInline = appState == UIApplicationStateActive;

This sets the "foreground" value inside the received message to a correct value.

zbranzov commented 5 years ago

A fix is released in nativescript-push-notifications@1.1.5