tencent cloud

Feedback

API (Java)

Last updated: 2024-01-17 14:26:44

    SDK Description

    This SDK is used to encapsulate the Tencent Push Notification Service server APIs and communicate with the Tencent Push Notification Service server. To use it, you only need to import the XingeApp package. This SDK mainly encapsulates push-related V3 APIs.

    Integration Methods

    Import Maven dependencies:
    <dependency>
    <groupId>io.github.tpnsPush</groupId>
    <artifactId>xinge</artifactId>
    <version>1.2.4.11</version>
    </dependency>
    Note:
    There are groupId changes since version 1.2.4.11.

    Usage

    XingeApp API description

    This class provides APIs for interaction with the Tencent Push Notification Service server, which is constructed by XingeApp.Builder with the following parameters:
    Parameter
    Type
    Required
    Default Value
    Description
    appId
    String
    Yes
    Empty
    Push target AccessID, which can be obtained on the Product Management page in the console.
    secretKey
    String
    Yes
    Empty
    Push key, which can be obtained on the Product Management > Configuration Management > Basic Configuration page.
    proxy
    Proxy
    No
    Proxy.NO_PROXY
    This parameter can be set if a proxy needs to be configured.
    connectTimeOut
    Integer
    No
    10s
    Connection timeout period.
    readTimeOut
    Integer
    No
    10s
    Request timeout period.
    domainUrl
    String
    No
    https://openapi.xg.qq.com/
    API request service domain name address, which is the XG API address by default. Change the value to the service URL corresponding to your service access point.

    Sample

    XingeApp xingeApp = new XingeApp.Builder()
    .appId(appid)
    .secretKey(secretKey)
    .domainUrl("https://api.tpns.tencent.com/")
    .build();
    
    PushAppRequest pushAppRequest = new PushAppRequest();
    // Complete the `PushAppRequest` message
    ...
    JSONObject ret = xingeApp.pushApp(pushAppRequest );

    pushAppRequest API description

    This class provides an encapsulated push message body. For the description and usage of parameters, please see Push API.

    Samples

    Note:
    The XingeAppSimple class provides API examples related to message push, account binding and unbinding, etc.

    Push to a single Android device

    public JSONObject pushTokenAndroid() {
    PushAppRequest pushAppRequest = new PushAppRequest();
    pushAppRequest.setAudience_type(AudienceType.token);
    pushAppRequest.setMessage_type(MessageType.notify);
    Message message = new Message();
    message.setTitle("title");
    message.setContent("content");
    pushAppRequest.setMessage(message);
    MessageAndroid messageAndroid = new MessageAndroid();
    message.setAndroid(messageAndroid);
    ArrayList<String> tokenList = new ArrayList();
    tokenList.add("04cac74a714f61bf089********63d880993");
    pushAppRequest.setToken_list(tokenList);
    return this.xingeApp.pushApp(pushAppRequest);
    }

    Push to a single Android account

    public JSONObject pushAccountAndroid() {
    PushAppRequest pushAppRequest = new PushAppRequest();
    pushAppRequest.setAudience_type(AudienceType.account);
    pushAppRequest.setPlatform(Platform.android);
    pushAppRequest.setMessage_type(MessageType.notify);
    pushAppRequest.setAccount_push_type(1);
    Message message = new Message();
    message.setTitle("title");
    message.setContent("content");
    MessageAndroid messageAndroid = new MessageAndroid();
    message.setAndroid(messageAndroid);
    pushAppRequest.setMessage(message);
    ArrayList<String> accountList = new ArrayList();
    accountList.add("123");
    pushAppRequest.setAccount_list(accountList);
    return this.xingeApp.pushApp(pushAppRequest);
    }

    Push to Android devices with specified tags

    public JSONObject pushTagAndroid() {
    PushAppRequest pushAppRequest = new PushAppRequest();
    pushAppRequest.setAudience_type(AudienceType.tag);
    pushAppRequest.setPlatform(Platform.android);
    pushAppRequest.setMessage_type(MessageType.notify);
    Message message = new Message();
    message.setTitle("title");
    message.setContent("content");
    MessageAndroid messageAndroid = new MessageAndroid();
    message.setAndroid(messageAndroid);
    pushAppRequest.setMessage(message);
    ArrayList<String> tagList = new ArrayList();
    tagList.add("tag");
    TagListObject tagListObject = new TagListObject();
    tagListObject.setTags(tagList);
    tagListObject.setOp(OpType.OR);
    pushAppRequest.setTag_list(tagListObject);
    return this.xingeApp.pushApp(pushAppRequest);
    }

    Push to all Android devices

    public JSONObject pushAllAndroid() {
    PushAppRequest pushAppRequest = new PushAppRequest();
    pushAppRequest.setAudience_type(AudienceType.all);
    pushAppRequest.setPlatform(Platform.android);
    pushAppRequest.setMessage_type(MessageType.notify);
    Message message = new Message();
    message.setTitle("title");
    message.setContent("content");
    MessageAndroid messageAndroid = new MessageAndroid();
    message.setAndroid(messageAndroid);
    pushAppRequest.setMessage(message);
    return this.xingeApp.pushApp(pushAppRequest);
    }

    Push to a single iOS device

    public JSONObject pushTokenIos(){
    PushAppRequest pushAppRequest = new PushAppRequest();
    pushAppRequest.setAudience_type(AudienceType.token);
    pushAppRequest.setEnvironment(Environment.valueOf("dev"));
    pushAppRequest.setMessage_type(MessageType.notify);
    Message message = new Message();
    message.setTitle("title");
    message.setContent("content");
    MessageIOS messageIOS = new MessageIOS();
    Alert alert = new Alert();
    Aps aps = new Aps();
    aps.setAlert(alert);
    messageIOS.setAps(aps);
    message.setIos(messageIOS);
    pushAppRequest.setMessage(message);
    ArrayList<String> tokenList = new ArrayList<String>();
    tokenList.add("0250df875c93c55********536b54fc1c49f");
    pushAppRequest.setToken_list(tokenList);
    return this.xingeApp.pushApp(pushAppRequest);
    }
    

    Push to a single iOS account

    public JSONObject pushAccountIos() {
    PushAppRequest pushAppRequest = new PushAppRequest();
    pushAppRequest.setAudience_type(AudienceType.account);
    pushAppRequest.setEnvironment(Environment.valueOf("dev"));
    pushAppRequest.setMessage_type(MessageType.notify);
    Message message = new Message();
    message.setTitle("Account push");
    message.setContent("content");
    MessageIOS messageIOS = new MessageIOS();
    Alert alert = new Alert();
    Aps aps = new Aps();
    aps.setAlert(alert);
    messageIOS.setAps(aps);
    message.setIos(messageIOS);
    pushAppRequest.setMessage(message);
    ArrayList<String> accountList = new ArrayList();
    accountList.add("1122");
    pushAppRequest.setAccount_list(accountList);
    return this.xingeApp.pushApp(pushAppRequest);
    }
    

    Push to iOS devices with specified tags

    public JSONObject pushTagIos() {
    PushAppRequest pushAppRequest = new PushAppRequest();
    pushAppRequest.setAudience_type(AudienceType.tag);
    pushAppRequest.setEnvironment(Environment.valueOf("dev"));
    pushAppRequest.setMessage_type(MessageType.notify);
    Message message = new Message();
    message.setTitle("Tag push");
    message.setContent("content");
    MessageIOS messageIOS = new MessageIOS();
    Alert alert = new Alert();
    Aps aps = new Aps();
    aps.setAlert(alert);
    messageIOS.setAps(aps);
    message.setIos(messageIOS);
    pushAppRequest.setMessage(message);
    ArrayList<String> tagList = new ArrayList();
    tagList.add("1122");
    TagListObject tagListObject = new TagListObject();
    tagListObject.setTags(tagList);
    tagListObject.setOp(OpType.OR);
    pushAppRequest.setMessage(message);
    pushAppRequest.setTag_list(tagListObject);
    return this.xingeApp.pushApp(pushAppRequest);
    }
    

    Push to all iOS devices

    public JSONObject pushAllIos() {
    PushAppRequest pushAppRequest = new PushAppRequest();
    pushAppRequest.setAudience_type(AudienceType.all);
    pushAppRequest.setEnvironment(Environment.valueOf("dev"));
    pushAppRequest.setMessage_type(MessageType.notify);
    Message message = new Message();
    message.setTitle("Full push");
    message.setContent("content");
    MessageIOS messageIOS = new MessageIOS();
    Alert alert = new Alert();
    Aps aps = new Aps();
    aps.setAlert(alert);
    messageIOS.setAps(aps);
    message.setIos(messageIOS);
    pushAppRequest.setMessage(message);
    return this.xingeApp.pushApp(pushAppRequest);
    }
    
    

    Push response sample

    {"result":"{}","environment":"","push_id":"1328245138690125824","err_msg":"NO_ERROR","err_msg_zh":"","ret_code":0,"seq":0}

    Server Return Codes

    For the meaning of ret_code, please see Server-Side Error Codes.

    FAQs

    What should I do if the error code 10101 or 403 is returned?

    Check whether the application AccessID matches with SecretKey, and whether domainUrl corresponds to the service access point of the application.

    What should I do if the API returns the error code 1008007 that indicates invalid parameters?

    Refer to Push API and enter all the parameters correctly.

    Are there SDKs in other programming languages?

    You can find more server SDKs in other programming languages on the SDK Download page.

    What should I do if the push API returns Peer certificate cannot be authenticated with given CA certificates?

    This is because the CA certificate has expired. Enter the certificate directory and run the openssl command to view the expiration time:
    # openssl x509 -in signed.crt -noout -dates
    Replace signed.crt with the name of the certificate on your server.
    Contact Us

    Contact our sales team or business advisors to help your business.

    Technical Support

    Open a ticket if you're looking for further assistance. Our Ticket is 7x24 avaliable.

    7x24 Phone Support