My word game published at Huawei AppGallery uses Account and Push Kits:
implementation 'com.huawei.hms:hwid:6.4.0.300'
implementation 'com.huawei.hms:push:6.3.0.302'
The Account Kit works well and I am able to obtain the open id and display name of the phone user.
This means, that the SHA-256 certificate fingerprints are configured properly and are not causing the problem with Push Kit described below.
The problem: the push token is not obtainable on an EMUI 9.1 phone (see screenshot at the very bottom).
AndroidManifest.xml:
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<application>
<service
android:name="de.afarber.HmsService"
android:exported="false">
<intent-filter>
<action android:name="com.huawei.push.action.MESSAGING_EVENT" />
</intent-filter>
</service>
</application>
HmsService.java:
public class HmsService extends HmsMessageService {
@Override
public void onNewToken(String token) { // THIS IS NEVER CALLED !!!
super.onNewToken(token);
Log.d(Utils.TAG,"onNewToken token=" + token);
}
}
My custom Application class:
private final ScheduledExecutorService mExecutor = Executors.newSingleThreadScheduledExecutor();
@Override
public void onCreate(@NonNull Context context) {
HmsMessaging.getInstance(this).setAutoInitEnabled(true);
mExecutor.execute(() -> {
try {
String appId = context.getString(R.string.huawei_app_id); // VALUE IN DEBUGGER: "102776361"
String token = HmsInstanceId.getInstance(context).getToken(appId, "HCM"); // ALWAYS EMPTY
if (!TextUtils.isEmpty(token)) {
// this only supposed to work for EMUI 10 or newer
Log.d(Utils.TAG,"getToken token=" + token);
}
} catch (Exception ex) {
Log.w(TAG,"getToken failed", ex);
}
});
}
When I debug the app at my Huawei ANE-LX1 phone I see in the logcat:
I/HMSSDK_HMSPackageManager: <initHmsPackageInfoForMultiService> Succeed to find HMS apk: com.huawei.hwid version: 60400311
...
I/HMSSDK_PendingResultImpl: init uri:push.gettoken
...
I/HMSSDK_HmsClient: post msg api_name:push.gettoken, app_id:102776361|, pkg_name:com.wordsbyfarber.huawei, sdk_version:60400300, session_id:*, transaction_id:102776361ttoken20220330184241694787985, kitSdkVersion:60300301, apiLevel:1
I/HMSSDK_BaseAdapter: In constructor, activityWeakReference is java.lang.ref.WeakReference@343238b, activity is de.afarber.MainActivity@50109d0
I/HMSSDK_BaseAdapter: in baseRequest + uri is :push.gettoken, transactionId is : 102776361ttoken20220330184241694787985
I/HMSSDK_PendingResultImpl: init uri:push.gettoken
I/HMSSDK_PendingResultImpl: setResultCallback
I/HMSSDK_PendingResultImpl: setResult:0
...
I/HMSSDK_BaseAdapter: baseCallBack.onComplete
I/HMSSDK_HmsClient: receive msg status_code:0, error_code:0, api_name:push.gettoken, app_id:102776361|, pkg_name:com.wordsbyfarber.huawei, session_id:*, transaction_id:102776361ttoken20220330184241642989857, resolution:null
I/HMSSDK_TaskApiCall: doExecute, uri:push.gettoken, errorCode:0, transactionId:102776361ttoken20220330184241642989857
I/HMSSDK_HmsInstanceId: GetTokenTask receive a empty token, please check HmsMessageService.onNewToken receive result.
I/HMSSDK_HMSPackageManager: Enter getHMSPackageNameForMultiService
I/HMSSDK_RequestManager: removeReqByTransId
I/HMSSDK_BaseAdapter: api is: push.gettoken, resolution: null, status_code: 0
I/HMSSDK_BaseAdapter: baseCallBack.onComplete
I/HMSSDK_HmsClient: receive msg status_code:0, error_code:0, api_name:push.gettoken, app_id:102776361|, pkg_name:com.wordsbyfarber.huawei, session_id:*, transaction_id:102776361ttoken20220330184241694787985, resolution:null
I/HMSSDK_TaskApiCall: doExecute, uri:push.gettoken, errorCode:0, transactionId:102776361ttoken20220330184241694787985
I/HMSSDK_HmsInstanceId: GetTokenTask receive a empty token, please check HmsMessageService.onNewToken receive result.
I/HMSSDK_HMSPackageManager: Enter getHMSPackageNameForMultiService
I/HMSSDK_RequestManager: removeReqByTransId
I/HMSSDK_AutoInit: Push init succeed
And by setting debugger breakpoints and inspecting logcat for my logs I see that:
I have unsuccessfully tried multiple things to resolve my issue -
Tried adding following meta data to the AndroidManifest.xml:
Tried obtaining AAID manually in onCreate (obtaining AAID works OK, but token is still not delivered):
HmsInstanceId.getInstance(context).getAAID()
.addOnSuccessListener(aaidResult -> Log.d(TAG, "getAAID aaid=" + aaidResult.getId()))
.addOnFailureListener(ex -> Log.w(TAG, "getAAID failed", ex));
Tried adding more permissions to the AndroidManifest.xml:
android.permission.READ_PHONE_STATE android.permission.ACCESS_NETWORK_STATE android.permission.ACCESS_WIFI_STATE android.permission.WRITE_EXTERNAL_STORAGE android.permission.REQUEST_INSTALL_PACKAGES
Tried exporting HmsService in the AndroidManifest.xml (this is not recommended according to @shirley!):
<service
android:name="de.afarber.HmsService"
android:enabled="true"
android:exported="true"
android:permission="${applicationId}.permission.PROCESS_PUSH_MSG"
android:process=":HmsMessageService">
<intent-filter>
<action android:name="com.huawei.push.action.MESSAGING_EVENT" />
</intent-filter>
</service>
Tried overriding public void onNewToken(String token, Bundle bundle)
in HmsService.java
My EMUI 9.1 phone is up to date and I am located in Germany:
UPDATE:
I have prepared a simple test case at Github and there the push token is delivered to the onNewToken()
method just fine:
After adding onCreate()
to the both custom HmsMessageService classes I have noticed that the method is called in the test app, but not in my real app.
Unfortunately, I have not found the reason for that yet... Here a screenshot of the merged AndroidManifest.xml:
I have tried starting the service by running the following line in the onCreate() method of my custom Application class:
startService(new Intent(this, de.afarber.HmsService.class));
And the line does not fail and I can see the onCreate()
method of the HmsService
being run. However the onNewToken()
method is still not called. I wonder if some additional registration is needed in the HMS Core Android code after starting the service "manually".
UPDATE 2:
After rechecking all settings and I have noticed, that I didn't enable HUAWEI Push Kit in the AppGallery Connect:
After enabling that setting I am able to receive push token on my Huawei Phone.
And after auto-updating I am even receiving a push token on a non-Huawei phone: Moto G Play.
We are checking on your issue. Can you help to confirm:
Don't config the following items: android:permission="${applicationId}.permission.PROCESS_PUSH_MSG" android:process=":HmsMessageService"
After the confirmation above, you can share more log to me.