gokujy
April 10, 2019, 12:07pm
1
Hello, I want display caller number in an incoming call using ionic 4, here my code.
home.ts
import { Component } from '@angular/core';
import { AndroidPermissions } from '@ionic-native/android-permissions/ngx';
declare var PhoneCallTrap: any;
//phone calls
declare var window: any;
@Component({
selector: 'app-home',
templateUrl: 'home.page.html',
styleUrls: ['home.page.scss'],
})
export class HomePage {
constructor(private androidPermissions: AndroidPermissions) {
this.checkPermissionCall();
}
//phone calls
checkPermissionCall() {
this.androidPermissions.checkPermission(this.androidPermissions.PERMISSION.READ_PHONE_STATE).then(
success => {
//if permission granted
this.phonecalls();
},
err => {
this.androidPermissions.requestPermission(this.androidPermissions.PERMISSION.READ_PHONE_STATE).
then(success => {
this.phonecalls();
},
err => {
console.log("cancelled")
});
});
this.androidPermissions.requestPermissions([this.androidPermissions.PERMISSION.READ_PHONE_STATE]);
}
//phone calls
phonecalls() {
if (window.PhoneCallTrap) {
window.PhoneCallTrap.onCall(function (state) {
alert("CHANGE STATE: " + state);
//var callObj = JSON.parse(state),
///state = callObj.state,
this.callingNumber = state.incomingNumber;
alert("callingNumber STATE: " + this.callingNumber);
switch (state) {
case "RINGING":
console.log("Phone is ringing");
break;
case "OFFHOOK":
console.log("Phone is off-hook");
break;
case "IDLE":
console.log("Phone is idle");
break;
}
});
}
}
}
I did this, with this I am getting a phone state like a phone is ringing when someone calls but how can I get number?
please help
1 Like
Hi
Do you find the way to make it?
change to use that plugin, Thatβs great and I can get the incoming call number now
1 Like
gokujy
November 4, 2019, 5:26am
6
I did it in ionic 4 working fine
Please give an example here
gokujy
November 4, 2019, 5:49am
8
Hello,
Plugin: cozzbie.plugin.phonecalltrap
//phone calls
phonecalls() {
if (window.PhoneCallTrap) {
window.PhoneCallTrap.onCall((obj) => {
var callObj = JSON.parse(obj),
state = callObj.state,
callingNumber = callObj.incomingNumber;
console.log("obj: " + obj);
//working
console.log("callingNumber STATE: " + callingNumber);
switch (state) {
case "RINGING":
console.log("Phone is ringing");
break;
case "OFFHOOK":
console.log("Phone is off-hook");
break;
case "IDLE":
console.log("Phone is idle");
break;
}
});
}
}
}
With this, you can get a mobile number with a phone state ex. rining⦠Also, you can search the number in your contact book to get name of the number.
1 Like
Hi.
In order to get the phone number you need to get the contact read permission.
You may call the phonecalls() function after getting the contact read permission as below
checkPermissionREAD_CONTACTS() {
this.androidPermissions.checkPermission(this.androidPermissions.PERMISSION.READ_CONTACTS).then(
success => {
this.mystatus = βpermission read contactsβ
setTimeout(() => {
this.phonecalls();
}, 100)
//if permission granted
},
err => {
this.androidPermissions.requestPermission(this.androidPermissions.PERMISSION.READ_CONTACTS).
then(success => {
this.mystatus = βpermission read contacts grantedβ
setTimeout(() => {
this.phonecalls();
}, 100)
},
err => {
this.mystatus = "permission read contacts canceled " + err
console.log(βcancelledβ)
});
});
this.androidPermissions.requestPermissions([this.androidPermissions.PERMISSION.READ_CONTACTS]);
}
Does this work in background? Even when the app is not open?
gokujy
April 17, 2020, 7:59pm
11
it is not working when app is close.
Hey! did you solve whether app works in background mode and detects incoming call??
I have tried it but canβt get mobile number only get state-ringing. show null in mobile number.
FIXED ISSUE - Follow Below steps:
Install ionic android permission -Android Permissions - Ionic Documentation
install call log plugin ,you need android call log permissions- Call Log - Ionic Documentation
install Cordova PhoneCall Trap plugin -cozzbie.plugin.phonecalltrap - npm
code:
import {ββββββββ Component, OnInit }ββββββββ fromβ@angular /coreβ;
import {ββββββββ AndroidPermissions }ββββββββ fromβ@ionic-native /android-permissions/ngxβ;
import {ββββββββ CallLog }ββββββββ fromβ@ionic-native /call-log/ngxβ;
import {ββββββββ Platform }ββββββββ fromβ@ionic /angularβ;
declarevarPhoneCallTrap: any;
@Component ({ββββββββ
selector:βapp-call-trapβ,
templateUrl:β./call-trap.component.htmlβ,
styleUrls: [β./call-trap.component.scssβ],
}ββββββββ)
exportclassCallTrapComponentimplementsOnInit {ββββββββ
constructor(privateandroidPermissions: AndroidPermissions,
privatecallLog: CallLog,
privateplatform: Platform) {ββββββββ
this.initializeApp();
}ββββββββ
ngOnInit() {ββββββββ}ββββββββ
initializeApp() {ββββββββ
this.platform.ready().then(() => {ββββββββ
this.androidPermissions.checkPermission(this.androidPermissions.PERMISSION.READ_CALL_LOG).then((result) => {ββββββββ
if (!result.hasPermission) {ββββββββ
alert(result.hasPermission);
this.androidPermissions.requestPermission(this.androidPermissions.PERMISSION.READ_CALL_LOG)
this.phonecalls();
}ββββββββ
}ββββββββ, (err) => {ββββββββ
this.androidPermissions.requestPermission(this.androidPermissions.PERMISSION.READ_CALL_LOG)
this.phonecalls();
}ββββββββ)
}ββββββββ)
}ββββββββ
phonecalls() {ββββββββ
PhoneCallTrap.onCall(function (obj) {ββββββββ
alert(JSON.stringify(obj));
varcallObj = JSON.parse(obj),
state = callObj.state,
callingNumber = callObj.incomingNumber;
switch (state) {ββββββββ
case"RINGING":
alert(βPhone is ringingβ + callingNumber);
break;
case"OFFHOOK":
console.log(βPhone is off-hookβ);
break;
case"IDLE":
console.log(βPhone is idleβ);
break;
}ββββββββ
}ββββββββ)
}ββββββββ
}ββββββββ
Have a good day!
this works even if the app is closed?
gokujy
March 17, 2021, 1:16pm
17
I think no, it is not working if the app is closed but itβs working in-app background.