java使用环信信息推送,使用环信给App推送Push消息

该博客展示了如何使用Java通过环信(Easemob)平台的REST API向App用户发送Push消息。代码示例包括注册IM用户、获取用户状态以及发送文本、图片、语音和透传消息的步骤。
摘要由CSDN通过智能技术生成

import java.net.URL;

import org.apache.commons.lang3.StringUtils;

import org.slf4j.Logger;

import org.slf4j.LoggerFactory;

import com.fasterxml.jackson.databind.JsonNode;

import com.fasterxml.jackson.databind.node.ArrayNode;

import com.fasterxml.jackson.databind.node.JsonNodeFactory;

import com.fasterxml.jackson.databind.node.ObjectNode;

import com.jx.lcs.common.huanxin.Constants;

import com.jx.lcs.common.huanxin.HTTPMethod;

import com.jx.lcs.common.huanxin.Roles;

import com.jx.lcs.common.huanxin.httpclient.HTTPClientUtils;

import com.jx.lcs.common.huanxin.vo.ClientSecretCredential;

import com.jx.lcs.common.huanxin.vo.Credential;

import com.jx.lcs.common.huanxin.vo.EndPoints;

/**

* REST API Demo: 发送消息 REST API HttpClient4.3实现

*/

public class EasemobMessages {

private static Logger LOGGER = LoggerFactory.getLogger(EasemobMessages.class);

private static final String APPKEY = Constants.APPKEY;

private static JsonNodeFactory factory = new JsonNodeFactory(false);

// 通过app的client_id和client_secret来获取app管理员token

private static Credential credential = new ClientSecretCredential(Constants.APP_CLIENT_ID,

Constants.APP_CLIENT_SECRET, Roles.USER_ROLE_APPADMIN);

//发送消息到单个用户

public static void sendMsgToUser(String fromUid, String toUid, String content) {

// 给用户发一条文本消息

String from = fromUid;

String targetTypeus = "users";

ObjectNode ext = factory.objectNode();

ArrayNode targetusers = factory.arrayNode();

targetusers.add(toUid);

ObjectNode txtmsg = factory.objectNode();

txtmsg.put("msg", content);

txtmsg.put("type","txt");

ObjectNode sendTxtMessageusernode = sendMessages(targetTypeus, targetusers, txtmsg, from, ext);

if (null != sendTxtMessageusernode) {

LOGGER.info("给" + toUid + "发一条文本消息: " + sendTxtMessageusernode.toString());

}

}

public static void main(String[] args) {

/**

* 注册IM用户[单个]

*/

ObjectNode datanode = JsonNodeFactory.instance.objectNode();

datanode.put("username","test");

datanode.put("password", Constants.DEFAULT_PASSWORD);

ObjectNode createNewIMUserSingleNode = createNewIMUserSingle(datanode);

if (null != createNewIMUserSingleNode) {

LOGGER.info("注册IM用户[单个]: " + createNewIMUserSingleNode.toString());

}

// 检测用户是否在线

// String targetuserPrimaryKey = "683697";

// ObjectNode usernode = getUserStatus(targetuserPrimaryKey);

// if (null != usernode) {

// LOGGER.info("检测用户是否在线: " + usernode.toString());

// }

// 给用户发一条文本消息

/*

String from =

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值