0
votes

I want use Huawei Push Kit in my Appcelerator Titanium app with Hyperloop.

    var tokenString = '';
    var Activity = require('android.app.Activity');
    var ActivityToken = require('com.huawei.hms.aaid.HmsInstanceId');

try{    
        const activity = new Activity(Ti.Android.currentActivity);
        tokenString = ActivityToken.getInstance(activity).getToken(appID, "HCM");
        console.log('tokenString', tokenString); 
}
catch (e){
    console.log(e);
}

But I receive error: "operation in MAIN thread prohibited"

How do I run the code in a separate thread?

2
Could you try the Automatic Initialization to get the token? ActivityToken.getInstance(activity).setAutoInitEnabled(true); Token returned by using the onNewToken(String token) method in the customized class that inherits HmsMessageService. - shirley

2 Answers

1
votes

You could try the Automatic Initialization, by calling the setAutoInitEnabled(boolean enable) method in HmsMessaging.

ActivityToken.getInstance(activity).setAutoInitEnabled(true);

The applied token is returned through the onNewToken() method after completing the configuration.

-1
votes

Wrap around the code inside background thread, since the error says clearly that this can not be run on main thread. Time consuming calls are usually not allowed in main thread, onCreate etc.

Thread { 
…
    }.run()