Skip to content

Commit

Permalink
fix: corrected double opens on iOS background
Browse files Browse the repository at this point in the history
  • Loading branch information
ethanneff committed Aug 28, 2017
1 parent 9389bed commit 0868dde
Showing 1 changed file with 6 additions and 7 deletions.
13 changes: 6 additions & 7 deletions src/branch.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,24 +35,23 @@ Branch.prototype.disableGlobalListenersWarnings = function () {
disableGlobalListenersWarnings = true
}

var runOnce = true
Branch.prototype.initSession = function (deepLinkDataListener) {
// handle double init from onResume on iOS
if (!runOnce) return
runOnce = (deviceVendor.indexOf('Apple') < 0)

// private method to filter out +clicked_branch_link = false in deep link callback
var previous = 0
var deepLinkDataParser = function (deepLinkData) {
var isBranchLink = '+clicked_branch_link'
var isNonBranchLink = '+non_branch_link'
// TODO: figure out why iOS SDK passes data twice on Ionic 2 terminated and no network connection
var dataLength = JSON.stringify(deepLinkData).length
var isNewData = dataLength !== previous
var isBranchLinkClick = deepLinkData.hasOwnProperty(isBranchLink) && deepLinkData[isBranchLink] === true
var isNonBranchLinkClick = deepLinkData.hasOwnProperty(isNonBranchLink)

// is +clicked_branch_link' = true || +non_branch_link
if (isNewData && (isBranchLinkClick || isNonBranchLinkClick)) {
// to Branch.initSession(function(data) {})
if (isBranchLinkClick || isNonBranchLinkClick) {
deepLinkDataListener(deepLinkData)
}
previous = dataLength
}

if (!disableGlobalListenersWarnings && !deepLinkDataListener && !window.DeepLinkHandler) {
Expand Down

3 comments on commit 0868dde

@mathatan
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems that there's an issue with this commit when returning from background which prevents the branch from being reinitialized and as such deepLinks which refer to an app which is in background cannot be followed within ios.

@ethanneff
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Deep link data will only pass into the app if we can confirm a fingerprint match. This match is between the link click (browser information) and app open (device information). If we cannot confidently determine a match, deep link data will not pass through.

In your tests, is deep link data never passing through whenever the app is backgrounded? Additionally, how are you testing? The steps and device information you use will be helpful for us to debug on our end.

Thanks,

@mathatan
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the original point was mainly that e.g. this code:

// sample index.js
var app = {
  initialize: function() {
    this.bindEvents();
  },
  bindEvents: function() {
    document.addEventListener('deviceready', this.onDeviceReady, false);
    document.addEventListener('resume', this.onDeviceResume, false);
  },
  onDeviceReady: function() {
    app.branchInit();
  },
  onDeviceResume: function() {
    app.branchInit();
  },
  branchInit: function() {
    // Branch initialization
    Branch.initSession(function(data) {
      if (data['+clicked_branch_link']) {
        // read deep link data on click
        alert('Deep Link Data: ' + JSON.stringify(data))
      }
    });
  }
};

app.initialize();

Cannot work, since the resume event can never result with the data being handled unless I've miss understood something from the code. The runOnce variable prevents the initSession from ever running again until the scope is reninitialized (i.e. the app is closed and reopened)

Please sign in to comment.