简体   繁体   English

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

[英]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.每当我刷新应用程序时,我都会面临多次调用onLaunch方法的问题。

I have found git issue here在这里找到了 git 问题

You can delete the Firebase application and requestPermission egain.您可以删除 Firebase 应用程序并重新请求权限。

In the bellow example i will pass the user to the on Launch:在下面的示例中,我会将用户传递给 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);
    });
  });
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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