Currently tackling an issue with the Cordova file plugin (version 3.4.1) on Android. Despite combing through documentation, including the HTML5 file storage API and numerous StackOverflow posts (most of which suggest installing the plugin first - already done), I'm unable to find a solution for my specific problem.
Developing an app for Android, with plans for iOS integration later on, that successfully receives push notifications. However, I need to incorporate a feature allowing users to disable these notifications. Since there is no SharedPreferences plugin available yet, I've opted to write user preferences to a file on the device instead of querying the server constantly.
Following this tutorial () along with Cordova's guidelines, here is the code snippet used to interact with the file system:
var FileIO = {
initializeFileSystem: function() {
window.requestFileSystem(LocalFileSystem.TEMPORARY, 0, FileIO.gotFS, FileIO.errorHandler);
},
gotFS : function(fileSystem) {
console.log("FILE SYSTEM IS ", fileSystem.name);
gFileSystem = fileSystem;
FileIO.getFile({create : true}, FileIO.writeFile);
},
getFile : function(params, success) {
gFileSystem.root.getFile('notification_preferences.txt', params, success, FileIO.errorHandler);
},
writeFile : function(fileEntry) {
console.log(fileEntry);
console.log('FILE URL: ' + fileEntry.toUrl());
fileEntry.createWriter(function(fileWriter) {
var blob = new Blob(['Lorem Ipsum'], {type: 'text/plain'});
fileWriter.write(blob);
}, errorCallback);
},
readFile : function (fileEntry) {
fileEntry.file(function (file) {
var reader = new FileReader();
reader.onloadend = function (e) {
console.log('CONTENTS OF FILE: ', e.target.result)
};
reader.readAsText(file, 'text/plain');
}, errorCallback);
},
removeFile : function(fileEntry){
fileEntry.remove();
},
// simple error handler
errorHandler : function(e) {
var msg = '';
switch (e.code) {
case FileError.QUOTA_EXCEEDED_ERR:
msg = 'QUOTA_EXCEEDED_ERR';
break;
case FileError.NOT_FOUND_ERR:
msg = 'NOT_FOUND_ERR';
break;
case FileError.SECURITY_ERR:
msg = 'SECURITY_ERR';
break;
case FileError.INVALID_MODIFICATION_ERR:
msg = 'INVALID_MODIFICATION_ERR';
break;
case FileError.INVALID_STATE_ERR:
msg = 'INVALID_STATE_ERR';
break;
default:
msg = e.code;
break;
}
console.log('Error: ' + msg);
}
};
Encountering an error when attempting to write to the file, shown below:
D/CordovaLog( 2871): file:///android_asset/www/cordova.js: Line 1034 : processMessage failed:
Message: S01 File30696438 {"fullPath":"\/\/notification_preferences.txt","filesystemName":"temporary",
"isDirectory":false,"nativeURL":"file:\/\/\/data\/data\/org.changefactor.pgdemo\/cache\/notification_preferences.txt",
"filesystem":0,"isFile":true,"name":"notification_preferences.txt"}
Frustrated by this hurdle, unsure of what's going wrong or if there's a simpler way to store this data. Could the configuration be causing issues?