Heartbeat always nil

Hello, I try to get user heartBeat but it always nil

here login
func login(sessionHandler: SessionHandler, id: String, name: String?, token: String?, imageUrl: String) {

    Task { @MainActor in
        do {
            let client = AmityUIKitManager.client

            try await client.login(userId: id,
                                   displayName: name,
                                   authToken: .empty,
                                   sessionHandler: sessionHandler)
            try await client.presence.enable()
            try await client.presence.startHeartbeat()

            let builder = AmityUserUpdateBuilder()

            builder.setAvatarCustomUrl(imageUrl)
            builder.setDisplayName(name)
            client.updateUser(builder) { success, _ in
            
            }
        } catch {
            print("Login error: \(error)")
        }
    }
}

func display(with channel: AmityChannelModel) {
    badgeView.badge = channel.unreadCount
    dateTimeLabel.text = AmityDateFormatter.Chat.getDate(date: channel.lastActivity)
    
    switch channel.channelType {
    case .standard:
        avatarView.setImage(withImageURL: channel.avatarURL, placeholder: AmityIconSet.defaultGroupChat)
    case .conversation:
        
        var currentUser = channel.object.currentMembership?.user
  
        let membershipParticipation = AmityChannelParticipation(client: AmityUIKitManager.client, andChannel: channel.channelId)
        
        token = membershipParticipation
            .getMembers(filter: .all, sortBy: .firstCreated, roles: [])
            .observeOnce({ members, change, error in
                for i in members.snapshots {
                    if i.userId != currentUser?.userId && !i.userId.hasPrefix("_admin") {
                        self.titleLabel.text = i.user?.displayName
                        let imageUrl = i.user?.avatarCustomUrl ?? ""
                        self.avatarView.loadImage(url: imageUrl)
                        
                        print( i.user?.lastHeartbeat ) <<< here nil
                        if let lastHeartbeat = i.user?.lastHeartbeat {
                            let timeInterval = Date().timeIntervalSince(lastHeartbeat)
                            if timeInterval > 60 {
                                self.isOnlineImage.backgroundColor = UIColor(named: "secondUnderlineColor")
                               
                            } else {
                                self.isOnlineImage.backgroundColor = .clear
                            }
                        } else {
                            print("No lastHeartbeat available")
                        }
                        
                        let text = channel.object.messagePreview?.data?["text"] as? String ?? ""
                        
                        if !text.isEmpty {
                            self.previewMessageLabel.font = UIFont(name: "Inter-Regular", size: 14)
                            self.previewMessageLabel.text = text
                        } else {
                            if channel.object.messagePreview?.dataType == .audio {
                                self.crownImage.isHidden = true
                                self.previewLeadingConstraint.constant = 0
                                self.previewMessageLabel.font = UIFont(name: "Inter-Regular", size: 14)
                                self.previewMessageLabel.textColor = AmityColorSet.base.blend(.shade2)
                                self.previewMessageLabel.text = "Audio"
                            } else if channel.object.messagePreview?.dataType == .video {
                                self.crownImage.isHidden = true
                                self.previewLeadingConstraint.constant = 0
                                self.previewMessageLabel.font = UIFont(name: "Inter-Regular", size: 14)
                                self.previewMessageLabel.textColor = AmityColorSet.base.blend(.shade2)
                                self.previewMessageLabel.text = "Video"
                            } else if channel.object.messagePreview?.dataType == .image {
                                self.crownImage.isHidden = true
                                self.previewLeadingConstraint.constant = 0
                                self.previewMessageLabel.font = UIFont(name: "Inter-Regular", size: 14)
                                self.previewMessageLabel.textColor = AmityColorSet.base.blend(.shade2)
                                self.previewMessageLabel.text = "Image"
                            } else {
                                self.crownImage.isHidden = true
                                self.previewLeadingConstraint.constant = 0
                                self.previewMessageLabel.font = UIFont(name: "Fraunces9pt-SemiBold", size: 14)
                                self.previewMessageLabel.textColor = .black
                                let name = i.user?.displayName ?? ""
                                self.previewMessageLabel.text = "Start conversation with \(name)"
                            }
                        }
                    }
                }
            })
    case .community:
        avatarView.setImage(withImageURL: channel.avatarURL, placeholder: AmityIconSet.defaultGroupChat)
    case .private, .live, .broadcast, .unknown:
        break
    @unknown default:
        break
    }
    
    
}

Hello, may we know your current iOS sdk version, please?

– ** AmitySDK Version: 6.34.0 Build: 1 ** –

1 Like

Hello @weeew , Let me pass this to our team for checking, and I’ll get back to you.

1 Like

Hello, could you please confirm if you have already requested us to enable the presence state for you?

Docs: Presence State | Amity Docs

Hello, yes I am doing this, here is the code:
Task { @MainActor in
do {
let client = AmityUIKitManager.client

        try await client.login(userId: id,
                               displayName: name,
                               authToken: .empty,
                               sessionHandler: sessionHandler)
        try await client.presence.enable()
        try await client.presence.startHeartbeat()

        let builder = AmityUserUpdateBuilder()

        builder.setAvatarCustomUrl(imageUrl)
        builder.setDisplayName(name)
        client.updateUser(builder) { success, _ in
        
        }
    } catch {
        print("Login error: \(error)")
    }
}

}

Hello @weeew , Regarding the issue you’re experiencing, we recommend submitting a request to enable presence state through the following link: Enable Presence State Request

Hello,thanks, my application has been approved, but the heartbeat is still nil.

Hello @weeew , Let our team check on this further, and I’ll keep you updated.

1 Like

Hello @weeew , After further investigation, we found that the attribute user?.lastHeartbeat only works with conversation channels. You can refer to the documentation here: Channel Presence Documentation.

For your specific use case, we recommend implementing the feature by referring to this document: User Presence Documentation.

If you have any further questions or need additional assistance, please feel free to reach out.