AppsFlyerSDK / appsflyer-cordova-plugin

AppsFlyer plugin for Cordova
MIT License
37 stars 71 forks source link

Universal Links action not triggered when app is in background or killed #152

Closed vlceklukas closed 3 years ago

vlceklukas commented 3 years ago

Report

Plugin Version

6.2.30

On what Platform are you having the issue?

iOS

What did you do?

Upon upgrade to the latest version, we are not able to log Universal Link action when app is in background or killed. We are able to detect the URL when we place such code to the AppDelegate didFinishLaunchingWithOptions. We are using a OneLink template. Everything works perfectly when the app is opened.

NSDictionary *activityDictionary = launchOptions[UIApplicationLaunchOptionsUserActivityDictionaryKey];
if (activityDictionary) {
    NSUserActivity *activity = [activityDictionary objectForKey:@"UIApplicationLaunchOptionsUserActivityKey"];
    NSString *urlString = activity.webpageURL.absoluteString;
    NSURL *url = [NSURL URLWithString:urlString];
}

However it does not seem the continueUserActivity or openURL is called.

What did you expect to happen?

Universal link action should have been logged and triggered.

What happened instead?

App opens on the main screen with no action triggered.

Copy of our AppDelegate.m file

#import "AppDelegate.h"
#import "MainViewController.h"
#import "AppsFlyerPlugin.h"

@implementation AppDelegate

- (BOOL)application:(UIApplication*)application didFinishLaunchingWithOptions:(NSDictionary*)launchOptions
{
    NSDictionary *activityDictionary = launchOptions[UIApplicationLaunchOptionsUserActivityDictionaryKey];
    if (activityDictionary) {

        // Here we can detect the Universal Link URL
        NSUserActivity *activity = [activityDictionary objectForKey:@"UIApplicationLaunchOptionsUserActivityKey"];
        NSString *urlString = activity.webpageURL.absoluteString;
        NSURL *url = [NSURL URLWithString:urlString];
    }

    self.viewController = [[MainViewController alloc] init];
    return [super application:application didFinishLaunchingWithOptions:launchOptions];
}

// Deep linking
// Reports app open from deep link for iOS 10
- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url options:(NSDictionary *) options {
   [[AppsFlyerLib shared] handleOpenUrl:url options:options];
   return YES;
 }
// Open Universal Links
- (BOOL) application:(UIApplication *)application continueUserActivity:(NSUserActivity *)userActivity restorationHandler:(void (^)(NSArray<id> *restorableObjects))restorationHandler {
    [[AppsFlyerLib shared] continueUserActivity:userActivity restorationHandler: nil];
    return YES;
}

@end
pazlavi commented 3 years ago

hey @vlceklukas, Please follow this guide since 6.2.30 you should replace [AppsFlyerLib shared] with [AppsFlyerAttribution shared]

vlceklukas commented 3 years ago

hey @pazlavi just tested the updated code, however the result is still the same. When the app is opened it works like a charm. If I kill the app and reopen with the same link, it still does nothing apart from opening the app.

Thank you very much for any help!

vlceklukas commented 3 years ago

adding how onAppOpenAttribution is handled in the code. It is executed when platform is ready.

window.plugins.appsFlyer.registerOnAppOpenAttribution((res) => {

      let deeplinkData = JSON.parse(res);
      if (deeplinkData.type === 'onAppOpenAttribution') {
        let deepLinkValue = deeplinkData.data.deep_link_value;

        if (deepLinkValue === 'dl_start_trial') {
          this.shakeButton();
        }

      } else {
        console.log('af deeplink :: onAppOpenAttribution error');
      }
    },
    function onAppOpenAttributionError(err) {
      console.log('af deeplink :: error :: ', err);
    }
);

window.plugins.appsFlyer.initSdk({
      devKey: '***', // your AppsFlyer devKey
      isDebug: false,
      appId: '***', // your ios appID,
      onInstallConversionDataListener: true
    },
    (result) => {
     console.log(result);
    },
    (error) => {
     console.error(error);
    }
);

EDIT: upon further debugging I can confirm I see AppsFlyer Debug: handle deep link in the syslog of my test device. However no-other log message appears.

I've tested using the onAppOpenAttributionListener and onDeepLinkListener with the root handleOpenUrl function.

var handleOpenURL = function(url) {
    console.log('AppsFlyer handleOpenUrl :: ', url);
    window.plugins.appsFlyer.handleOpenUrl(url);
}

EDIT 2: Confirming this behavior on iOS 13.5, 14.4 and 14.5 beta.

amit-kremer93 commented 3 years ago

Hi @vlceklukas do you use cordova with ionic or just cordova? please try to remove this from your code

var handleOpenURL = function(url) {
    console.log('AppsFlyer handleOpenUrl :: ', url);
    window.plugins.appsFlyer.handleOpenUrl(url);
}

it will be more helpful if you will contact our support support@appsflyer.com

vlceklukas commented 3 years ago

Hi @amit-kremer93 thank you for your comments.

After some more testing I found out that continueUserActivity is not called for some reason when the app is opened through universal link.

What did the trick for me was handling it in didFinishLaunchingWithOptions as follows. Now it triggers everything correctly when the app is opened or previously terminated.

- (BOOL)application:(UIApplication*)application didFinishLaunchingWithOptions:(NSDictionary*)launchOptions
{
    self.viewController = [[MainViewController alloc] init];

    NSDictionary *activityDictionary = launchOptions[UIApplicationLaunchOptionsUserActivityDictionaryKey];
    if (activityDictionary) {
        NSUserActivity *activity = [activityDictionary objectForKey:@"UIApplicationLaunchOptionsUserActivityKey"];
        NSURL *url = activity.webpageURL;
        [[AppsFlyerAttribution shared] handleOpenUrl:url options:launchOptions];
    }

    return [super application:application didFinishLaunchingWithOptions:launchOptions];
}

I will keep digging into it and try to find the root-cause.