Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why can't I get pushToken by using Huawei Push Kit?

I am using following code snippet, however I still can't get the pushToken.

private void obtainToken() {
    // get token
    new Thread() {
        @Override
        public void run() {
            try {
                String appId = AGConnectServicesConfig.fromContext(MainActivity.this).getString("client/app_id");
                pushtoken = HmsInstanceId.getInstance(MainActivity.this).getToken(appId, "HCM");
                if(!TextUtils.isEmpty(pushtoken)) {
                    Log.i(TAG, "get token:" + pushtoken);
                }
            } catch (Exception e) {
                Log.i(TAG,"getToken failed, " + e);

            }
       }
   }.start();
}
like image 945
Irene Li Avatar asked Apr 23 '20 10:04

Irene Li


People also ask

What is Huawei Push Service app?

HUAWEI Push Service serves as a platform for app developers to send new messages in real time from the cloud to a terminal device, improving user awareness and engagement.

Does HMS push kit provides the capability of automatically applying for tokens?

Automatic Initialization The HMS Core Push SDK provides the capability of automatically generating AAIDs and automatically applying for tokens. You can enable or disable automatic initialization by calling Push.

Which of the following scenarios cause the push token to change?

A push token may change in some scenarios, including but not limited to the following: App data is cleared (when the app is uninstalled, when the device is restored to its factory settings, or in other scenarios), and the app is reinstalled and launched.


1 Answers

Having a log would be perfect but if everything fine in the logs, no exception and result code from HCM is success, then verify the EMUI version of your device.

If your device's EMUI version is earlier than 10.0, the code you have used will return empty push token. In such case, it is necessary to implement a custom service extending HmsMessageService.

In your AndroidManifest.xml add;

<service
   android:name=".CustomPushService"
   android:exported="false">
      <intent-filter>
         <action android:name="com.huawei.push.action.MESSAGING_EVENT" />
     </intent-filter>
</service>

Then create following class;

public class CustomPushService extends HmsMessageService {
     private static final String TAG = "PushTokenLog";

     @Override
     public void onNewToken(String token) {

       super.onNewToken(token);
       Log.i(TAG, "receive token:" + token);
      }
}

Last but not least, make sure your device is Huawei :) Most of the features of HMS Core relies on EMUI. Without EMUI, functionality of the functions is not guaranteed for now.

Below is a nice reference to see HMS Core - EMUI relation. https://developer.huawei.com/consumer/en/doc/development/HMS-Guides/emui_version_dependent_features

Update as per the comment of question owner

The return code 907135000 means that your SDK configurations are not correct. Take your time to check following points;

  1. Check whether the app_id and package_name parameters in the agconnect-services.json file are correct. The app_id and package name should match the app created on AGC. Also, consider re-downloading corresponding agconnect-service.json
  2. Check whether the certificate signature is configured in the build.gradle file.
  3. The fault may be caused by the cache of HMS Core (APK). Uninstall and then reinstall HMS Core (APK), disconnect and reconnect the phone with the Internet, and start the app again.
like image 197
captaink Avatar answered Oct 18 '22 01:10

captaink