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.
Involved APIs
Procedure
- Obtain the token by following instructions in Authentication.
- Create a platform application.
- Send request POST https://{SMN_Endpoint}/v2/{project_id}/notifications/applications.
- Add X-Auth-Token to the request header.
- 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" }
- Add an application endpoint.
- Send request POST https://{SMN_Endpoint}/v2/{project_id}/notifications/applications/{application_urn}/endpoints.
Set application_urn based on the response in 2.c.
- Add X-Auth-Token to the request header.
- 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" }
- Send request POST https://{SMN_Endpoint}/v2/{project_id}/notifications/applications/{application_urn}/endpoints.
You can add multiple endpoints for an application, and modify the endpoint information. For details, see Updating Application Endpoint Attributes.
- Publish an application message.
- Send request POST https://{SMN_Endpoint}/v2/{project_id}/notifications/topics/{topic_urn}/publish.
- Add X-Auth-Token to the request header.
- 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" }
Last Article: Example 2: Publishing a Message Using a Message Template
Next Article: Permissions Policies and Supported Actions
Did this article solve your problem?
Thank you for your score!Your feedback would help us improve the website.