コード例 #1
0
  public static void main(String[] args) throws PushClientException, PushServerException {
    // 1. get apiKey and secretKey from developer console
    String apiKey = "xxxxxxxxxxxxxxxxxxxx";
    String secretKey = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
    PushKeyPair pair = new PushKeyPair(apiKey, secretKey);

    // 2. build a BaidupushClient object to access released interfaces
    BaiduPushClient pushClient = new BaiduPushClient(pair, BaiduPushConstants.CHANNEL_REST_URL);

    // 3. register a YunLogHandler to get detail interacting information
    // in this request.
    pushClient.setChannelLogHandler(
        new CloudLogHandler() {
          @Override
          public void onHandle(CloudLogEvent event) {
            System.out.println(event.getMessage());
          }
        });

    try {
      // 4. specify request arguments
      // pushTagTpye = 1 for common tag pushing
      PushMsgToTagRequest request =
          new PushMsgToTagRequest()
              .addTagName("xxxxx")
              .addMsgExpires(new Integer(3600))
              .addMessageType(0) // 添加透传消息
              // .addSendTime(System.currentTimeMillis() / 1000 + 120) //设置定时任务
              .addMessage("Hello Baidu push")
              .addDeviceType(3);
      // 5. http request
      PushMsgToTagResponse response = pushClient.pushMsgToTag(request);
      // Http请求结果解析打印
      System.out.println(
          "msgId: "
              + response.getMsgId()
              + ",sendTime: "
              + response.getSendTime()
              + ",timerId: "
              + response.getTimerId());
    } catch (PushClientException e) {
      if (BaiduPushConstants.ERROROPTTYPE) {
        throw e;
      } else {
        e.printStackTrace();
      }
    } catch (PushServerException e) {
      if (BaiduPushConstants.ERROROPTTYPE) {
        throw e;
      } else {
        System.out.println(
            String.format(
                "requestId: %d, errorCode: %d, errorMessage: %s",
                e.getRequestId(), e.getErrorCode(), e.getErrorMsg()));
      }
    }
  }
コード例 #2
0
  @Test
  @Category(com.baidu.llv23.test.category.LocalCloudPushInterface.class)
  public void testPushBroadcastMessage()
      throws PushClientException, PushServerException, FileNotFoundException, IOException {
    // see: pre-requisite. ApiKey/SecretKey on developer platform
    Properties defaultProps = new Properties();
    try (FileInputStream in =
        new FileInputStream("src/main/resources/META-INF/application.properties")) {
      defaultProps.load(in);
    }

    // 1. get apiKey and secretKey from developer console
    String appidKey = defaultProps.getProperty("appidKey");
    if (appidKey.equals("${appidKey}")) {
      // TODO : not defined
      appidKey = System.getProperty("appidKey");
    }
    String secretKey = defaultProps.getProperty("secretKey");
    if (secretKey.equals("${secretKey}")) {
      // TODO : not defined
      secretKey = System.getProperty("secretKey");
    }

    PushKeyPair pair = new PushKeyPair(appidKey, secretKey);

    // 2. build a BaidupushClient object to access released interfaces
    BaiduPushClient pushClient = new BaiduPushClient(pair, BaiduPushConstants.CHANNEL_REST_URL);

    // 3. register a YunLogHandler to get detail interacting information in this request.
    pushClient.setChannelLogHandler(
        new CloudLogHandler() {
          @Override
          public void onHandle(CloudLogEvent event) {
            System.out.println(event.getMessage());
          }
        });

    try {
      // 4. specify request arguments
      // see : create iOS notification
      JSONObject notification = new JSONObject();
      JSONObject jsonAPS = new JSONObject();
      jsonAPS.put("alert", "Hello Baidu Push");
      jsonAPS.put("sound", "ttt"); // setup for ringing style, for example "ttt", user defined
      notification.put("aps", jsonAPS);
      notification.put("key1", "value1");
      notification.put("key2", "value2");

      PushMsgToAllRequest request =
          new PushMsgToAllRequest()
              .addMsgExpires(new Integer(3600))
              .addMessageType(1)
              .addMessage(notification.toString())
              .addSendTime(
                  System.currentTimeMillis() / 1000
                      + 70) // setup of delay time for sending, must more than 1 minute, unit in
              // second, here set for 70 seconds
              .addDepolyStatus(DeployStatus.development.getValue())
              .addDeviceType(4);
      // 5. http request
      PushMsgToAllResponse response = pushClient.pushMsgToAll(request);
      // Http response print out
      System.out.println(
          String.format(
              "msgId: %s ,sendTime: %d,timerId: %s",
              response.getMsgId(), response.getSendTime(), response.getTimerId()));
    } catch (PushClientException e) {
      if (BaiduPushConstants.ERROROPTTYPE) {
        throw e;
      } else {
        e.printStackTrace();
      }
    } catch (PushServerException e) {
      if (BaiduPushConstants.ERROROPTTYPE) {
        throw e;
      } else {
        System.err.println(
            String.format(
                "requestId: %d, errorCode: %d, errorMessage: %s",
                e.getRequestId(), e.getErrorCode(), e.getErrorMsg()));
      }
    }
  }