离线提醒

更新时间:2024-08-22 03:38:21下载pdf

当普通设备离线超过 30 分钟或者低功耗设备离线超过 8 个小时,手机推送中心App 消息中心 都将收到离线提醒的消息。

接入

source 'https://github.com/tuya/tuya-pod-specs.git'
platform :ios, '11.0'

target 'Your_Project_Name' do
    pod "ThingSmartBusinessExtensionKit"
end

API 说明

判断设备是否支持离线提醒

入参 类型 说明
deviceId string 设备 ID
success block 成功回调
failure block 失败回调
/// get the offline reminder support status of device
/// - Parameters:
///   - deviceId: the ID of the device
///   - success: supported if status is true, otherwise not supported
///   - failure: an error occurs
class func getOfflineReminderSupportStatus( withDeviceId deviceId: String, success: @escaping (Bool) -> Void, failure: @escaping (Error) -> Void)

获取设备当前的离线提醒状态

入参 类型 说明
deviceId string 设备 ID
success block 成功回调
failure block 失败回调
/// get the offline reminder status of device
/// - Parameters:
///   - deviceId: the ID of the device
///   - success: open if status is true, otherwise close
///   - failure: an error occurs
class func getOfflineReminderStatus(withDeviceId deviceId: String,  success: @escaping (Bool) -> Void, failure: @escaping (Error) -> Void)

更新设备的离线提醒状态

入参 类型 说明
deviceId string 设备 ID
status bool 打开为 true,关闭为 false
success block 成功回调
failure block 失败回调
/// update the offline reminder status of device
/// - Parameters:
///   - deviceId: the ID of the device
///   - status: the offline reminder status
///   - success: update success
///   - failure: an error occurs
class func updateOfflineReminderStatus(withDeviceId deviceId: String, status: Bool, success: @escaping () -> Void, failure: @escaping (Error) -> Void)

调用示例

class DeviceOfflineReminderVC: UIViewController {

    var deviceId: String

    lazy var label: UILabel = {
        let label = UILabel(frame: CGRect(x: 0, y: 0, width: 0, height: 0))
        label.textColor = UIColor.green
        return label
    }()

    lazy var statusSwitch: UISwitch = {
        let statusSwitch = UISwitch(frame: CGRectZero)
        statusSwitch.addTarget(self, action: #selector(change(sender:)), for: .valueChanged)
        return statusSwitch
    }()

    init(deviceId: String) {
        self.deviceId = deviceId
        super.init(nibName: nil, bundle: nil)
    }

    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }


    override func viewDidLoad() {
        super.viewDidLoad()

        self.view.addSubview(self.label)
        self.view.addSubview(self.statusSwitch)

        self.label.frame = CGRect(x: 20, y: 100, width: 150, height: 27)
        self.statusSwitch.frame = CGRect(x: 170, y: 100, width: 0, height: 0)
        self.statusSwitch.isHidden = true
        self.loadData()

    }

    func loadData() {
        SVProgressHUD.show()
        ThingDeviceOfflineReminderManager.getOfflineReminderSupportStatus(withDeviceId: self.deviceId) { support in
            if (support) {

                ThingDeviceOfflineReminderManager.getOfflineReminderStatus(withDeviceId: self.deviceId) { status in
                    SVProgressHUD.dismiss()
                    self.label.text = "support"
                    self.statusSwitch.isHidden = false
                    self.statusSwitch.isOn = status
                } failure: { e in
                    SVProgressHUD.dismiss()
                    self.label.text = "support"
                    self.statusSwitch.isHidden = false
                    self.statusSwitch.isOn = false
                }


            }else{
                SVProgressHUD.dismiss()
                self.label.text = "do not support"
                self.statusSwitch.isHidden = true
            }
        } failure: { e in
            SVProgressHUD.dismiss()
        }
    }

    @objc func change(sender: UISwitch) {
        ThingDeviceOfflineReminderManager.updateOfflineReminderStatus(withDeviceId: self.deviceId, status: sender.isOn) {

        } failure: { e in

        }
    }
}

实现简单的离线提醒功能

  1. 首先,简单实现一个界面。该界面初始化的时候,会传入 deviceId 作为当前的设备 ID。

    另外,会在界面上显示一个 UILabel 和一个 UISwitch。当 UISwitch 被单击时,它会调用 change(sender:) 方法。因为还不知道设备是否支持离线提醒,所以先将 UISwitch 隐藏起来。

    class DeviceOfflineReminderVC: UIViewController {
    
        var deviceId: String
    
        lazy var label: UILabel = {
            let label = UILabel(frame: CGRect(x: 0, y: 0, width: 0, height: 0))
            label.textColor = UIColor.green
            return label
        }()
    
        lazy var statusSwitch: UISwitch = {
            let statusSwitch = UISwitch(frame: CGRectZero)
            statusSwitch.addTarget(self, action: #selector(change(sender:)), for: .valueChanged)
            return statusSwitch
        }()
    
        init(deviceId: String) {
            self.deviceId = deviceId
            super.init(nibName: nil, bundle: nil)
        }
    
        required init?(coder: NSCoder) {
            fatalError("init(coder:) has not been implemented")
        }
    
    
        override func viewDidLoad() {
            super.viewDidLoad()
    
            self.view.addSubview(self.label)
            self.view.addSubview(self.statusSwitch)
    
            self.label.frame = CGRect(x: 20, y: 100, width: 150, height: 27)
            self.statusSwitch.frame = CGRect(x: 170, y: 100, width: 0, height: 0)
            self.statusSwitch.isHidden = true
        }
    
        @objc func change(sender: UISwitch) {
    
        }
    }
    
  2. 接下来,新增一个 loadData 的方法,该方法会通过 ThingDeviceOfflineReminderManager.getOfflineReminderSupportStatus(withDeviceId:success:failure:) 来判断设备是否支持离线提醒,并在 viewDidLoad 中调用它。当出现错误或者返回的 supportfalse 时,表示当前设备不支持离线提醒。将 UILabel 的文案设置为 "not support"

    class DeviceOfflineReminderVC: UIViewController {
    
        override func viewDidLoad() {
            ...
            self.statusSwitch.isHidden = true
            self.loadData()
    
        }
    
        func loadData() {
            SVProgressHUD.show()
            ThingDeviceOfflineReminderManager.getOfflineReminderSupportStatus(withDeviceId: self.deviceId) { support in
                if (support) {
                    //todo
    
                }else{
                    SVProgressHUD.dismiss()
                    self.label.text = "not support"
                    self.statusSwitch.isHidden = true
                }
            } failure: { e in
                SVProgressHUD.dismiss()
                self.label.text = "not support"
                self.statusSwitch.isHidden = true
            }
        }
    }
    

    supporttrue 时,需要通过 ThingDeviceOfflineReminderManager.getOfflineReminderStatus(withDeviceId:success:failure:) 来获取当前设备离线提醒的状态,并相应地更新 UISwitch 的状态。

    class DeviceOfflineReminderVC: UIViewController {
    
        func loadData() {
            SVProgressHUD.show()
            ThingDeviceOfflineReminderManager.getOfflineReminderSupportStatus(withDeviceId: self.deviceId) { support in
                if (support) {
    
                    ThingDeviceOfflineReminderManager.getOfflineReminderStatus(withDeviceId: self.deviceId) { status in
                        SVProgressHUD.dismiss()
                        self.label.text = "support"
                        self.statusSwitch.isHidden = false
                        self.statusSwitch.isOn = status
                    } failure: { e in
                        SVProgressHUD.dismiss()
                        self.label.text = "support"
                        self.statusSwitch.isHidden = false
                        self.statusSwitch.isOn = false
                    }
    
    
                }else{
                    SVProgressHUD.dismiss()
                    self.label.text = "not support"
                    self.statusSwitch.isHidden = true
                }
            } failure: { e in
                SVProgressHUD.dismiss()
                self.label.text = "not support"
                self.statusSwitch.isHidden = true
            }
        }
    }
    
  3. 最后,当 UISwitch 被单击时,通过 ThingDeviceOfflineReminderManager.updateOfflineReminderStatus(withDeviceId:status:success:failure:) 来更新设备离线提醒的状态。

    class DeviceOfflineReminderVC: UIViewController {
            @objc func change(sender: UISwitch) {
            ThingDeviceOfflineReminderManager.updateOfflineReminderStatus(withDeviceId: self.deviceId, status: sender.isOn) {
    
            } failure: { e in
    
            }
        }
    }
    

Demo

更多信息,参考 Demo