I am using ionic-plugin-deeplinks for deeplinking, deeplink works properly when app is killed and started again . But it fails to route when the app is inactive(switched to some other app). My code structure is same as example shared on the plugin.
$cordovaDeeplinks.route({
’#/home/xyz’: {
target: ‘xyz’,
parent: ‘home’
}
}).subscribe(
function(match) {
}, function (nomatch) { //Since my url doesn’t match, i am handling the navigation on nomatch.
var loc = nomatch.$link.fragment;
$rootScope.$apply(function() {
$location.path(loc);
});
}
);
** i am using $location.path(loc) for the navigation . As i have few dynamic params on the url .
Observation:
- When an app starts ondevice ready event fires, then user kills the app.
if user clicks on a apps link , the plugin opens the app properly and goes to the respective page.(Here onDevice ready will trigger and the deeplink observable serves the incoming deeplink). - Afte step 1 When the app is switched , on pause triggers , now user clicks on an app link from gmail , here plugin opens the app , and the “onResume” event is called .
Question: Will the same ondeviceready .subscribe() will serve the same scenario ?? In my case it doesn’t happen . And it never works on all subsequent pause/resume(flip app and comeback to app again).
To handle this I have added the same code snippet on the onResume block :
Now the behaviour is as below:
- an app starts, ondevice ready event fires, then user kills the app.
- user clicked a link from Gmail and app opened.
- Now switch the app and click the link again - failed----(this is the only case where it fails)
- Repeat the step-3 -passed - Worked fine
- Repeat the step-3 - passed - Worked fine
Debugging further , following is finding :
On device ready : The observer on my app , is called before the deeplink object is created on plugin
On Resume : The observer on my app is called after the deeplink object is created on plugin .
To fix this , now i have changed my ondevice ready code as below and removed the deeplink code from onresume. So now i don’t see any issue .
Is it correct way of solving the discussed case ??
//outside onready
function subscribe_deeplink(){
$cordovaDeeplinks.route({
'#/home/xyz': {
target: 'xyz',
parent: 'home'
}
}).subscribe(
function(match) {
}, function (nomatch) {
$rootScope.isNavigated = true;
var loc = nomatch.$link.fragment;
subscribe_deeplink();
$rootScope.$apply(function() {
$location.path(loc);
});
}
);
}
//inside onready
$cordovaDeeplinks.route({
'#/home/xyz': {
target: 'xyz',
parent: 'home'
}
}).subscribe(
function(match) {
}, function (nomatch) {
$rootScope.isNavigated = true;
var loc = nomatch.$link.fragment;
subscribe_deeplink();
$rootScope.$apply(function() {
$location.path(loc);
});
}
);