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();
	}
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章