简体   繁体   中英

Flutter Firebase push onLaunch called multiple times on app refresh

I'm facing an issue of getting called onLaunch method multiple times whenever I refresh the app.

I have found git issue here

You can delete the Firebase application and requestPermission egain.

In the bellow example i will pass the user to the 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);
    });
  });
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM