百度云推送

阅读更多
项目需求:1.对所有用户广播消息。
                 2.对新版用户推送消息,对所有新版用户添加一个相同TagName。
工具:百度云推送
实现结果:在Android和IOS上都可用,且IOS实现脚标功能。
import net.sf.json.JSONObject;
import com.baidu.yun.channel.auth.ChannelKeyPair;
import com.baidu.yun.channel.client.BaiduChannelClient;
import com.baidu.yun.channel.exception.ChannelClientException;
import com.baidu.yun.channel.exception.ChannelServerException;
import com.baidu.yun.channel.model.PushBroadcastMessageRequest;
import com.baidu.yun.channel.model.PushBroadcastMessageResponse;
import com.baidu.yun.channel.model.PushTagMessageRequest;
import com.baidu.yun.channel.model.PushTagMessageResponse;
import com.baidu.yun.core.log.YunLogEvent;
import com.baidu.yun.core.log.YunLogHandler;
import com.baidu.yun.core.utility.StringUtility;               
public class MessageUtil {
public static void pushNewsInfoBroadcastNotification(){
  JSONObject androidJson = new JSONObject();
//显示内容,相应的格式可以手机端自己定义
  androidJson.put("name", "XX");  
  JSONObject customContent = new JSONObject();
//进入具体内容需要的信息
  customContent.put("id", "");
  androidJson.put("custom_content", customContent.toString());
  androidPushBroadcastNotification(androidJson.toString());
//ios推送  alert对应的是显示内容,sound对应的是推送声音,badge是脚标数
  String iosJson = String.format("{\"aps\":{\"alert\":\"%s\",\"sound\":\"%s\",\"badge\":%s},\"id\":\"%s\"}",
    "XX ",ConfigConstants.PUSH_SOUND,1, "");
  iosPushBroadcastNotification(iosJson);
}
private static void androidPushBroadcastNotification(String jsonMessage) {
  ChannelKeyPair pair = new ChannelKeyPair(ConfigConstants.API_KEY, ConfigConstants.SECRET_KEY); 
  BaiduChannelClient channelClient = new BaiduChannelClient(pair); 
  channelClient.setChannelLogHandler(new YunLogHandler() {
   @Override
   public void onHandle(YunLogEvent event) {
    System.out.println(event.getMessage());
   }
});
  try {
   PushBroadcastMessageRequest request = new PushBroadcastMessageRequest();
   request.setDeviceType(3); // device_type => 1: web 2: pc 3:android 4:ios 5:wp
   request.setDeployStatus(2); // DeployStatus => 1: Developer 2: Production
   request.setMessageType(1);  //1表示通知类型
   request.setMessage(jsonMessage);
  
   PushBroadcastMessageResponse response = channelClient.pushBroadcastMessage(request);
  
   System.out.println("push amount : " + response.getSuccessAmount());
  
  } catch (ChannelClientException e) {
   e.printStackTrace();
  } catch (ChannelServerException e) {
   System.out.println(
     String.format("request_id: %d, error_code: %d, error_message: %s" ,
      e.getRequestId(), e.getErrorCode(), e.getErrorMsg()));
  }
}

private static void iosPushBroadcastNotification(String jsonMessage){
  ChannelKeyPair pair = new ChannelKeyPair(ConfigConstants.API_KEY, ConfigConstants.SECRET_KEY);  
  BaiduChannelClient channelClient = new BaiduChannelClient(pair);  
  channelClient.setChannelLogHandler(new YunLogHandler() {
   @Override
   public void onHandle(YunLogEvent event) {
    System.out.println(event.getMessage());
   }
  });  
  try {   
   PushBroadcastMessageRequest request = new PushBroadcastMessageRequest();
   request.setDeviceType(4); // device_type => 1: web 2: pc 3:android 4:ios 5:wp
   request.setMessageType(1);
   request.setDeployStatus(2); // DeployStatus => 1: Developer 2: Production
   request.setMessage(jsonMessage);
   PushBroadcastMessageResponse response = channelClient.pushBroadcastMessage(request);
   System.out.println("push amount : " + response.getSuccessAmount());
  } catch (ChannelClientException e) {
   e.printStackTrace();
  } catch (ChannelServerException e) {
   System.out.println(
     String.format("request_id: %d, error_code: %d, error_message: %s" ,
      e.getRequestId(), e.getErrorCode(), e.getErrorMsg()));
  }
     }


//如果是添加tag,则调用下面的方法

private static void  PushNotification(String jsonMessage) {
  ChannelKeyPair pair = new ChannelKeyPair(ConfigConstants.API_KEY, ConfigConstants.SECRET_KEY);
  BaiduChannelClient channelClient = new BaiduChannelClient(pair);
  channelClient.setChannelLogHandler(new YunLogHandler() {
   @Override
   public void onHandle(YunLogEvent event) {
    System.out.println(event.getMessage());
   }
  });
  try {
   PushTagMessageRequest tagRequest = new PushTagMessageRequest();
   tagRequest.setDeviceType(3);  // device_type => 1: web 2: pc 3:android 4:ios 5:wp
   tagRequest.setDeployStatus(2);   // DeployStatus => 1: Developer 2: Production
   tagRequest.setMessageType(1);
   tagRequest.setTagName("XX");  //前端需要设置相应的tag
   tagRequest.setMessage(jsonMessage);
   PushTagMessageResponse response = channelClient.pushTagMessage(tagRequest);
   System.out.println("push amount : " + response.getSuccessAmount());
  } catch (ChannelClientException e) {
   e.printStackTrace();
  } catch (ChannelServerException e) {
   System.out.println(
     String.format("request_id: %d, error_code: %d, error_message: %s" ,
      e.getRequestId(), e.getErrorCode(), e.getErrorMsg()));
  }
}

}

你可能感兴趣的:(百度云推送,android,ios,web)