[Huawei Developers] Mike-mei:
If you want to send messages to your app using Push Kit, you'll need to obtain a push token. A push token is unique for each app on a device, and can be used to send messages to the app.

Obtaining a Push Token​

There are two ways to obtain a push token. One is to call the getToken method to request a push token from the Push server. You need to implement the onNewToken method, which will return a push token when the getToken method returns null. The other is to obtain a push token through the onNewToken method during automatic initialization. Click here to learn about the two ways for obtaining a push token.

Reasons for Failing to Obtain a Push Token​

There are two reasons why you are unable to obtain a push token. One is that the API for obtaining a push token fails to be executed and that the error details and code are recorded in logs. The other reason is that the API for obtaining a push token is successfully executed and no error is reported, but the getToken method returns null and the onNewToken method is not called.

Troubleshooting by Reason​

If the error details and code are recorded in logs, refer to common error codes to troubleshoot the error based on the solution corresponding to the recorded error code. Most errors can be solved in this way.

For example, a push token may fail to be obtained if the app ID used to apply for the token does not match that of the current app. Therefore, you need to ensure that the used agconnect-services.json file is the latest one corresponding to your app. You can download the latest file from AppGallery Connect and search for all app IDs in the file to verify if they are correct. If you develop your app together with other developers, you may not be able to search for all app IDs used in the file because each developer may use different variable names in the file. In this case, you can check whether the used app ID is correct based on logs recorded on your device.

To capture logs on the device, perform the following:

1. Connect the ADB tool to your device and run the following commands:

adb shell setprop log.tag.hwpush VERBOSE

adb logcat -v threadtime 1> D:\hwpush.log

2. Try to reproduce the error on the device.

3. Press Ctrl+C to capture logs.

Key logs:

View attachment 5808241

Search for api_name:push.gettoken in the logs and find the log shown in the figure above. In the log, error_code indicates the error code returned when a push token fails to be obtained, app_id indicates the app ID used to apply for the push token, and pkg_name indicates the package name. Check whether the value of app_id is your app ID. If not, you can search for this value in your app project and replace it with your app ID.

If the API for obtaining a push token is successfully executed but no error is reported and the onNewToken method is not called, check the custom class that extends HmsMessageService in your app code and verify that you only override the methods for obtaining a data message and push token as described in Configuring the AndroidManifest.xml File. You do not need to override other methods because those in the class may not be called after overriding. If the error persists, check how many classes that extend HmsMessageService are defined in your project. If multiple such classes are defined, the implemented methods will not be called. Ensure that your project has only one class that extends HmsMessageService.

For example, if you use a third-party push SDK and the SDK has defined a class that extends HmsMessageService, you do not need to define another class that extends HmsMessageService. In this case, you need to ask technical support personnel of the third-party SDK about how to use this defined class. You can view the Android declaration file through decompilation to check whether a class that extends HmsMessageService is defined in the third-party SDK. You can check logs on the device to determine how many classes extend HmsMessageService. The procedure for capturing logs is the same as that described previously. The following introduces how to view key logs.

Key logs:

View attachment 5808243

Search for HmsMessageService num is in the logs and find the logs shown in the figure above. Then, find the logs of your app based on packageName in the log context. The number next to HmsMessageService num is is the number of classes that extend HmsMessageService. If the number is not 1, check your project code and delete redundant classes that extend HmsMessageService.

References​

Push Kit official website

Push Kit documentation