Flutter Firebase push onLaunch 在应用刷新时被多次调用

Sha*_*dal 6 push-notification firebase flutter

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

在这里发现了 git 问题

小智 0

您可以删除 Firebase 应用程序并再次请求权限。

在下面的示例中,我将把用户传递到启动时:

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);
    });
  });
}
Run Code Online (Sandbox Code Playgroud)