springboot+APPcan集成极光推送,实现APP消息推送

实现过程:

           从APP端集成极光推送插件,启动时调用方法获取极光推送返回的registrationId,登录时传入后台保存在数据库里,后台服务集成极光的java版SDK封装自己常用的推送服务方法的服务类,即可实现消息推送。

 

一、APP后台服务springboot  部分的代码

  1. 修改POM文件,引入集成的极光推送java版SDK所需要的包

		
		
			cn.jpush.api
			jpush-client
			3.3.11
		
		
			cn.jpush.api
			jiguang-common
			1.1.5
		

		
			io.netty
			netty-all
			4.1.36.Final
			compile
		
		
			com.google.code.gson
			gson
		
		
			org.slf4j
			slf4j-api
		

		
			org.slf4j
			slf4j-log4j12
		
		
			log4j
			log4j
			1.2.17
		


		
			net.sf.json-lib
			json-lib
			2.4
			jdk15
		
		

2、在application.properties文件里,加上在极光官网注册申请的appKey和masterSecret

#极光推送
jpush.appKey=ed4b5b5b4a2a28dadc20bb**
jpush.masterSecret=49bc960f1801bdd69d07****
jpush.apnsProduction=false

3、JPushService.java

package com.sgitg.***.service;
 
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import cn.jiguang.common.resp.APIConnectionException;
import cn.jiguang.common.resp.APIRequestException;
import cn.jpush.api.JPushClient;
import cn.jpush.api.device.TagListResult;
import cn.jpush.api.push.PushResult;
import cn.jpush.api.push.model.Message;
import cn.jpush.api.push.model.Options;
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;

@Component
public class JPushService {
	/** 日志 */
	protected static final Logger logger = LoggerFactory.getLogger(JPushService.class);

	@Value("${jpush.appKey}")
	private String appKey;

	@Value("${jpush.masterSecret}")
	private String masterSecret;

	@Value("${jpush.apnsProduction}")
	private boolean apnsProduction;

	private static JPushClient jPushClient = null;
	private static final int RESPONSE_OK = 200;

	public JPushClient getJPushClient() {
		if (jPushClient == null) {
			jPushClient = new JPushClient(masterSecret, appKey);
		}

		return jPushClient;
	}

 
	/**
	 * 推送到alias列表
	 *
	 * @param alias             别名或别名组
	 * @param notificationTitle 通知内容标题
	 * @param msgTitle          消息内容标题
	 * @param msgContent        消息内容
	 * @param extras            扩展字段
	 */
	public void sendToAliasList(List alias, String notificationTitle, String msgTitle, String msgContent,
			String extras) {
		PushPayload pushPayload = buildPushObject_all_aliasList_alertWithTitle(alias, notificationTitle, msgTitle,
				msgContent, extras);
		this.sendPush(pushPayload);
	}
	
	/**
	 * 
	 * @param registrationIds  设备ID
	 * @param notificationTitle
	 * @param msgTitle
	 * @param msgContent
	 * @param extras
	 */
	public void sendToRegistRationIdsList(List registrationIds, String notificationTitle, String msgTitle, String msgContent,
			String extras) {
		PushPayload pushPayload = buildPushObject_all_registrationIdsList_alertWithTitle(registrationIds, notificationTitle, msgTitle,
				msgContent, extras);
		this.sendPush(pushPayload);
	}

	/**
	 * 推送到tag列表
	 *
	 * @param tagsList          Tag或Tag组
	 * @param notificationTitle 通知内容标题
	 * @param msgTitle          消息内容标题
	 * @param msgContent        消息内容
	 * @param extras            扩展字段
	 */
	public void sendToTagsList(List tagsList, String notificationTitle, String msgTitle, String msgContent,
			String extras) {
		PushPayload pushPayload = buildPushObject_all_tagList_alertWithTitle(tagsList, notificationTitle, msgTitle,
				msgContent, extras);
		this.sendPush(pushPayload);
	}

	/**
	 * 发送给所有安卓用户
	 *
	 * @param notificationTitle 通知内容标题
	 * @param msgTitle          消息内容标题
	 * @param msgContent        消息内容
	 * @param extras            扩展字段
	 */
	public void sendToAllAndroid(String notificationTitle, String msgTitle, String msgContent, String extras) {
		PushPayload pushPayload = buildPushObject_android_all_alertWithTitle(notificationTitle, msgTitle, msgContent,
				extras);
		this.sendPush(pushPayload);
	}

	/**
	 * 发送给所有IOS用户
	 *
	 * @param notificationTitle 通知内容标题
	 * @param msgTitle          消息内容标题
	 * @param msgContent        消息内容
	 * @param extras            扩展字段
	 */
	public void sendToAllIOS(String notificationTitle, String msgTitle, String msgContent, String extras) {
		PushPayload pushPayload = buildPushObject_ios_all_alertWithTitle(notificationTitle, msgTitle, msgContent,
				extras);
		this.sendPush(pushPayload);
	}

	/**
	 * 发送给所有用户
	 *
	 * @param notificationTitle 通知内容标题
	 * @param msgTitle          消息内容标题
	 * @param msgContent        消息内容
	 * @param extras            扩展字段
	 */
	public void sendToAll(String notificationTitle, String msgTitle, String msgContent, String extras) {
		PushPayload pushPayload = buildPushObject_android_and_ios(notificationTitle, msgTitle, msgContent, extras);
		this.sendPush(pushPayload);
	}

	private PushResult sendPush(PushPayload pushPayload) {
		logger.info("pushPayload={}", pushPayload);
		PushResult pushResult = null;
		try {
			pushResult = this.getJPushClient().sendPush(pushPayload);
			logger.info("" + pushResult);
			if (pushResult.getResponseCode() == RESPONSE_OK) {
				logger.info("push successful, pushPayload={}", pushPayload);
			}
		} catch (APIConnectionException e) {
			logger.error("push failed: pushPayload={}, exception={}", pushPayload, e);
		} catch (APIRequestException e) {
			logger.error("push failed: pushPayload={}, exception={}", pushPayload, e);
		}

		return pushResult;
	}

	/**
	 * 向所有平台所有用户推送消息
	 *
	 * @param notificationTitle
	 * @param msgTitle
	 * @param msgContent
	 * @param extras
	 * @return
	 */
	public PushPayload buildPushObject_android_and_ios(String notificationTitle, String msgTitle, String msgContent,
			String extras) {
		return PushPayload.newBuilder().setPlatform(Platform.android_ios()).setAudience(Audience.all())
				.setNotification(
						Notification.newBuilder().setAlert(notificationTitle)
								.addPlatformNotification(AndroidNotification.newBuilder().setAlert(notificationTitle)
										.setTitle(notificationTitle)
										// 此字段为透传字段,不会显示在通知栏。用户可以通过此字段来做一些定制需求,如特定的key传要指定跳转的页面(value)
										.addExtra("androidNotification extras key", extras).build())
								.addPlatformNotification(IosNotification.newBuilder()
										// 传一个IosAlert对象,指定apns
										// title、title、subtitle等
										.setAlert(notificationTitle)
										// 直接传alert
										// 此项是指定此推送的badge自动加1
										.incrBadge(1)
										// 此字段的值default表示系统默认声音;传sound.caf表示此推送以项目里面打包的sound.caf声音来提醒,
										// 如果系统没有此音频则以系统默认声音提醒;此字段如果传空字符串,iOS9及以上的系统是无声音提醒,以下的系统是默认声音
										.setSound("default")
										// 此字段为透传字段,不会显示在通知栏。用户可以通过此字段来做一些定制需求,如特定的key传要指定跳转的页面(value)
										.addExtra("iosNotification extras key", extras)
										// 此项说明此推送是一个background推送,想了解background看:http://docs.jpush.io/client/ios_tutorials/#ios-7-background-remote-notification
										// .setContentAvailable(true)
										.build())
								.build())
				// Platform指定了哪些平台就会像指定平台中符合推送条件的设备进行推送。 jpush的自定义消息,
				// sdk默认不做任何处理,不会有通知提示。建议看文档http://docs.jpush.io/guideline/faq/的
				// [通知与自定义消息有什么区别?]了解通知和自定义消息的区别
				.setMessage(Message.newBuilder().setMsgContent(msgContent).setTitle(msgTitle)
						.addExtra("message extras key", extras).build())
				.setOptions(Options.newBuilder()
						// 此字段的值是用来指定本推送要推送的apns环境,false表示开发,true表示生产;对android和自定义消息无意义
						.setApnsProduction(apnsProduction)
						// 此字段是给开发者自己给推送编号,方便推送者分辨推送记录
						.setSendno(1)
						// 此字段的值是用来指定本推送的离线保存时长,如果不传此字段则默认保存一天,最多指定保留十天,单位为秒
						.setTimeToLive(86400).build())
				.build();
	}

	/**
	 * 向所有平台单个或多个指定别名用户推送消息
	 *
	 * @param aliasList
	 * @param notificationTitle
	 * @param msgTitle
	 * @param msgContent
	 * @param extras
	 * @return
	 */
	private PushPayload buildPushObject_all_aliasList_alertWithTitle(List aliasList, String notificationTitle,
			String msgTitle, String msgContent, String extras) {
		// 创建一个IosAlert对象,可指定APNs的alert、title等字段
		// IosAlert iosAlert = IosAlert.newBuilder().setTitleAndBody("title",
		// "alert body").build();

		return PushPayload.newBuilder()
				// 指定要推送的平台,all代表当前应用配置了的所有平台,也可以传android等具体平台
				.setPlatform(Platform.all())
				// 指定推送的接收对象,all代表所有人,也可以指定已经设置成功的tag或alias或该应应用客户端调用接口获取到的registration
				// id
				.setAudience(Audience.alias(aliasList))
				// jpush的通知,android的由jpush直接下发,iOS的由apns服务器下发,Winphone的由mpns下发
				.setNotification(Notification.newBuilder()
						// 指定当前推送的android通知
						.addPlatformNotification(
								AndroidNotification.newBuilder().setAlert(notificationTitle).setTitle(notificationTitle)
										// 此字段为透传字段,不会显示在通知栏。用户可以通过此字段来做一些定制需求,如特定的key传要指定跳转的页面(value)
										.addExtra("androidNotification extras key", extras).build())
						// 指定当前推送的iOS通知
						.addPlatformNotification(IosNotification.newBuilder()
								// 传一个IosAlert对象,指定apns
								// title、title、subtitle等
								.setAlert(notificationTitle)
								// 直接传alert
								// 此项是指定此推送的badge自动加1
								.incrBadge(1)
								// 此字段的值default表示系统默认声音;传sound.caf表示此推送以项目里面打包的sound.caf声音来提醒,
								// 如果系统没有此音频则以系统默认声音提醒;此字段如果传空字符串,iOS9及以上的系统是无声音提醒,以下的系统是默认声音
								.setSound("default")
								// 此字段为透传字段,不会显示在通知栏。用户可以通过此字段来做一些定制需求,如特定的key传要指定跳转的页面(value)
								.addExtra("iosNotification extras key", extras)
								// 此项说明此推送是一个background推送,想了解background看:http://docs.jpush.io/client/ios_tutorials/#ios-7-background-remote-notification
								// 取消此注释,消息推送时ios将无法在锁屏情况接收
								// .setContentAvailable(true)
								.build())
						.build())
				// Platform指定了哪些平台就会像指定平台中符合推送条件的设备进行推送。 jpush的自定义消息,
				// sdk默认不做任何处理,不会有通知提示。建议看文档http://docs.jpush.io/guideline/faq/的
				// [通知与自定义消息有什么区别?]了解通知和自定义消息的区别
				.setMessage(Message.newBuilder().setMsgContent(msgContent).setTitle(msgTitle)
						.addExtra("message extras key", extras).build())
				.setOptions(Options.newBuilder()
						// 此字段的值是用来指定本推送要推送的apns环境,false表示开发,true表示生产;对android和自定义消息无意义
						.setApnsProduction(apnsProduction)
						// 此字段是给开发者自己给推送编号,方便推送者分辨推送记录
						.setSendno(1)
						// 此字段的值是用来指定本推送的离线保存时长,如果不传此字段则默认保存一天,最多指定保留十天;
						.setTimeToLive(86400).build())
				.build();

	}

	/**
	 * 向所有平台单个或多个指定Tag用户推送消息
	 *
	 * @param tagsList
	 * @param notificationTitle
	 * @param msgTitle
	 * @param msgContent
	 * @param extras
	 * @return
	 */
	private PushPayload buildPushObject_all_tagList_alertWithTitle(List tagsList, String notificationTitle,
			String msgTitle, String msgContent, String extras) {
		// 创建一个IosAlert对象,可指定APNs的alert、title等字段
		// IosAlert iosAlert = IosAlert.newBuilder().setTitleAndBody("title",
		// "alert body").build();

		return PushPayload.newBuilder()
				// 指定要推送的平台,all代表当前应用配置了的所有平台,也可以传android等具体平台
				.setPlatform(Platform.all())
				// 指定推送的接收对象,all代表所有人,也可以指定已经设置成功的tag或alias或该应应用客户端调用接口获取到的registration
				// id
				.setAudience(Audience.tag(tagsList))
				// jpush的通知,android的由jpush直接下发,iOS的由apns服务器下发,Winphone的由mpns下发
				.setNotification(Notification.newBuilder()
						// 指定当前推送的android通知
						.addPlatformNotification(
								AndroidNotification.newBuilder().setAlert(notificationTitle).setTitle(notificationTitle)
										// 此字段为透传字段,不会显示在通知栏。用户可以通过此字段来做一些定制需求,如特定的key传要指定跳转的页面(value)
										.addExtra("androidNotification extras key", extras).build())
						// 指定当前推送的iOS通知
						.addPlatformNotification(IosNotification.newBuilder()
								// 传一个IosAlert对象,指定apns
								// title、title、subtitle等
								.setAlert(notificationTitle)
								// 直接传alert
								// 此项是指定此推送的badge自动加1
								.incrBadge(1)
								// 此字段的值default表示系统默认声音;传sound.caf表示此推送以项目里面打包的sound.caf声音来提醒,
								// 如果系统没有此音频则以系统默认声音提醒;此字段如果传空字符串,iOS9及以上的系统是无声音提醒,以下的系统是默认声音
								.setSound("default")
								// 此字段为透传字段,不会显示在通知栏。用户可以通过此字段来做一些定制需求,如特定的key传要指定跳转的页面(value)
								.addExtra("iosNotification extras key", extras)
								// 此项说明此推送是一个background推送,想了解background看:http://docs.jpush.io/client/ios_tutorials/#ios-7-background-remote-notification
								// 取消此注释,消息推送时ios将无法在锁屏情况接收
								// .setContentAvailable(true)
								.build())
						.build())
				// Platform指定了哪些平台就会像指定平台中符合推送条件的设备进行推送。 jpush的自定义消息,
				// sdk默认不做任何处理,不会有通知提示。建议看文档http://docs.jpush.io/guideline/faq/的
				// [通知与自定义消息有什么区别?]了解通知和自定义消息的区别
				.setMessage(Message.newBuilder().setMsgContent(msgContent).setTitle(msgTitle)
						.addExtra("message extras key", extras).build())
				.setOptions(Options.newBuilder()
						// 此字段的值是用来指定本推送要推送的apns环境,false表示开发,true表示生产;对android和自定义消息无意义
						.setApnsProduction(apnsProduction)
						// 此字段是给开发者自己给推送编号,方便推送者分辨推送记录
						.setSendno(1)
						// 此字段的值是用来指定本推送的离线保存时长,如果不传此字段则默认保存一天,最多指定保留十天;
						.setTimeToLive(86400).build())
				.build();

	}

	
	/**
	 * 向所有平台单个或多个指定Tag用户推送消息
	 *
	 * @param tagsList
	 * @param notificationTitle
	 * @param msgTitle
	 * @param msgContent
	 * @param extras
	 * @return
	 */
	private PushPayload buildPushObject_all_registrationIdsList_alertWithTitle(List tagsList, String notificationTitle,
			String msgTitle, String msgContent, String extras) {
		// 创建一个IosAlert对象,可指定APNs的alert、title等字段
		// IosAlert iosAlert = IosAlert.newBuilder().setTitleAndBody("title",
		// "alert body").build();

		return PushPayload.newBuilder()
				// 指定要推送的平台,all代表当前应用配置了的所有平台,也可以传android等具体平台
				.setPlatform(Platform.all())
				// 指定推送的接收对象,all代表所有人,也可以指定已经设置成功的tag或alias或该应应用客户端调用接口获取到的registration
				// id
				.setAudience(Audience.registrationId(tagsList))
				// jpush的通知,android的由jpush直接下发,iOS的由apns服务器下发,Winphone的由mpns下发
				.setNotification(Notification.newBuilder()
						// 指定当前推送的android通知
						.addPlatformNotification(
								AndroidNotification.newBuilder().setAlert(notificationTitle).setTitle(msgTitle)
										// 此字段为透传字段,不会显示在通知栏。用户可以通过此字段来做一些定制需求,如特定的key传要指定跳转的页面(value)
										.addExtra("androidNotification extras key", extras).build())
						// 指定当前推送的iOS通知
						.addPlatformNotification(IosNotification.newBuilder()
								// 传一个IosAlert对象,指定apns
								// title、title、subtitle等
								.setAlert(notificationTitle)
								 
								// 直接传alert
								// 此项是指定此推送的badge自动加1
								.incrBadge(1)
								// 此字段的值default表示系统默认声音;传sound.caf表示此推送以项目里面打包的sound.caf声音来提醒,
								// 如果系统没有此音频则以系统默认声音提醒;此字段如果传空字符串,iOS9及以上的系统是无声音提醒,以下的系统是默认声音
								.setSound("default")
								// 此字段为透传字段,不会显示在通知栏。用户可以通过此字段来做一些定制需求,如特定的key传要指定跳转的页面(value)
								.addExtra("iosNotification extras key", extras)
								// 此项说明此推送是一个background推送,想了解background看:http://docs.jpush.io/client/ios_tutorials/#ios-7-background-remote-notification
								// 取消此注释,消息推送时ios将无法在锁屏情况接收
								// .setContentAvailable(true)
								.build())
						.build())
				// Platform指定了哪些平台就会像指定平台中符合推送条件的设备进行推送。 jpush的自定义消息,
				// sdk默认不做任何处理,不会有通知提示。建议看文档http://docs.jpush.io/guideline/faq/的
				// [通知与自定义消息有什么区别?]了解通知和自定义消息的区别
				.setMessage(Message.newBuilder().setMsgContent(msgContent).setTitle(msgTitle)
						.addExtra("message extras key", extras).build())
				.setOptions(Options.newBuilder()
						// 此字段的值是用来指定本推送要推送的apns环境,false表示开发,true表示生产;对android和自定义消息无意义
						.setApnsProduction(apnsProduction)
						// 此字段是给开发者自己给推送编号,方便推送者分辨推送记录
						.setSendno(1)
						// 此字段的值是用来指定本推送的离线保存时长,如果不传此字段则默认保存一天,最多指定保留十天;
						.setTimeToLive(86400).build())
				.build();

	}

	/**
	 * 向android平台所有用户推送消息
	 *
	 * @param notificationTitle
	 * @param msgTitle
	 * @param msgContent
	 * @param extras
	 * @return
	 */
	private PushPayload buildPushObject_android_all_alertWithTitle(String notificationTitle, String msgTitle,
			String msgContent, String extras) {
		return PushPayload.newBuilder()
				// 指定要推送的平台,all代表当前应用配置了的所有平台,也可以传android等具体平台
				.setPlatform(Platform.android())
				// 指定推送的接收对象,all代表所有人,也可以指定已经设置成功的tag或alias或该应应用客户端调用接口获取到的registration
				// id
				.setAudience(Audience.all())
				// jpush的通知,android的由jpush直接下发,iOS的由apns服务器下发,Winphone的由mpns下发
				.setNotification(Notification.newBuilder()
						// 指定当前推送的android通知
						.addPlatformNotification(
								AndroidNotification.newBuilder().setAlert(notificationTitle).setTitle(notificationTitle)
										// 此字段为透传字段,不会显示在通知栏。用户可以通过此字段来做一些定制需求,如特定的key传要指定跳转的页面(value)
										.addExtra("androidNotification extras key", extras).build())
						.build())
				// Platform指定了哪些平台就会像指定平台中符合推送条件的设备进行推送。 jpush的自定义消息,
				// sdk默认不做任何处理,不会有通知提示。建议看文档http://docs.jpush.io/guideline/faq/的
				// [通知与自定义消息有什么区别?]了解通知和自定义消息的区别
				.setMessage(Message.newBuilder().setMsgContent(msgContent).setTitle(msgTitle)
						.addExtra("message extras key", extras).build())

				.setOptions(Options.newBuilder()
						// 此字段的值是用来指定本推送要推送的apns环境,false表示开发,true表示生产;对android和自定义消息无意义
						.setApnsProduction(apnsProduction)
						// 此字段是给开发者自己给推送编号,方便推送者分辨推送记录
						.setSendno(1)
						// 此字段的值是用来指定本推送的离线保存时长,如果不传此字段则默认保存一天,最多指定保留十天,单位为秒
						.setTimeToLive(86400).build())
				.build();
	}

	/**
	 * 向ios平台所有用户推送消息
	 *
	 * @param notificationTitle
	 * @param msgTitle
	 * @param msgContent
	 * @param extras
	 * @return
	 */
	private PushPayload buildPushObject_ios_all_alertWithTitle(String notificationTitle, String msgTitle,
			String msgContent, String extras) {
		return PushPayload.newBuilder()
				// 指定要推送的平台,all代表当前应用配置了的所有平台,也可以传android等具体平台
				.setPlatform(Platform.ios())
				// 指定推送的接收对象,all代表所有人,也可以指定已经设置成功的tag或alias或该应应用客户端调用接口获取到的registration
				// id
				.setAudience(Audience.all())
				// jpush的通知,android的由jpush直接下发,iOS的由apns服务器下发,Winphone的由mpns下发
				.setNotification(Notification.newBuilder()
						// 指定当前推送的android通知
						.addPlatformNotification(IosNotification.newBuilder()
								// 传一个IosAlert对象,指定apns title、title、subtitle等
								.setAlert(notificationTitle)
								// 直接传alert
								// 此项是指定此推送的badge自动加1
								.incrBadge(1)
								// 此字段的值default表示系统默认声音;传sound.caf表示此推送以项目里面打包的sound.caf声音来提醒,
								// 如果系统没有此音频则以系统默认声音提醒;此字段如果传空字符串,iOS9及以上的系统是无声音提醒,以下的系统是默认声音
								.setSound("default")
								// 此字段为透传字段,不会显示在通知栏。用户可以通过此字段来做一些定制需求,如特定的key传要指定跳转的页面(value)
								.addExtra("iosNotification extras key", extras)
								// 此项说明此推送是一个background推送,想了解background看:http://docs.jpush.io/client/ios_tutorials/#ios-7-background-remote-notification
								// .setContentAvailable(true)
								.build())
						.build())
				// Platform指定了哪些平台就会像指定平台中符合推送条件的设备进行推送。 jpush的自定义消息,
				// sdk默认不做任何处理,不会有通知提示。建议看文档http://docs.jpush.io/guideline/faq/的
				// [通知与自定义消息有什么区别?]了解通知和自定义消息的区别
				.setMessage(Message.newBuilder().setMsgContent(msgContent).setTitle(msgTitle)
						.addExtra("message extras key", extras).build())
				.setOptions(Options.newBuilder()
						// 此字段的值是用来指定本推送要推送的apns环境,false表示开发,true表示生产;对android和自定义消息无意义
						.setApnsProduction(apnsProduction)
						// 此字段是给开发者自己给推送编号,方便推送者分辨推送记录
						.setSendno(1)
						// 此字段的值是用来指定本推送的离线保存时长,如果不传此字段则默认保存一天,最多指定保留十天,单位为秒
						.setTimeToLive(86400).build())
				.build();
	}
	
	/**
	 * 默认按照注册设备ID推送消息
	 * @param rIdsList
	 * @param notificationTitle
	 * @param msgTitle
	 * @param msgContent
	 */
    public void sendToRIdsListdefault(List rIdsList,String notificationTitle,String msgTitle,String msgContent ) {
       // List aliasList = Arrays.asList("170976fa 8acf4909843","100d855909006086b67","18071adc03474ca0fc1");
        //String notificationTitle = "你有新的4条待办任务";
        //String msgTitle = "msg_title";
       // String msgContent = "更新内容:修复了一些小bug";
        
       this.sendToRegistRationIdsList(rIdsList, notificationTitle, msgTitle, msgContent, "exts");
 	}

}

4、测试JPushApplicationTests.java

package com.sgitg.**.service;

import java.util.ArrayList;
import java.util.List;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

@RunWith(SpringRunner.class)
@SpringBootTest
public class JPushApplicationTests {

	@Autowired
	private JPushService jPushService;

	@Test
	public void testJPush() {
 
		List aliasList = new ArrayList();
		aliasList.add("170976fa8acf4909843");
		aliasList.add("100d855909006086b67");
		aliasList.add("18071adc03474ca0fc1");
		String notificationTitle = "你有新的1条待办任务";
		String msgTitle = "APP名称";
		String msgContent = "";
         //默认按注册ID推送 消息 
		jPushService.sendToRegistRationIdsList(aliasList, notificationTitle, msgTitle, msgContent, "exts");
		 //推送所有设备广播
		// jPushService.sendToAll(notificationTitle, msgTitle, msgContent, "exts");

	}

}

 

 

springboot+APPcan集成极光推送,实现APP消息推送_第1张图片

springboot+APPcan集成极光推送,实现APP消息推送_第2张图片

 

二、APPcan端app前端实现

1、配置config.xml配置,同样是APPkey,和极光官网创建的应用对应的包名和app的包名保持一致

      
        
        
    

2、获取 registrationID 的js代码,登录时传入后台保存,后台在需要发送消息的地方调用推送服务的方法,实现消息推送。

   window.uexOnload = function(){
    	(function(){
    	 
		    var jpush = typeof(uexJPush);
		    if(jpush == "undefined"){
		    	return alert("没有集成极光推送");
		    }
	    	uexJPush.cbSetAlias = function(data){
				//单独设置别名,只能设置一个别名
	
				var msg = JSON.parse(data);
				if(msg){
					if(msg.result == 0){
						alert("设置别名成功,:" + msg.alias);
					}else{
						alert("设置别名失败");
					}
				}else{
					alert("json转换失败")
				}
			}
			uexJPush.cbSetTags=function(data){
				//单独设置标签,
	
				var msg = JSON.parse(data);
				if(msg){
					if(msg.result == 0){
						alert("设置标签成功,:" + msg.tags);
					}else{
						alert("设置标签失败");
					}
				}else{
					alert("json转换失败")
				}
		    }
			uexJPush.cbSetAliasAndTags=function(data){
				//同时设置标签和别名
				var msg = JSON.parse(data);
				if(msg){
					if(msg.result == 0){
						alert("设置的标签:" + msg.tags);
						alert("设置的别名:" + msg.alias);
					}else{
						alert("设置标签失败");
					}
				}else{
					alert("json转换失败")
				}
		    }
		    uexJPush.cbGetRegistrationID=function(data){
		    	//获取getRegistrationID
		        alert(data);
		    }
		    
		  /**  $(document).on("tap",".myjpush",function(){
		    	var str = '';
				//获取getRegistrationID
				str = uexJPush.getRegistrationID();
		    	alert(str);
		    });**/
    	})();   




 	//有效的 RegistrationID 长度为11或19个字节,其中第一位有效字符[0,1],第二位有效字符[0-9a],第三位有效字符[0-2],剩余位有效字符[0-9a-f],一次最多1000个。
    	//建议您根据限制规则修改不合法 RegistrationID 。
    	var registrationID = "";
    	(function(){
    		var jpush = typeof(uexJPush);
		    if(jpush == "undefined"){
		    	return ;
		    }else{
		    	(function(){		    		
					var thefun = arguments.callee;
			    	registrationID = uexJPush.getRegistrationID();
			    	if((registrationID.length >= 11) && (registrationID.length <= 19)){
			    		return ;
			    	}else{
			    		setTimeout(function(){
				    		thefun();
				    	},500);
			    	}
		    	})();
		    }
    	})();

 

你可能感兴趣的:(springboot+APPcan集成极光推送,实现APP消息推送)