记一次后台集成Jpush 极光推送代码

本项目为meaven项目。

第一步:pom文件添加依赖

记一次后台集成Jpush 极光推送代码_第1张图片`

    
        cn.jpush.api
        jpush-client
        3.3.12
    
    
          cn.jpush.api
          jiguang-common
          1.1.6
      
      
          io.netty
          netty-all
          4.1.6.Final
          compile
      
      
          com.google.code.gson
          gson
          2.3
      
      
          org.slf4j
          slf4j-api
          1.7.7
      

`

第二部:build项目 下载依赖包。编写推送代码:

main方法测试 :

package com.lantong.ltapp.web;


import java.util.HashMap;

import cn.jiguang.common.ClientConfig;
import cn.jpush.api.JPushClient;
import cn.jpush.api.push.PushResult;
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.AndroidNotification;
import cn.jpush.api.push.model.notification.IosNotification;
import cn.jpush.api.push.model.notification.Notification;

public class JPushUtil {
	
    private static String masterSecret = "9b1065283b4f9937d8ad085e";
    private static String appKey = "300e32a95337c2f47684843b";
	
//	private static String masterSecret = "8af1c248c189b855ab2ee6f7";
//  private static String appKey = "eefd509a63182ea306dfc5c4";
	
	
	// {"账号"  "告警信息"}      单推
	public static void main(String[] args) {
	    try {
	    	
			JPushClient jpushClient = new JPushClient(masterSecret, 
					appKey, null, ClientConfig.getInstance());
			PushResult result = jpushClient.sendPush(pushTag(new JPushMessageObj(),"15803997083"));
			
			//注意让他睡一会   记得关闭  否则进程不会退出
	        Thread.sleep(5000);
	        jpushClient.close();
	        
	        System.out.println("------" + result); // {"msg_id":54043208147710502,"sendno":672317924,"statusCode":0}
	        
	    } catch (Exception e) {
			e.printStackTrace();
			System.out.println("----Exception----"  + e.toString() );
		}
	}

	static	HashMap extras = new HashMap<>();

	/**
	 * 根据    账号推送信息:
	 * @param obj  需要推送的内容对象
	 * phoneNumTag 手机号码标记用户    用于实现单推
	 * @return
	 */
	@SuppressWarnings("static-access")
	private static PushPayload pushTag(JPushMessageObj message,String phoneNumTag) {
		
		//需要添加的参数  可以在此添加
		extras.put("id",message.id);
		extras.put("station",message.station);
		extras.put("alarmContent",message.alarmContent);
		extras.put("alarmDate",message.alarmDate);
		extras.put("alarmLevel",message.alarmLevel);
		extras.put("alarmType",message.alarmType);
		
		return  PushPayload.newBuilder()
				.setPlatform(Platform.all())
				.setAudience(Audience.all())  //推送给特定用户 tag标记   更改为 phoneNumTag
				
				//推送通知消息:
				.setNotification(
						Notification.newBuilder()
								  .addPlatformNotification
								  (
										  AndroidNotification.newBuilder()
			                              .setAlert(message.alarmContent)
			                              .setTitle(message.station)
			                              .addExtras(extras)
			                              .build()
			                       )
								  .addPlatformNotification
								  (
										  IosNotification.newBuilder()
			                              .setAlert(message.alarmContent)
			                              .addExtras(extras)
			                              .build()
	                              ).build()
						)
//						//推送应用内    消息 :  推送消息 但不通知:
//						.setMessage(Message.newBuilder()
//								.setMsgContent("") // 应用内消息
//								.addExtra("id", 1)//传递参数
//								.addExtra("station", "河南蓝通,收到请回话")//传递参数
//								.addExtra("alarmContent", 1)//传递参数
//								.addExtra("alarmDate", true)//传递参数
//								.addExtra("alarmLevel", 5)//传递参数
//								.addExtra("alarmType", 1)//传递参数
//								.build())
				
				.build();
	}
	
	
	 /**
	  * 消息对象
	  * author:zhaoq
	  * github:https://github.com/zqHero
	  * csdn:http://blog.csdn.net/u013233097
	  * 2019年6月5日
	  */
	 private static class JPushMessageObj{
		 static String id =  "1200981223";
		 static String station = "上海华容电站";
		 static String alarmContent = "上海华容电站用电量过大五级警告";
		 static String alarmDate = "20190629";
		 static String alarmLevel = "5";
		 static String alarmType = "1";
	}
}

官方文档:
https://docs.jiguang.cn//jpush/server/sdk/java_sdk/

你可能感兴趣的:(jpush)