每当我刷新应用程序时,我都会遇到多次调用onLaunch
方法的问题。
我在这里找到了git问题链接
您可以删除 Firebase 应用并再次请求权限。
在下面的示例中,我将把用户传递到 on Launch:
onLaunch(User user) async {
Firebase.initializeApp().then((value) async {
try {
value.delete(); // The default Firebase app instance cannot be deleted. but this will remove previous subscriptions.
} catch (e) {}
FirebaseMessaging messaging = FirebaseMessaging.instance;
NotificationSettings settings = await messaging.requestPermission(
alert: true,
announcement: false,
badge: true,
carPlay: false,
criticalAlert: false,
provisional: false,
sound: true,
);
String token = await messaging.getToken(); // get the token
user.mobileId = token.toString();
BackendRequester.registerPushClient(user); // send it to the backend
messaging.onTokenRefresh.listen((token) { // if token refreshed will notify the backend
user.mobileId = token.toString();
BackendRequester.registerPushClient(user);
});
});
}