Example 3: Using Mobile Push

Scenarios

This section describes how to use mobile push by calling APIs. For details, see Calling APIs.

Prerequisites

  • For HMS, you have obtained the app ID and app secret.
  • For APNS, you have obtained a .p12 iOS push certificate and its password.

Procedure

  1. Obtain the token by following instructions in Authentication.
  2. Create a platform application.
    1. Send request POST https://{SMN_Endpoint}/v2/{project_id}/notifications/applications.
    2. Add X-Auth-Token to the request header.
    3. Specify the following parameters in the request body:
      For details about the parameters, see Creating a Topic.
      {     
          "name": "application_name",      
          "platform": "HMS",      
          "platform_principal": "appId",      
          "platform_credential": "appSecret" 
      }
      Example response
      {     
          "request_id": "6a63a18b8bab40ffb71ebd9cb80d0085",      
          "application_urn": "urn:smn:regionId:f96188c7ccaf4ffba0c9aa149ab2bd57:app-HMS-application_name",
          "application_id": "4f9ad22e1c244034995a77760252553c"
      }
  1. Add an application endpoint.
    1. Send request POST https://{SMN_Endpoint}/v2/{project_id}/notifications/applications/{application_urn}/endpoints.

      Set application_urn based on the response in 2.c.

    1. Add X-Auth-Token to the request header.
    2. Specify the following parameters in the request body:
      {
          "token": "37082*******383445",
          "user_data": "add endpoint"
      }
      Example response
      {     
          "request_id": "6a63a18b8bab40ffb71ebd9cb80d0085",      
          "endpoint_urn": "urn:smn:regionId:f96188c7ccaf4ffba0c9aa149ab2bd57:endpoint-APNS-example_application_name-37a2cb947fa43a20bc66e91281194e7a" 
      }

You can add multiple endpoints for an application, and modify the endpoint information. For details, see Updating Application Endpoint Attributes.

  1. Publish an application message.
    1. Send request POST https://{SMN_Endpoint}/v2/{project_id}/notifications/topics/{topic_urn}/publish.
    2. Add X-Auth-Token to the request header.
    3. Specify the following parameters in the request body:
      {
          "message":{
               "hps":{
                     "msg":{
                           "action":{ 
                                 "param":{
                                      "intent": "#Intent;compo=com.HC.SmnAppPush.defaultAction/.Activity;S.W=U;end"
                                 },
                                 "type": 1
                           },
                           "body": {
                                 "title": "hello world",
                                 "content": "hello world"
                           },
                           "type": 3
                      }
               }
          }
      }

      Example response

      {    
          "request_id": "6a63a18b8bab40ffb71ebd9cb80d0085",   
          "message_id": "992bbd32819448af95f83814cb3c7454"  
      }

      Ensure that the token in 4.c is correct. Otherwise, you may fail to receive mobile app messages.