how to download file from url in Ionic 2 or Ionic 3? - ionic2

I am trying this code but not working:
this.fileTransfer.download(this.urls, this.file.dataDirectory +'001.pdf',true).then((entry) => {
console.log('download complete: ' + entry.toURL());
}, (error) => {
});
Any help on above.

Related

(React Native and Django) TypeError: Network request failed

I'm developing a mobile app with the concept of using react-native as front end and Django as back-end. I'm actually following this series of tutorials: Javascript Fetch - Communicating between a ReactNative App and Django REST API but when I got to the part where I have to use a fetch method I am getting a TypeError: Network request failed.
Here is a snippet of my code:
const [ domain, setDomain ] = useState("http://10.0.2.2:8000")
function initAppSettings(){
console.log(domain)
fetch(`$(domain)/api/v1.0/app/settings`, {
method: 'GET',
})
.then (result => {
if (result.ok) {
return result.json()
} else {
throw result.json()
}
})
.then (JSON => {
console.log(JSON)
})
.catch (error => {
console.log(error)
})
}
useEffect(() => {
initAppSettings()
}, [])
I have already tried editing the AndroidManifest.xml by adding
android:usesCleartextTraffic="true". I also tried using my own ipv4 address in my Django runserver but still failed to access it.
I have the same question as this but the answer here do not solve my issue.

Push Notification in Ionic2

I am new to Ionic development. I have been using Ionic2 for my application and trying to add push notification (FCM) for the last couple of hours. However, I am stuck in adding push configuration. Here are the details:
"phonegap-plugin-push": "^2.0.0" is configured in my package.json file. Also native push plugin is "#ionic-native/push": "^4.3.1"
In config.xml, I have added <resource-file src="google-services.json" target="google-services.json" />. Also added <plugin name="phonegap-plugin-push" spec="^2.0.0" />.
3.Here is the code I am using in my main component file:
const options: PushOptions = {
android: {
topics: ['topic1']
},
ios: {
alert: 'true',
badge: true,
sound: 'false'
},
windows: {},
browser: {
pushServiceURL: 'http://push.api.phonegap.com/v1/push'
}
};
const pushObject: PushObject = this.push.init(options);
pushObject.on('notification').subscribe((notification: any) => {
if (notification.additionalData.foreground) {
let pushAlert = this.alertCtrl.create({
title: 'BrainCal Notification',
message: notification.message.title
});
pushAlert.present();
}
});
pushObject.on('registration').subscribe((registration: any) => {
//do whatever you want with the registration ID
});
pushObject.on('error').subscribe(error => alert('Error with Push plugin' + error));
It is deployed successfully in my Android phone. However, it return following error while opening the app first time:
"Error with Push plugin no senderID value given".
As per my understanding latest phone gap plugin does not require senderID as it uses Google-services.json file. Please advise.
remove your plugin ionic cordova plugin remove phonegap-plugin-push then install it with parameters like below
ionic cordova plugin add phonegap-plugin-push --variable SENDER_ID=HERE_YOUR_SENDER_ID

Accessing result of map/reduce within Ionic2 app using PouchDB

I am using PouchDB on an Ionic 2 app that syncs with my CouchDB that is on AWS.
I have created a design document in futon and this works well if I go to http://my-ip-address:5984/wardround_jobs/_design/teams/_view/teams - I can see the JSON that is output by the map/reduce used in my design.
I am now trying to figure out how to access this in my app.
I start with this - it gives me all documents:
return new Promise(resolve => {
this.db.allDocs({
include_docs: true
}).then((result) => {
console.log(result);
}).catch((error) => {
console.log(error);
});
});
I can then amend this to get back one specific document:
return new Promise(resolve => {
this.db.get('0448071807c0f37f53e06aab54034a42').then((result) => {
console.log(result);
}).catch((error) => {
console.log(error);
});
});
But if I try and get back my design view using this code then I get back an error:
return new Promise(resolve => {
this.db.get('_design/teams/_view/teams').then((result) => {
console.log(result);
}).catch((error) => {
console.log(error);
});
});
The error is:
CustomPouchError {
status: 404,
name: "not_found",
message: "missing",
error: true,
reason: "missing"
}
How can I access the result from my map/reduce within my app?
EDIT - with the help of Alexis I got this working ...
return new Promise(resolve => {
this.db.get('teams/teams').then((result) => {
console.log(result);
}).catch((error) => {
console.log(error);
});
});
To get the result from any map/reduce design document, you need to use the query() function.

Ionic 2 Google Plus Authetication

google login authentication in ionic 2
with step by step.
i try to print simple response but it does not work
GLogin(){
alert("you are in google plush ");
GooglePlus.login((res)=>{
alert(this.data=res);
});
}
Add the plugin (check this link):
cordova plugin add cordova-plugin-googleplus --save --variable REVERSED_CLIENT_ID=myreversedclientid
Include the lib:
import { GooglePlus } from 'ionic-native';
Now the login:
googlePlus_login() {
GooglePlus.login(
{
'scopes': '',
'webClientId': '',
'offline': false
}
).then(
(success) => {
alert( '\n id: ' + JSON.stringify(success.userId) +
'\n name: ' + JSON.stringify(success.displayName) +
'\n email: ' + JSON.stringify(success.email)
);
},
(failure) => {
console.log('GOOGLE+ login FAILED', failure);
}
);
}
where:
scope: optional, space-separated list of scopes, If not included or empty, defaults to profile and email.
webClientId: optional clientId of your Web application from Credentials settings of your project - On Android, this MUST be included to get an idToken. On iOS, it is not required.
offline: optional, but requires the webClientId - if set to true the plugin will also return a serverAuthCode, which can be used to grant offline access to a non-Google server.
Logout function:
googlePlus_logout() {
GooglePlus.logout().then(
(success) => {
console.log('GOOGLE+: logout DONE', success);
},
(failure) => {
console.log('GOOGLE+: logout FAILED', failure);
}
);
}
Hope it will help you. :)

State not changing when unit testing VueJS and VueResource

I'm trying to test a service that I've created that makes an API call with vue-resource. The service should make the call and update the component with the new data, however in my tests it doesn't register the updated value. I'm using the same setup as the vue-cli webpack example and have based my auth service off this repo (which unfortunately doesn't have any tests)
my service:
export default {
login(context, creds){
context.$http.post(LOGIN_URL, creds)
.then((response) => {
//do something else here
}, (response) => {
context.error = response.data.error
}
}
}
my test:
import Vue from 'vue'
import VueResource from 'vue-resource'
Vue.use(VueResource)
import Auth from 'src/auth'
describe('Auth', () => {
it('should throw an error on unsuccessful login', (done) => {
//intercept the api call and force an invalid response
Vue.http.interceptors.unshift((request, next) => {
next(request.respondWidth({error: 'some error'}, {status: 401}));
});
const vm = new Vue({
data: {
error: ''
}
}).$mount()
Auth.login(vm, {email: 'test#test.com', pass: 'test'} )
//this always fails
expect(vm.error).to.equal('some error')
//ive also tried:
vm.$nextTick(() => {
expect(vm.error).to.equal('some error')
//done()
});
//undo our interceptor
Vue.http.interceptors.shift()
}
}
When I run the test it fails because it's expecting '' to equal 'some error'.
My suspicions are around the fact that vue-resource is using promises.
After reading through some of the Vue.js tests I found my answer. Instead of using vm.$nextTick I did the following:
setTimeout(function(){
expect(vm.error).to.equal('something')
done()
}, 0)