Java Jpush集成记录,测试log4j日志打印

亲测可以正常推送到手机端
环境:eclipse 4.10
1.添加maven依赖到pom

<dependency>
			<groupId>cn.jpush.api</groupId>
			<artifactId>jpush-client</artifactId>
			<version>3.3.10</version>
		</dependency>
		<dependency>
			<groupId>cn.jpush.api</groupId>
			<artifactId>jiguang-common</artifactId>
			<version>1.1.4</version>
		</dependency>
		<dependency>
			<groupId>io.netty</groupId>
			<artifactId>netty-all</artifactId>
			<version>4.1.6.Final</version>
			<scope>compile</scope>
		</dependency>
		<dependency>
			<groupId>org.slf4j</groupId>
			<artifactId>slf4j-api</artifactId>
			<version>1.7.7</version>
		</dependency>
		<!-- For log4j -->
		<dependency>
			<groupId>log4j</groupId>
			<artifactId>log4j</artifactId>
			<version>1.2.17</version>
		</dependency>

2.在接口中使用jpush推送消息

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.example.demo.bean.UserInfo;
import com.example.demo.service.MyService;
import com.example.demo.websocket.MyWebSocket;
import com.google.gson.Gson;

import cn.jiguang.common.ClientConfig;
import cn.jiguang.common.resp.APIConnectionException;
import cn.jiguang.common.resp.APIRequestException;
import cn.jpush.api.JPushClient;
import cn.jpush.api.push.PushResult;
import cn.jpush.api.push.model.Message;
import cn.jpush.api.push.model.Platform;
import cn.jpush.api.push.model.PushPayload;
import cn.jpush.api.push.model.audience.Audience;
import cn.jpush.api.push.model.notification.Notification;

@Controller
@RequestMapping(value = "/test")
public class MyController {

	@Autowired
	private MyService Service;

	@ResponseBody
	@RequestMapping(value = "/hello", method = RequestMethod.GET)
	public String test() {
		Gson gson = new Gson();
		pushJGMsg();// 推送消息
		//测试使用log4j打印日志
		Logger log = Logger.getLogger(this.getClass());
		log.info("测试日志");
		Map<String, Object> map = new HashMap<>();
		map.put("result", "Hello");
		return gson.toJson(map);
	}

	private String MASTER_SECRET = "01f6d7cf6d6a6624f48c54bf";
	private String APP_KEY = "caebb9bccfddf8633e205e48";

	/**
	 * 推送示例
	 */
	public void pushJGMsg() {
		JPushClient jpushClient = new JPushClient(MASTER_SECRET, APP_KEY, null, ClientConfig.getInstance());
		try {
			PushPayload payload = buildPushObject_all_alias_alert();
			PushResult result = jpushClient.sendPush(payload);
			System.out.println("Got result - " + result);
		} catch (APIConnectionException e) {
			System.out.println("APIConnectionException error,:" + e.getMessage());
		} catch (APIRequestException e) {
			System.out.println("APIRequestException error," + e.getMessage());
		}
	}

	String ALERT = "你好J";
	String ALERT1 = "你好J1";

	public PushPayload buildPushObject_all_all_alert() {
		return PushPayload.alertAll(ALERT);
	}

	/**
	 * 构建推送 builder
	 * @return
	 */
	public PushPayload buildPushObject_all_alias_alert() {
		List<String> users = new ArrayList<>();
		users.add("13541354186");
		Map<String, String> extras = new HashMap<>();
		return PushPayload.newBuilder().setPlatform(Platform.all()).setAudience(Audience.alias(users))// 收到通知的用户别名
				.setMessage(Message.content(ALERT))// 消息体
				.setNotification(Notification.alert(ALERT1))// 显示在通知里面
				.build();
	}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值