Aza
Aza

Reputation: 63

Firebase FCM and Subscribing to Topics Clarification

#1 I'm confused once didReceiveRegistrationToken gets the fcmToken how that is saved to Cloud Messaging. I couldn't find a simple answer in the documentation. I save the token to the database by calling UserService.updateChildS(child: "userToken", childVal: fcmToken) so I am able to send push notifications to each user as such.

    Database.database().reference().child("allUsers").child(toId).child("userToken").observeSingleEvent(of: .value, with: { (snapshot) in
        if let toToken = snapshot.value as? String {
            if toToken != "" {
                PushNotifications.sendMessageTouser(to: toToken, title: "Some title", body: properties["text"] as! String)
            }
        }
    })

struct PushNotifications {
    static func sendMessageTouser(to token: String, title: String, body: String) {
        print("sendMessageTouser()")
        let paramString: [String : Any] = ["to" : token,
                                           "notification" : ["title" : title, "body" : body, "sound": "default"]]
        sendMessage(paramString: paramString)
   }

But what I am wondering still is which step in the app delegate sends/pushes/adds the registered token to Cloud Messaging so sendMessageTouser actually works.

Part of App Delegate

extension AppDelegate : MessagingDelegate {
    func messaging(_ messaging: Messaging, didReceiveRegistrationToken fcmToken: String) {

        if let _ = Auth.auth().currentUser,
           let userData = UserDefaults.standard.object(forKey: Constants.UserDefaults.currentUser) as? Data,
           let user = try? JSONDecoder().decode(User.self, from: userData) {
            User.setCurrent(user)
            UserService.updateChildS(child: "userToken", childVal: fcmToken)
        } else {
            UserDefaults.standard.set(fcmToken, forKey: "fcmToken")
        }

    }
    func messaging(_ messaging: Messaging, didRefreshRegistrationToken fcmToken: String) {
        UserService.show(forUID: User.current.uid) { (user) in
            if let user = user {
                for group in user.groups {
                    Messaging.messaging().subscribe(toTopic: group.trimmingCharacters(in: .whitespaces)) { error in
                        
                    }
                }
            }
        }
        UserService.updateChildS(child: "userToken", childVal: fcmToken)
    }
}

#2 I subscribe users to topics so they can get group chat notifications. Is the token used to subscribe a user to a topic the same one as the fcmToken above? Because I was thinking if it changes, I'd need to subscribe users to their existing chats using the refreshed registration token in didRefreshRegistrationToken, right?

Upvotes: 0

Views: 339

Answers (1)

Frank van Puffelen
Frank van Puffelen

Reputation: 598668

which step in the app delegate sends/pushes/adds the registered token to Cloud Messaging so sendMessageTouser actually works.

The token is actually sent to FCM before your code is invoked in any way. Only once FCM knows about the token, will your code be called so that you can also register it.

Upvotes: 1

Related Questions