简体   繁体   中英

iOS SwuftUI - get firebase messaging user token

I have this code:

@main
    struct AppName: App {
        
        var body: some Scene {
            WindowGroup {
                ContentView()
                    .task {
                        
                        print("tttttt")
                        
                        Messaging.messaging().token { token, error in
                          if let error = error {
                            print("Error fetching FCM registration token: \(error)")
                          } else if let token = token {
                            print("FCM registration token: \(token)")
                          }
                        }
                    }
            }
        }
    }

"tttttt" gets printed out but I don't get the user token nor any error message.

I'm already using google sign in so I guess I have set up firebase sdk correctly

Please help

This is not something you should be doing in a View. Create an ObservableObject type that has the FCM token as an @Published variable. Then in whatever View is internal to your App, just instantiate the ObservableObject and pass it to sub-views as needed as an EnvironmentObject. Then you can reference your Firebase related values in views without issue.

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