Java xinge 消息推送Android和IOs

xinge消息推送: 比如就像淘宝一样,用户下完订单,就会给用户提示一条信息:订单核对等等。如果我们也想实现类似的功能呢?

一、往pom.xml文件中引入包

<dependency>
			<groupId>com.github.xingePush</groupId>
			<artifactId>xinge</artifactId>
			<version>1.2.0-SNAPSHOT</version>
		</dependency>
		<repository>
			<id>xingePush</id>
			<url>https://raw.githubusercontent.com/xingePush/maven-repository/snapshot/</url>
		</repository>

二、配置application.yml配置推送账号信息(可上xinge官网上进行申请)

      xinge:
			androidAppid: ########
			androidSecretkey: ################################
			   
			isoAppid: ########
			isoSecretkey: ################################
			

三、配置信息的实体类

@Data
		@Component
		@ConfigurationProperties(prefix = "xinge")
		public class XgAccountConfig {

			/**
			 * android端
			 */
			private String androidAppid;

			private String androidSecretkey;

			
			/**
			 * iso端
			 */
			private String isoAppid;

			private String isoSecretkey;

		}
		

四、进行注入配置

	@Component
		public class XingeConfig {
		@Autowired
		private XgAccountConfig xgAccountConfig;
		@Bean("androidXinge")
		public XingeApp androidXingeUser() {
			return new XingeApp(xgAccountConfig.getAndroidAppid(), xgAccountConfig.getAndroidSecretkey());
		}

		@Bean("isoXinge")
		public XingeApp isoXingeUser() {
			return new XingeApp(xgAccountConfig.getIsoAppid(), xgAccountConfig.getIsoSecretkey());
		}

		}		
		

五、创建消息推送工具类

@Component
		public class XgPushUtil {
			/**
			 * 安卓端
			 */
			@Autowired
			private XingeApp androidXinge;
			/**
			 * ios端
			 */
			@Autowired
			private XingeApp isoXinge;

			/**
			 * 
			 * @param platformType
			 *            设备类型
			 * @param msgTitle
			 *            消息标题
			 * @param msgContent
			 *            消息内容
			 * @param token
			 *            设备token
			 * @return 消息是否推送成功
			 * @throws JSONException
			 */
			public boolean singlePushMsg(String platformType, String msgTitle, String msgContent,
					ArrayList<String> tokens) throws JSONException {

				PushAppRequest push = new PushAppRequest();

				ArrayList<String> token_list = tokens;
				push.setToken_list(token_list);
				push.setAudience_type(AudienceType.token_list);
				push.setMessage_type(MessageType.notify);
				Message message = new Message();
				message.setTitle(msgTitle);
				message.setContent(msgContent);
				push.setMessage(message);
				JSONObject json = null;
				// 根据不同的设备类型选用不同的操作
				if ("ios".equals(platformType)) {
					push.setEnvironment(Environment.dev);
					MessageIOS ios = new MessageIOS();
					Aps aps = new Aps();
					Alert alert = new Alert();
					aps.setAlert(alert);
					ios.setAps(aps);
					message.setIos(ios);
					push.setPlatform(Platform.ios);
					json = isoXinge.pushApp(push);// 进行消息推送
				} else if ("android".equals(platformType)) {
					push.setPlatform(Platform.android);
					// 判断用户的类型(快递员还是客户)
					json = androidXinge.pushApp(push);
				}

				if (json != null) {
					Object object = json.get("ret_code");
					if (object != null && object.equals(0)) {
						return true;
					} else {
						return false;
					}
				} else {
					return false;
				}
			}
		}
		

六、到时候很具设备类型,推送消息就行。 在每一次登录的时候,会重新获取一下,当前设备是Android还是iOS,获取当前的token.
转载需注明:https://blog.youkuaiyun.com/juan1997/article/details/90678923

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值