目录

Pushing an MDM Message

Starting from API version 26.0.1, Push Kit supports MDM message push.

Use Cases

Mobile device management (MDM) messages apply to device management scenarios to deliver management instructions to devices. MDM apps can still receive MDM messages while running in the background. This allows device administrators or enterprise IT departments to promptly deliver or receive MDM messages carrying instructions, such as device exception alerts, security policy updates, or remote operation instructions, under any circumstances.

Constraints

The function of pushing MDM messages supports phones, tablets, and PCs/2-in-1 devices.

Applying for Required Permissions

You need to apply for the MDM message permission to push MDM messages to your users.

Frequency Control

During the debugging phase, the maximum number of test messages that can be pushed for each project every day is 1000. To send test messages, you need to set testMessage to true.

After formal release, the total number of messages that each app can push to a single device each day is subject to the device-based message frequency control policy. The message sending frequency is governed based on service scenarios and traffic. In cases where message sending is unnecessary or unreasonable, frequency limits will be applied.

Development Procedure

  1. Obtain a push token, which is used to push MDM messages.

  2. In the onCreate method of the existing UIAbility class in your project, call the receiveMessage method to receive MDM messages. The following sample code uses PushMessageAbility as an example. MDM messages and background messages share the same callback entry point, and the type parameter is set to 'BACKGROUND' for both message types. When the value of data.type is 'MDM', the message is an MDM message. Only UIAbility can be used to receive MDM messages.

    import { AbilityConstant, UIAbility, Want } from '@kit.AbilityKit';
    import { pushCommon, pushService } from '@kit.PushKit';
    import { hilog } from '@kit.PerformanceAnalysisKit';
    import { BusinessError } from '@kit.BasicServicesKit';
    // ...
    
    const DOMAIN = 0x0000;
    
    export default class PushMessageAbility extends UIAbility {
      onCreate(want: Want, launchParam: AbilityConstant.LaunchParam): void {
      // ...
        try {
          pushService.receiveMessage('BACKGROUND', this, (data: pushCommon.PushPayload) => {
            // Process the message.
            try {
              if (data.type === 'MDM') {
                hilog.info(DOMAIN, 'testTag', 'Receive MDM message : %{public}s', JSON.stringify(data));
                // Process the MDM message.
                // ...
              }
            } catch (e) {
              let errRes: BusinessError = e as BusinessError;
              hilog.error(DOMAIN, 'testTag', 'Failed to process data: %{public}d %{public}s', errRes.code, errRes.message);
            }
          });
        } catch (err) {
          let e: BusinessError = err as BusinessError;
          hilog.error(DOMAIN, 'testTag', 'Failed to get background message: %{public}d %{public}s', e.code, e.message);
        }
        // ...
      }
    }
  3. Set actions to action.ohos.push.listener in skills in the abilities section of the src/main/module.json5 file at the project level. (This action can be defined for only one ability. If the uris parameter also exists, it should be left empty.)

    {
      "name": "PushMessageAbility",
      "srcEntry": "./ets/abilities/PushMessageAbility.ets",
      "description": "$string:PushMessageAbility_desc",
      "icon": "$media:layered_image",
      "label": "$string:PushMessageAbility_label",
      "startWindowIcon": "$media:startIcon",
      "startWindowBackground": "$color:start_window_background",
      "launchType": "singleton",
      "exported": false,
      "skills": [
        // Keep the existing skill object unchanged.
        {
          "actions": [
            "com.app.action"
          ]
        },
        // Add an independent skill object and set the actions parameter.
        {
          "actions": [
            "action.ohos.push.listener"
          ]
        }
      ]
      // ...
    }
  4. Call the REST API provided by Push Kit on your app server to send an MDM message. For details, please refer to the function description for the API used to push scenario-specific messages. The following is a request example:

    // Request URL
    POST "https://push-api.cloud.huawei.com/v3/[projectId]/messages:send"
    
    // Request header
    Content-Type: application/json
    Authorization: Bearer eyJr*****OiIx---****.eyJh*****iJodHR--***.QRod*****4Gp---****
    push-type: 11
    
    // Request body
    {
      "payload": {
        "data": "Carried MDM instruction data"
      },
      "target": {
        "token": ["MAMzLg**********lPW"]
      }
    }
    • [projectId]: project ID. To obtain the value, sign in to AppGallery Connect, click Development and services, click the desired project, and go to Project settings.
    • Authorization: a character string in JWT format. Obtain the value by referring to Authorization.
    • push-type: The value 11 indicates the MDM message type.
    • token: push token. Obtain the value by referring to Obtaining a Push Token.
    • data: MDM instruction data carried, which is a character string. For details, please refer to data.

    After the message is delivered, the app is directly launched to transfer message data. You can call the receiveMessage() method to obtain message data.