iOS MDM详解(5)— 给设备发个指令操作
转自:https://www.jianshu.com/p/23ced86382d9
简介
给设备发个指令操作的第一步不是由MDM Server直接向APNs推送指令的,但是由Server向APNs发送一个特定的指令来好比唤醒设备,设备被唤醒之后会根据已安装的配置文件的ServerURL 的地址主动发起请求,报告自己的当前状态,只有其状态值为Idle设备才会接收Server指令操作。
如图所示:

MDM工作流程
所以完成一次指令推送经历以下过程:
1、server 与APNs建立连接,发送数据。
2、当设备收到APNs推送消息时,主动连接server报告本身的状态空闲
3、server收到设备发来的状态信息,发出操作命令
4、设备收到命令执行,并返回数据
5、server响应,此次查询完成,连接关闭。
以下以设备信息查询指令DeviceInformation为例进一步分析每个过程。
查询设备信息的指令操作过程
MDM Server 与 APNs建立连接,发送一个固定的指令,内容如下。

在这里需要我们前面得到的p12格式的证书,形式上和APP的差不多。其中token就是在TokenUpdate时的token,mdm是其中 的PushMagic,这个值是每次推送时都必须有的。所以根据内容看出Sever与APNs推送的消息基本固定,不同于APP的消息推送。发送这个消息主要目的就是通知设备,MDM Server要给你发指令了,赶快去连接服务器。
当设备收到APNs推送消息主动连接Server
收到有APNs发来的消息,发起请求到通过配置文件的服务器URL(即ServerURL字段的值)。向Server报告自己的当前状态是否空闲。

以上可以看出每次请求或应答都会有UDID来标记设备,Status的值表示设备当前状态。状态值有以下几种状态:
正常情况下大多数出现的是Acknowledged和Idle两种状态。

正常情况下大多数出现的是Acknowledged 和Idle两种状态。
Server收到设备发来的状态信息
收到设备状态信息,判断是否空闲,只有空闲的时候再去发送指令。发送查询设备信息指令:

Server发送一个命令操作时必定包含Command和CommandUUID
Command必须有RequestType表示具体的命令操作 + 该命令相关的操作参数。以上命令用来查询设备信息,Queries数组中表示要查询的内容的key。
CommandUUID表示命令的ID,当设备响应命令操作时,Sever可以此来确定是哪个命令操作,然后做相应的数据处理。
设备收到命令执行,根据指定的key返回相应的数据



查询设备已安装的应用




参考:
1、MDM协议官方文档-Mobile Device Management Protocol Referencehttps://developer.apple.com/library/content/documentation/Miscellaneous/Reference/MobileDeviceManagementProtocolRef/3-MDM_Protocol/MDM_Protocol.html#//apple_ref/doc/uid/TP40017387-CH3-SW2
2、配置描述文件参考-Configuration Profile Referencehttps://developer.apple.com/library/content/featuredarticles/iPhoneConfigurationProfileRef/Introduction/Introduction.html#//apple_ref/doc/uid/TP40010206-CH1-SW1