DEV Community

HarmonyOS
HarmonyOS

Posted on

Resolving Country Code Matching Issues for Corporate Contact Phone Numbers

Read the original article:Resolving Country Code Matching Issues for Corporate Contact Phone Numbers

Context

The onQueryCallerInfo callback for corporate caller information provides phone numbers with inconsistent country codes. This can be resolved by using the @ohos.i18n library to standardize and format phone numbers to the international E.164 standard.

Description

How to fix the inconsistent country code rules for the phoneNumber parameter in the onQueryCallerInfo callback for corporate contacts?

The phoneNumber parameter in the onQueryCallerInfo callback contains the country code, area code, mobile number, and landline number.

  • Mainland China mobile numbers return an 11-digit mobile number without "86" or "00086", e.g., 188xxxxxxx.
  • Hong Kong mobile numbers return a 5-digit country code "00852" followed by an 8-digit mobile number, e.g., 008521xxxxxx8.
  • Mainland China landline numbers return the area code followed by an 8-digit landline number, e.g., Beijing 0108xxxxxx4.

E.164 is an international, unambiguous telephone number format standard. Its core function is to unify numbers into a string of up to 15 digits using a plus sign and country code, ensuring compatibility and correctness across global communication systems. Formatting to E.164 is a crucial step when handling cross-border phone numbers.

Solution

The number passed to the corporate application is the original number provided by the network side, and HarmonyOS does not process it. Generally, international calls will automatically include a country code from the network side. When the application overrides the onQueryCallerInfo method, it can use @ohos.i18nto format the phone number. The system region can be obtained using getSystemRegion.

Example code:

import { CallerInfoQueryExtensionAbility, CallerInfo } from '@kit.CallServiceKit';
import { i18n } from '@kit.LocalizationKit';
export default class EntryCallerInfoQueryExtAbility extends CallerInfoQueryExtensionAbility {
  onQueryCallerInfo(phoneNumber: string): Promise<CallerInfo> {
    return new Promise<CallerInfo>((resolve, reject) => {
      let phoneNumberFormat = new i18n.PhoneNumberFormat(i18n.System.getSystemRegion(), {type: 'E164'});
    });
  }
}
Enter fullscreen mode Exit fullscreen mode

Key Takeaways

  • Phone numbers from the network side may be inconsistent and not follow a standard format.
  • To ensure compatibility and a consistent format for phone numbers, use the @ohos.i18n library to format them to the E.164 standard.
  • Use the getSystemRegion API to automatically retrieve the device's current country or region for accurate formatting.

Written by Taskhyn Maksim

Top comments (0)