[IONIC 3/4] How to detect incoming call and get caller number using Ionic-v3/4?

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 :slight_smile:

1 Like

Hi
Do you find the way to make it?

No did’t find anything

change to use that plugin, That’s great and I can get the incoming call number now

1 Like

Does it work in ionic 4?

I did it in ionic 4 working fine

Please give an example here

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?

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:

  1. Install ionic android permission -Android Permissions - Ionic Documentation
  2. install call log plugin ,you need android call log permissions- Call Log - Ionic Documentation
  3. 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?

I think no, it is not working if the app is closed but it’s working in-app background.