Reputation: 650
I am developing an app using Phonegap Build for a customer that has chosen not to use the Google Play Store. Instead, they are opting to have me privately host the APK files for the app.
I am trying to develop a way to dynamically check my server every time the app is launched to check to see if there is an update and if there is, to download and install that update.
I do an ajax request to my server to check for the latest file, I then return the latest version number and URL of the file on AWS to my frontend. If the file version is greater than the current app version, I want to update.
Here's the code I have right now using the FileTransfer
and Web Intent
plugins for Phonegap Build:
$.ajax
type: 'GET'
dataType: 'json'
url: "#{baseUrl}/v2/update"
data:
app_token: 'fubar'
success: (data) =>
window.downloadApkAndroid(data)
window.downloadApkAndroid = (data) ->
fileURL = "cdvfile://localhost/persistent/#{data.filename}"
fileTransfer = new FileTransfer
uri = encodeURI(data.download_url)
fileTransfer.download uri, fileURL, ((entry) ->
alert 'download complete: ' + entry.fullPath
promptForUpdateAndroid entry
return
), ((error) ->
console.log 'download error source ' + error.source
console.log 'download error target ' + error.target
console.log 'upload error code' + error.code
alert "#{error.code} + #{error.source} + #{error.target}"
return
), false, {}
return
window.promptForUpdateAndroid = (entry) ->
alert entry
window.plugins.webintent.startActivity {
action: window.plugins.webintent.ACTION_VIEW
url: entry.toURL()
type: 'application/vnd.android.package-archive'
}, (->
), ->
alert 'Failed to open URL via Android Intent.'
console.log 'Failed to open URL via Android Intent. URL: ' + entry.fullPath
return
return
Nothing happens when I launch the app, though. I am returning a newer version from the first Ajax request and the success method is being called. But nothing seems to happen after that. Can anyone tell me any better methods of doing this or what I'm doing wrong here?
Upvotes: 1
Views: 1331
Reputation: 4148
Have you considered using the Cordova Hot Code Push plugin, which would allow you to update your application's www folder (the app views, JS, CSS, any images etc) without having to do a Google Play update. This method would allow you to ship a version of your application in the apk, and periodically get updates from a server without having to build boilerplate file fetching code yourself.
Upvotes: 1