How to get incoming call number in ionic4?

Viewed 217

I used this plugin: https://www.npmjs.com/package/cozzbie.plugin.phonecalltrap

By using this plugin I got only Phone State (ringing, idle, OFFHOOK). I want incoming call mobile number. Also i added Android Permission for READ_PHONE_STATE and READ_CONTACTS still i cant able to get mobile number. I dont known whether I need to add anything in AndroidManifest.xml file.

Here I Share My codes.

import { Component } from '@angular/core';
import { Platform } from '@ionic/angular';
import { SplashScreen } from '@ionic-native/splash-screen/ngx';
import { StatusBar } from '@ionic-native/status-bar/ngx';
import { AndroidPermissions } from '@ionic-native/android-permissions/ngx';

declare var PhoneCallTrap: any;
declare var window: any;

@Component({
  selector: 'app-root',
  templateUrl: 'app.component.html',
  styleUrls: ['app.component.scss']
})

export class AppComponent {
  constructor(
    private platform: Platform,private androidPermissions: AndroidPermissions,
    private splashScreen: SplashScreen,
    private statusBar: StatusBar
  ) {
    this.initializeApp();
  }

  initializeApp() {
    this.platform.ready().then(() => {
      this.statusBar.styleDefault();
      this.splashScreen.hide();
      this.checkPermissionCall();
      this.checkPermissionREAD_CONTACTS();
    });
  }
  
   checkPermissionCall() {
    this.androidPermissions.checkPermission(this.androidPermissions.PERMISSION.READ_PHONE_STATE).then(
      success => {
        //if permission granted       
        console.log(success);      
      },
      err => {
        this.androidPermissions.requestPermission(this.androidPermissions.PERMISSION.READ_PHONE_STATE).
          then(success => {          
            console.log(success);         
          },
            err => {
              console.log("cancelled")
            });
      });
    this.androidPermissions.requestPermissions([this.androidPermissions.PERMISSION.READ_PHONE_STATE]);    
  }

  checkPermissionREAD_CONTACTS() {
    this.androidPermissions.checkPermission(this.androidPermissions.PERMISSION.READ_CONTACTS).then(
    success => {        
      console.log(success);
    setTimeout(() => {
      this.phonecalls();
    }, 5000)
    //if permission granted
    },
    err => {
    this.androidPermissions.requestPermission(this.androidPermissions.PERMISSION.READ_CONTACTS).
    then(success => {   
    console.log(success);
    setTimeout(() => {
    this.phonecalls();
    }, 5000)
     },
    err => {  
    });
    });
    this.androidPermissions.requestPermissions([this.androidPermissions.PERMISSION.READ_CONTACTS]);    
    }
  
  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;
        }
      });
  }
}
}
0 Answers
Related