Device Offline Notification

Last Updated on : 2024-08-22 05:39:37download

When a common device stays offline for more than 30 minutes or a low power device is offline for more than 8 hours, both the phone’s push notifications and the app’s Message Center will receive a message.

Integration

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

target 'Your_Project_Name' do
    pod "ThingSmartBusinessExtensionKit"
end

API description

Check support for offline notifications

Request parameters Type Description
deviceId string The device ID.
success block The success callback.
failure block The failure callback.
/// 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)

Get offline notification status

Request parameters Type Description
deviceId string The device ID.
success block The success callback.
failure block The failure callback.
/// 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)

Update offline notification status

Request parameters Type Description
deviceId string The device ID.
status bool true: Enable. false: Disable.
success block The success callback.
failure block The failure callback.
/// 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)

Example

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

        }
    }
}

Implementing notifications for device offline events

  1. Implement a user interface. When the interface is initialized, pass in the deviceId as the device ID.

    Show UILabel and UISwitch on the interface. Tapping UISwitch will invoke the change(sender:) method. Hide UISwitch because it is currently unknown if the device supports offline notifications.

    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. Add the loadData method that checks whether the device supports offline notifications through ThingDeviceOfflineReminderManager.getOfflineReminderSupportStatus(withDeviceId:success:failure:), and call this method back in viewDidLoad. When an error occurs or the returned value of support is false, the device does not support offline notifications. Set the UILabel text to "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
            }
        }
    }
    

    When support is true, request the current device’s offline notification status through ThingDeviceOfflineReminderManager.getOfflineReminderStatus(withDeviceId:success:failure:) and update UISwitch accordingly.

    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. When UISwitch is tapped, update the offline notification status through 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
    
            }
        }
    }
    

    For more information, see Demo.

Demo

For more information, see Demo.