This chapter explains how to troubleshoot problems with the HarmonyOS SDK.
Feature description
The SDK provides two troubleshooting methods: logs and error codes.
Log output
The SDK provides two types of log outputs: HiLog and interface logs.
HiLog log output
The SDK provides a switch that you can use to output SDK logs to HiLog. The following code shows an example:
import { aliyunPushLog } from '@aliyun/push';
aliyunPushLog.enableHiLogLogger();
Call this method before you initialize the SDK. Use this method only for development and testing.
Interface log output
The SDK provides the `addLogger` and `removeLogger` methods to add or remove log output interfaces. The following code shows an example:
import { aliyunPushLog, Logger } from '@aliyun/push'
import hilog from '@ohos.hilog'
class MyLogger implements Logger {
print(domain: number, tag: string, level: hilog.LogLevel, msg: string): void {
// This example shows how to output logs to HiLog. The actual implementation depends on the log collection system that your application uses.
if (level >= hilog.LogLevel.ERROR) {
switch (level) {
case hilog.LogLevel.FATAL:
hilog.fatal(domain, tag, msg);
break;
case hilog.LogLevel.ERROR:
default:
hilog.error(domain, tag, msg);
break;
}
}
}
}
const logger = new MyLogger();
// Add the log interface.
aliyunPushLog.addLogger(logger);
// Remove the log interface.
aliyunPushLog.removeLogger(logger);
Follow these recommendations when you use the log interface:
In a production environment, collect only logs at the `hilog.LogLevel.ERROR` level or higher by default.
In a production environment, you can dynamically collect logs at the `hilog.LogLevel.INFO` level or higher for specific devices. To do this, deliver the required configurations to the devices.
Log parameter descriptions
The `domain` for SDK logs is `0xF101`.
The main `tag` for SDK logs is `PS`. This tag identifies logs for SDK interface inputs and outputs.
The SDK outputs logs at the `hilog.LogLevel.ERROR` level if an SDK call fails or a push is abnormal.
Error codes
If an SDK interface call fails, the SDK throws a `PushError`. The following code shows an example for the device registration interface:
import { aliyunPush, PushError } from '@aliyun/push';
aliyunPush.register((err: PushError | undefined) => {
if (err) {
console.error(`Failed to register the device. Error code: ${err.code}. Error message: ${err.message}`);
return;
}
console.info(`Device registered successfully. Device ID: ${aliyunPush.getDeviceId()}`);
});A `PushError` has two main fields:
`code`: The error code. It indicates the type and location of the error.
`message`: The specific error details. This includes information about other exceptions that caused the error.
The following table lists all error codes.
Error code | Error description | Possible cause | Troubleshooting steps |
PUSH_001 | Unknown error. | A system error or an undiscovered edge case occurred. |
|
PUSH_002 | First, initialize the SDK by calling the init method. | Another `Ability` was started directly without initialization. |
|
PUSH_003 | The push data cannot be found. | The provided push parameters are incorrect. |
|
PUSH_004 | You must register the device first. | The `aliyunPush.register` method was not called. |
|
PUSH_005 | The data passed to the notification extension is not from Alibaba Cloud Push. | The notification extension message was not pushed from the Alibaba Cloud Mobile Push platform. |
|
PUSH_101_XXX | Feature interface error. | The service response does not match the expected response. |
|
PUSH_102 | Invalid parameter. | The parameter is invalid. |
|
PUSH_103 | Device not registered. | An API that requires device registration was called before the device was registered. |
|
PUSH_201 | Device not registered. | The registration process was interrupted. This caused an inconsistent application state. |
|
PUSH_202_XXX | Device registration failed. | The device has an abnormal status on the service. |
|
PUSH_203_XXX | Error when enabling or disabling push notifications. | The device has an abnormal status on the service. |
|
PUSH_301_XXX | HTTP request failed. | Service interface error. |
|
PUSH_302_XXX | HTTP request failed. | The device cannot connect to the service over the network. |
|
PUSH_303_XXX | HTTP download error. | The system failed to download the notification image. |
|
PUSH_401_XXX | Failed to establish the Alibaba Cloud push channel. | The device is blocklisted. |
|
PUSH_402 | The Alibaba Cloud push channel is not established. | The device is not registered. |
|
PUSH_403_XXX PUSH_404_XXX PUSH_405_XXX | Interface call failed. | The network is unstable. |
|
PUSH_501_XXX | `dataPreferences` interface call failed. | System interface error. |
|
PUSH_502_XXX | `notificationManager` interface call failed. | System interface error. |
|
PUSH_503_XXX | `relationalStore` interface call failed. | System interface error. |
|
PUSH_504_XXX | `image` interface call failed. | System interface error. |
|
PUSH_505_XXX | System interface call failed. | System interface error. |
|