简体   繁体   English

使用 Firebase 通知用户新登录 - Swift/Xcode

[英]Notify User of New Signin with Firebase - Swift/Xcode

I have looked all over and can't figure out how to notify a user when a new device signs into their account.我已经查看了所有内容,但无法弄清楚如何在新设备登录用户帐户时通知用户。 I'm using Firebase Authentication.我正在使用 Firebase 身份验证。 I'd like to setup some kind of notification system or email that tells the user someone has signed into their account from a different device so they can know for security reasons.我想设置某种通知系统或 email,告诉用户有人从不同的设备登录到他们的帐户,以便他们出于安全原因知道。

Ideas?想法?

Also, how could you monitor information about what device (s) are signed into a specific account?此外,您如何监控有关哪些设备登录到特定帐户的信息? For example, maybe the user is curious how many devices he has signed into his account, what type of device they are, what the name of those devices are, and where they are located (example: San Antonio, Texas).例如,用户可能想知道他登录了多少台设备、它们是什么类型的设备、这些设备的名称是什么以及它们位于何处(例如:德克萨斯州圣安东尼奥)。

Ideas?想法?

yes, you can use keychain for this situation.是的,您可以在这种情况下使用钥匙串。 First you should create an uniqueId.首先,您应该创建一个 uniqueId。 After you can save to the db uniqueId.在你可以保存到 db uniqueId 之后。 This value will change if the user uses another device.如果用户使用其他设备,此值将更改。 I'm using third party framework for Keychain services.我正在为钥匙串服务使用第三方框架。 You can use framework.你可以使用框架。 It's perfect.这是完美的。

https://github.com/kishikawakatsumi/KeychainAccess https://github.com/kishikawakatsumi/KeychainAccess

final class DeviceManager {
    
    static let shared = DeviceManager()
    
    private init() { }
    
    var uniqueDeviceId: String {
        get {
            let keychain = Keychain(service: KeyManager.keychainServiceName).accessibility(.always)
            if let deviceId = keychain[KeyManager.keychainDeviceIdKey] {
                return deviceId
            }
            
            let vendorId = UIDevice.current.identifierForVendor!.uuidString
            keychain[KeyManager.keychainDeviceIdKey] = vendorId
            
            return vendorId
        }
    }
}

Super simple;超级简单; have a field in your user document that stores a device name along with its status.在您的用户文档中有一个字段,用于存储设备名称及其状态。

You app will be observing this users document and when something changes, all of the users devices will be notified of that change.您的应用程序将观察此用户文档,并且当某些内容发生变化时,所有用户设备都将收到该更改的通知。

Let me set this up;让我设置一下; here's a basic Firestore structure这是一个基本的 Firestore 结构

users
   uid_0
      userName: "Jay"
      devices:
         device_0: offline
         device_1: offline

When the app starts, it will add an observer to this users document (using the uid as the documentId)当应用程序启动时,它会向这个用户文档添加一个观察者(使用 uid 作为 documentId)

func observeUser() {
    let usersCollection = self.db.collection("users")
    usersCollection.document("uid_0").addSnapshotListener { (documentSnapshot, err) in

        guard let document = documentSnapshot else {
            print("Error fetching document: \(err!)")
            return
        }

        let device = document.get("devices") as! [String: String]
        print(device)
    }
}

Now in the Firestore closure shown above, if a users device changes status, offline to online for example, it outputs all of the devices to console.现在在上面显示的 Firestore 闭包中,如果用户设备更改状态,例如离线到在线,它会将所有设备输出到控制台。 You would take whatever action is needed when the device changes status.当设备改变状态时,您将采取任何需要的行动。

Keep in mind that if a NEW device is added, that event will also fire so you could present a message in the UI "A new device was added!"请记住,如果添加了新设备,该事件也会触发,因此您可以在 UI 中显示一条消息“已添加新设备!”

So then some testing code that toggles the device_0 status from offline to online.然后是一些将 device_0 状态从离线切换到在线的测试代码。 I have a button click that does self.status =.self.status and then calls the toggleStatus function我有一个按钮点击self.status =.self.status然后调用toggleStatus function

var status = false
func toggleStatus() {
    var isOnline = ""
    if self.status == false {
        isOnline = "online"
    } else {
        isOnline = "offline"

    }
    let userCollection = self.db.collection("users")
    let thisDevice = "device_0"

    let devicesDict = [
        "devices":
            [thisDevice: isOnline] //sets device_0 to offline or online
    ]

    let document = usersCollection.document("uid_0").setData(devicesDict, merge: true)
}

In a nutshell, when a user authenticates with a device for the first time, it would perhaps ask for a device name, or craft one from the devices mac address or something under the hood.简而言之,当用户第一次使用设备进行身份验证时,它可能会询问设备名称,或者根据设备的 mac 地址或其他内容制作一个。 That device name is stored in the users document/devices with it's online status.该设备名称以其在线状态存储在用户文档/设备中。

The device name would be stored locally as well, in user defaults for example so it's automatically sent up to Firestore upon login.设备名称也将存储在本地,例如在用户默认设置中,因此它会在登录时自动发送到 Firestore。

The end result here is that if any users devices change status;这里的最终结果是,如果任何用户设备改变状态; offline to online or vice versa, or any device is added or removed all of the devices are notified of that event.离线到在线,反之亦然,或者添加或删除任何设备,所有设备都会收到该事件的通知。

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

相关问题 “标记”/报告用户功能正常工作 — Swift / Xcode / Firebase - "Flag" / Report user functionality to work — Swift / Xcode / Firebase 在 Swift 中向新的 Firebase 用户发送电子邮件验证电子邮件 - Send an email verfication email to a new Firebase User in Swift HomeViewController 在 AppDelegate.swift 中的 Firebase Google SignIN 后未加载 - HomeViewController not loading after Firebase Google SignIN in AppDelegate.swift 使用 firebase admin sdk 创建用户,可以使用 email 和密码登录 - Create user with firebase admin sdk that can signIn using email and password 每次用户登录时在 Firebase Google 登录中强制输入密码 - Force password in Firebase Google signin every time the user login 如何使用google auth区分firebase中的登录和注册用户? - How to differentiate signin and signup user in firebase using google auth? Firebase 登录未运行 - Firebase signIn not running 如何使用 gcloud 控制台或 firebase 工具 CLI 为新的 firebase 项目启用 email 和密码登录提供程序? - How to enable email and password signin provider for new firebase project using gcloud console or firebase tools CLI? 没有这样的模块 'Firebase' - Xcode 和 Swift Package 经理 - No such module 'Firebase' - Xcode and Swift Package Manager 在 firebase 中创建新用户的问题 - Problems with creating new user in firebase
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM