【发布时间】:2021-10-02 14:26:11
【问题描述】:
我正在 Flutter 中实现华为推送通知。 这里:https://pub.dev/packages/huawei_push/example
我希望能够有一种调用方式:
String? token = await PushNotificationsService().getPushNotificationsToken();
从我的代码和我自己的类 PushNotificationService() 获取令牌并返回它。
我的问题是,对于华为,我需要在 PushNotificationsService() 类中实现一些监听器。
void _onTokenEvent(String event) {
_token = event;
showResult("TokenEvent", _token);
}
void _onTokenError(Object error) {
PlatformException e = error as PlatformException;
showResult("TokenErrorEvent", e.message!);
}
...
Push.getTokenStream.listen(_onTokenEvent, onError: _onTokenError);
但是我应该如何设置我的 getPushNotificationsToken() ?我需要在其中返回一个未来,但是如何处理 Future 以便它只有在我的两个侦听器回调之一被调用时才会“触发”?!
我确信这是一个基本的解决方案,但不幸的是我是 Flutter 的新手。
【问题讨论】:
标签: flutter huawei-mobile-services huawei-developers huawei-push-notification