华为推送套件Flutter:Push.getToken()返回ResultCode-5



当我查看文档时,它告诉-5与proguard规则有关,所以我在这里添加了它们。https://developer.huawei.com/consumer/en/doc/development/HMSCore-References-V5/error-code-0000001050255690-V5

android/proguard-rules.pro

## Flutter wrapper
-keep class io.flutter.app.** { *; }
-keep class io.flutter.plugin.**  { *; }
-keep class io.flutter.util.**  { *; }
-keep class io.flutter.view.**  { *; }
-keep class io.flutter.**  { *; }
-keep class io.flutter.plugins.**  { *; }
-dontwarn io.flutter.embedding.**
## HMS Core SDK
-ignorewarnings
-keepattributes *Annotation*
-keepattributes Exceptions
-keepattributes InnerClasses
-keepattributes Signature
-keepattributes SourceFile,LineNumberTable
-keep class com.huawei.hianalytics.**{*;}
-keep class com.huawei.updatesdk.**{*;}
-keep class com.huawei.hms.**{*;}

android/app/build.gradle部分:

buildTypes {
release {
// TODO: Add your own signing config for the release build.
// Signing with the debug keys for now, so `flutter run --release` works.
signingConfig signingConfigs.release
useProguard true
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}

以下是代码init和get token部分:

class HomePage extends StatefulWidget {
@override
_HomePageState createState() => _HomePageState();
}
class _HomePageState extends State<HomePage> {

String _token = '';
@override
void initState() {
HwAds.init();
initPush();
super.initState();
}
Future<void> initPush() async {
if (!mounted) return;
Push.getTokenStream.listen(_onTokenEvent, onError: _onTokenError);
}
void _onTokenEvent(String event) {
print('onTokenEvent');
_token = event;
showResult("TokenEvent", _token);
}
void _onTokenError(Object error) {
print('onTokenError');
PlatformException e = error;
showResult("TokenErrorEvent", e.message);
}
void showResult(String name, [String msg = "Button pressed."]) {
print('$name: $msg');
if (msg.isNotEmpty) Push.showToast(msg);
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Home'),
),
body: SingleChildScrollView(
child: Column(
children: [
RaisedButton(
child: Text('Interstitial Ad Page'),
onPressed: ()=> Navigator.pushNamed(context, '/interstitialPage'),
),
RaisedButton(
child: Text('Get token'),
onPressed: ()=> Push.getToken(''),
),
],
),
),
);
}
}

我使用的是云调试,型号名称是P40。我创建了一个发布的apk,选择了数据存储位置,提供了SHA256,启用了Push Kit,在启用Push Kit后更新了agconnect服务。我还缺少什么吗?

您可以尝试添加

-keep class com.huawei.hms.flutter.** { *; }

到您的proguard-rules.pro

查看此官方样品

https://github.com/HMS-Core/hms-flutter-plugin/blob/master/flutter-hms-push/example/android/app/proguard-rules.pro

我希望我的答案能帮助

试着调用getToken来生成令牌,然后调用getTokenStrem,这对我有效

Push.getToken('');
final token = await Push.getTokenStream.first;

没有。我只是困惑于我应该在Scope中发送什么,而不是一个空的存储

相关内容

  • 没有找到相关文章

最新更新