1

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?

zhangxaochen
  • 32,744
  • 15
  • 77
  • 108
Tvik
  • 35
  • 4
  • Could you try the [Automatic Initialization](https://developer.huawei.com/consumer/en/doc/development/HMSCore-Guides/android-client-dev-0000001050042041#EN-US_TOPIC_0000001050042041__section13546121751811) 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**. – zhangxaochen Nov 30 '20 at 11:02

2 Answers2

1

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.

zhangxaochen
  • 32,744
  • 15
  • 77
  • 108
  • Thank you very much! "setAutoInitEnabled" works. But how can I get a token using "onNewToken"? If I use: `var ActivityToken = require('com.huawei.hms.push.HmsMessaging'); const activity = Ti.Android.currentActivity; ActivityToken.getInstance(activity).setAutoInitEnabled(true); var ActivityGetToken = require('com.huawei.hms.push.HmsMessageService'); ActivityGetToken.onNewToken();` I receive error: "ActivityGetToken.onNewToken is not a function". If I use: `Titanium.App.addEventListener('onNewToken', function(e) { console.log('Ok!'); });`. it never works. – Tvik Dec 01 '20 at 08:07
  • If I use: `var HmsMessageService = require('com.huawei.hms.push.HmsMessageService'); var MyView = com.huawei.hms.push.HmsMessageService.extend({ onNewToken: function(token) { this.super.onNewToken(token); console.log(token); } }); var customView = new MyView(activity);` I receive nothing. – Tvik Dec 01 '20 at 09:35
  • @Tvik I am not aware of Appcelerator Titanium, about how to use **onNewToken()** method on it. :( – zhangxaochen Dec 01 '20 at 11:24
  • I'm not sure if that works in hyperloop since hyperloop is creating things at runtime and you'll need a `service` that points to your class (check https://medium.com/huawei-developers/integrating-huawei-core-push-kit-90f24c8c5d29). Wouldn't it be a lot easier to wrap that as a module? Take an existing push module (e.g. ti.firebase) as a basis and adapt it to the Huawei module. – miga Dec 01 '20 at 17:43
  • @shirley Could you tell me. How can I find out the current push token of the device without overload methods. Maybe there are some properties – Tvik Dec 02 '20 at 06:41
-1

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()
Zinna
  • 1,947
  • 2
  • 5
  • 20
  • The question is specific to Hyperloop in Appcelerator Titanum. This syntax won't help there. – miga Dec 05 '20 at 17:14