Push message transmitted using codes Aurora

Copyright: Finishing easy, please indicate the source. https://blog.csdn.net/linmengmeng_1314/article/details/90778134

1. Obtain AppKey and Master Secret

First, there should be a push Aurora's official website account, then add your application, which is used below in order to get AppKey and Master Secret these two parameters, easy to use below. Adding applications can refer to the document's official website, where major record your own call SMS verification code interfaces pit encountered.
Here Insert Picture Description
Here Insert Picture Description

2. Set SMS templates and SMS signature

After the first step to get the two parameters, then the verification code text message settings, mouse on the left side of the screen, you can see the aurora SMS.
Here Insert Picture Description
Aurora SMS page, you can see an overview of the data, where you can display the remaining number of short messages out of the account, which is the third icon industry margin
Here Insert Picture Description
first thought first SMS text message code verification is the margin of it, only to discover later No, the balance of trade which also includes SMS verification service.

Click on the left of the signature management, signing settings before you can not set the signature push Aurora, Aurora push default signature, which is the most signature verification code SMS message content in brackets, is the signature.
For example: 【天眼查】你好, 你的验证码是:2047,有效时间3分钟Here is the text of the eye in the sky to check the signature.
Here Insert Picture Description
I can see there has been a signature, and if there is no signature, click on the upper right corner to create a signature, you can set your own SMS signature. This is a must have, because now Aurora push the default signature is not provided, the need to set up their own first signature created is the default signature.

Then set the SMS template, fill specific requirements, you can see the message page, this point is very detailed.

3. Start achieve server interface

First posted here about the Aurora document, if you look at the foggy, then, and then look down, you might have to help, if you just write out in accordance with the document, do not waste time reading this blog up.

Recommendations or personal use Aurora push SDK, so relatively easy to avoid unnecessary trouble, I just started looking at the document to write their own, the results of various pit, using the SDK cool moment, has been using has been cool.

First, import-dependent: https://docs.jiguang.cn/jsms/server/sdk/java_sdk/ specific facie this document, which can be found in the value of LATEST_VERSION release page acquisition.

Currently I use the version

		<dependency>
			<groupId>cn.jpush.api</groupId>
			<artifactId>jsms-client</artifactId>
			<version>1.2.9</version>
		</dependency>

Direct copy of the above documentation may also be dependent, but if the project is imported into the log4j jar package, and then import slf4j package, the console will get an error, both of which leave a can.

Here it is necessary to look at demo posted on Aurora push github, just started really did not see how clear it is, according to the document has been focusing instead on writing, then all kinds of problems. . . . .
https://github.com/jpush/jsms-api-java-client/blob/master/example/main/java/cn/jsms/api/JSMSExample.java#L33

Aurora offers two ways to push verification codes, one is generated directly by the Aurora platform random verification code, the server can just pass the phone number and then call the SMS verification code verification interface to validate user input verification code is correct this method does not require the server generates a random verification code, and verify the validity of the code of Aurora platform is done, the server only needs to call the relevant interface.

Another is to set SMS templates, the server generates its own random verification code, Aurora platform is responsible for the user to send SMS verification code SMS, verify the correctness of code required to verify their own server.

Here we use the second approach, that is, the use of SMS templates, server-side validation to verify the validity and correctness of their own code.

Use templates to send a single text message: Use this method testSendTemplateSMS()

	public static void testSendTemplateSMS() {
	    SMSClient client = new SMSClient(masterSecret, appkey);
        SMSPayload payload = SMSPayload.newBuilder()
                .setMobileNumber("13800138000")
                .setTempId(1)
                .addTempPara("code", "666666")
                .build();
        try {
            SendSMSResult res = client.sendTemplateSMS(payload);
            LOG.info(res.toString());
        } catch (APIRequestException e) {
            LOG.error("Error response from JPush server. Should review and fix it. ", e);
            LOG.info("HTTP Status: " + e.getStatus());
            LOG.info("Error Message: " + e.getMessage());
        } catch (APIConnectionException e) {
            LOG.error("Connection error. Should retry later. ", e);
        }
    }

This method returns a result {"msg_id":"773945179045"}

There is no set the signature, using the default signature above our own set, if there are multiple signatures account, you can SMSPayload.newBuilder () behind the use of .setSignId(signId)the specified signature.

Use bulk SMS template: Use testSendBatchTemplateSMS () method

    public static void testSendBatchTemplateSMS() {
        SMSClient client = new SMSClient(masterSecret, appkey);
        List<RecipientPayload> list = new ArrayList<RecipientPayload>();
        RecipientPayload recipientPayload1 = new RecipientPayload.Builder()
                .setMobile("13800138000")
                .addTempPara("code", "638938")
                .build();
        RecipientPayload recipientPayload2 = new RecipientPayload.Builder()
                .setMobile("13800138000")
                .addTempPara("code", "829302")
                .build();
        list.add(recipientPayload1);
        list.add(recipientPayload2);
        RecipientPayload[] recipientPayloads = new RecipientPayload[list.size()];
        BatchSMSPayload smsPayload = BatchSMSPayload.newBuilder()
                .setTempId(1)
                .setRecipients(list.toArray(recipientPayloads))
                .build();
        try {
            BatchSMSResult result = client.sendBatchTemplateSMS(smsPayload);
            LOG.info("Got result: " + result);
        } catch (APIConnectionException e) {
            LOG.error("Connection error. Should retry later. ", e);
        } catch (APIRequestException e) {
            LOG.error("Error response from JPush server. Should review and fix it. ", e);
            LOG.info("HTTP Status: " + e.getStatus());
            LOG.info("Error Message: " + e.getMessage());
        }
    }

When sending a number of messages, the result is returned {"success_count":1,"failure_count":0}

If the first manner using Aurora platform generates random codes and the authentication codes, then the effectiveness of using the method:

    public static void testSendSMSCode() {
    	SMSClient client = new SMSClient(masterSecret, appkey);
    	SMSPayload payload = SMSPayload.newBuilder()
				.setMobileNumber("13800138000")
				.setTempId(1)
				.build();
    	try {
			SendSMSResult res = client.sendSMSCode(payload);
            System.out.println(res.toString());
			LOG.info(res.toString());
		} catch (APIConnectionException e) {
            LOG.error("Connection error. Should retry later. ", e);
        } catch (APIRequestException e) {
            LOG.error("Error response from JPush server. Should review and fix it. ", e);
            LOG.info("HTTP Status: " + e.getStatus());
            LOG.info("Error Message: " + e.getMessage());
        }
    }

Or this:

    public static void testSendSMSWithIHttpClient() {
        SMSClient client = new SMSClient(masterSecret, appkey);
        String authCode = ServiceHelper.getBasicAuthorization(appkey, masterSecret);
        ApacheHttpClient httpClient = new ApacheHttpClient(authCode, null, ClientConfig.getInstance());
        // NettyHttpClient httpClient = new NettyHttpClient(authCode, null, ClientConfig.getInstance());
        // ApacheHttpClient httpClient = new ApacheHttpClient(authCode, null, ClientConfig.getInstance());
        // 可以切换 HttpClient,默认使用的是 NativeHttpClient
        client.setHttpClient(httpClient);
        // 如果使用 NettyHttpClient,发送完请求后要调用 close 方法
        // client.close();
        SMSPayload payload = SMSPayload.newBuilder()
                .setMobileNumber("13800138000")
                .setTempId(1)
                .build();
        try {
            SendSMSResult res = client.sendSMSCode(payload);
            System.out.println(res.toString());
            LOG.info(res.toString());
        } catch (APIConnectionException e) {
            LOG.error("Connection error. Should retry later. ", e);
        } catch (APIRequestException e) {
            LOG.error("Error response from JPush server. Should review and fix it. ", e);
            LOG.info("HTTP Status: " + e.getStatus());
            LOG.info("Error Message: " + e.getMessage());
        }
    }

The results are returned: { "msg_id": "773945179045 "}, which msg_idis our next call Aurora platform verification SMS verification code correctness and effectiveness of the use to.

Verification method to verify the validity of the code:

	public static void checkSms(String msgId, String code) {
		SMSClient client = new SMSClient(masterSecret, appkey);
		try {
			ValidSMSResult res = client.sendValidSMSCode(msgId,code);
			System.out.println(res.toString());
		} catch (APIConnectionException e) {
			e.printStackTrace();
		} catch (APIRequestException e) {
			e.printStackTrace();
		}
	}

Return result: { "is_valid": true}or{"is_valid":false,"error":{"code":50026,"message":"wrong msg_id"}}

So far, SMS verification code function is complete.

The following record about the pit I encountered:
the beginning is this error:

  1. Request is forbidden! Maybe your appkey is listed in blacklist or your params is invalid.
    Then I thought it was appkey blacklisted it, results in the Aurora community push to ask technical personnel and found that he did not set the signature, and Aurora push the new version does not provide a default signature.
  2. { "Error": { "code ": 50051, "message": "signatures not exist"}}
    The problem may be due to a problem that appkey I use the company account the following three applications, I changed a appkey after it Normal.
  3. If you use the api documentation to write, direct access using curl SMS verification code interface, it should be noted that the head generated in the Authorizationparameter, you need to pay attention to, which contains a parameter value Basic 和 空格, if this is not the case, verification is not passed.
  4. There is a problem is the parameters I use base64 encryption, my arguments and documents generated inside the parameters are inconsistent, I am also very puzzled.
    This document which is the result
    Here Insert Picture Description
    and I am using org.apache.commons.codec.binary.Base64.Base64 () encodeToString class method of this tool, the results of my generation is that this: NTQ0ZjI5MDY1YWZmZTA2MDIyMjdmMzk1OjIzYTM5NGM4MWQyMTg0NDA4YWE5ODhkNA ==, if big brother who know, gets advice is a good wow.

Guess you like

Origin blog.csdn.net/linmengmeng_1314/article/details/90778134
Recommended