Flutter Firebase应用程序刷新时触发多次onLaunch调用

7

每当我刷新应用程序时,我都会遇到多次调用onLaunch方法的问题。

我在这里找到了git问题链接

1个回答

0

您可以删除 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);
    });
  });
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接