App integrates third parties to realize message push function

App integrates third parties to realize message push function

There are many domestic push platforms, the common ones are:

  • Aurora push
  • Alibaba Cloud Push
  • Umeng push
  • Tencent Cloud Push
  • Tweets

Personally, I have done two pushes so far, both of which are using Jiguang push. I am not advertising for Jiguang, it is because Jiguang has a free version, haha, I haven’t used it on other platforms so I’m not sure if there is a free version. Yes, this time I will summarize the process of integrating Aurora Push, if you forget it later, you can check it again

One: Register and log in to Jiguang official website (preferably company account, mobile phone number)

2: Log in to the service center (below the avatar), enter the developer platform, and click on the message push

Insert picture description here

Three: Create an application, get AppKey and Master Secret , so that you can configure it in your yml configuration file later

Four: Push settings. At this time, you need to communicate with the front end to obtain the Android application package name and ios certificate

Insert picture description here
Insert picture description here

Five: After the setting is completed, we will start our configuration and business code. Note that the code below can be quoted, and some push business codes may not be included

Pom dependency add:

<!--极光推送-->
        <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.3</version>
        </dependency>
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-api</artifactId>
            <version>RELEASE</version>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
        </dependency>

config configuration file: JPushConfig

package cn.xof.config;

import cn.jpush.api.JPushClient;
import lombok.Data;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;
import javax.annotation.PostConstruct;

@Configuration
@Data
@ConfigurationProperties(prefix = "xym.push")
public class JPushConfig {

	private String appkey;
	private String secret;
	private boolean apns;

	private JPushClient jPushClient;

	/**
	 * 推送客户端
	 * @return
	 */
	@PostConstruct
	public void initJPushClient() {
		jPushClient = new JPushClient(secret, appkey);
	}

	/**
	 * 获取推送客户端
	 * @return
	 */
	public JPushClient getJPushClient() {
		return jPushClient;
	}

	/**
	 * 区分开发和线上环境
	 * @return
	 */
	public boolean getApns() {
		return apns;
	}
}

PushService:

package cn.xof.tzy.service;


import cn.xof.tzy.entity.PushBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * 推送服务
 * 封装业务功能相关
 * @author xym
 */
@Service
public class PushService {

	/** 一次推送最大数量 (极光限制1000) */
	private static final int max_size = 800;

	@Autowired
	private JPushService jPushService;

	/**
	 * 推送全部, 不支持附加信息
	 * @author xym
	 * @return
	 */
	public boolean pushAll(PushBean pushBean){
		return jPushService.pushAll(pushBean);
	}

	/**
	 * 推送全部ios
	 * @author xym
	 * @return
	 */
	public boolean pushIos(PushBean pushBean){
		return jPushService.pushIos(pushBean);
	}

	/**
	 * 推送ios 指定id
	 * @author xym
	 * @return
	 */
	public boolean pushIos(PushBean pushBean, String ... registids){
		registids = checkRegistids(registids); // 剔除无效registed
		while (registids.length > max_size) { // 每次推送max_size个
			jPushService.pushIos(pushBean, Arrays.copyOfRange(registids, 0, max_size));
			registids = Arrays.copyOfRange(registids, max_size, registids.length);
		}
		return jPushService.pushIos(pushBean, registids);
	}

	/**
	 * 推送全部android
	 * @author xym
	 * @return
	 */
	public boolean pushAndroid(PushBean pushBean){
		return jPushService.pushAndroid(pushBean);
	}

	/**
	 * 推送android 指定id
	 * @author xym
	 * @return
	 */
	public boolean pushAndroid(PushBean pushBean, String ... registids){
		registids = checkRegistids(registids); // 剔除无效registed
		while (registids.length > max_size) { // 每次推送max_size个
			jPushService.pushAndroid(pushBean, Arrays.copyOfRange(registids, 0, max_size));
			registids = Arrays.copyOfRange(registids, max_size, registids.length);
		}
		return jPushService.pushAndroid(pushBean, registids);
	}

	/**
	 * 剔除无效registed
	 * @author xym [2016年7月15日 下午4:03:31]
	 * @param registids
	 * @return
	 */
	private String[] checkRegistids(String[] registids) {
		List<String> regList = new ArrayList<String>(registids.length);
		for (String registid : registids) {
			if (registid!=null && !"".equals(registid.trim())) {
				regList.add(registid);
			}
		}
		return regList.toArray(new String[0]);
	}
}

JPushService:

package cn.xof.tzy.service;


import cn.jiguang.common.resp.APIConnectionException;
import cn.jiguang.common.resp.APIRequestException;
import cn.jpush.api.push.PushResult;
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.IosNotification;
import cn.jpush.api.push.model.notification.Notification;
import cn.xof.config.JPushConfig;
import cn.xof.tzy.entity.PushBean;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 极光推送
 * 封装第三方api相关
 * @author xym
 */
@Service
@Slf4j
public class JPushService {
	@Autowired
	private JPushConfig jPushConfig;

	//@Autowired
	//private  TbPushHistoryService pushHistoryService;


	/**
	 * 广播 (所有平台,所有设备, 不支持附加信息)
	 * @author xym
	 * @param pushBean 推送内容
	 * @return
	 */
	public boolean pushAll(PushBean pushBean){
		return sendPush(PushPayload.newBuilder()
	            .setPlatform(Platform.all())
	            .setAudience(Audience.all())
	            .setNotification(Notification.alert(pushBean.getAlert()))
	            .setOptions(Options.newBuilder().setApnsProduction(jPushConfig.getApns()).build())
	            .build());
	}

	/**
	 * ios广播
	 * @author xym
	 * @param pushBean 推送内容
	 * @return
	 */
	public boolean pushIos(PushBean pushBean){
		return sendPush(PushPayload.newBuilder()
				.setPlatform(Platform.ios())
				.setAudience(Audience.all())
				.setNotification(Notification.ios(pushBean.getAlert(), pushBean.getExtras()))
				.setOptions(Options.newBuilder().setApnsProduction(jPushConfig.getApns()).build())
                .build());
	}

	/**
	 * ios通过registid推送 (一次推送最多 1000 个)
	 * @author xym
	 * @param pushBean 推送内容
	 * @param registids 推送id
	 * @return
	 */
	public boolean pushIos(PushBean pushBean, String ... registids){
		return sendPush(PushPayload.newBuilder()
				.setPlatform(Platform.ios())
				//.setAudience(Audience.registrationId(registids))
				.setAudience(Audience.alias(registids))
				//.setNotification(Notification.ios(pushBean.getAlert(), pushBean.getExtras()))
                .setNotification(Notification.newBuilder()
                        .addPlatformNotification(IosNotification.newBuilder()
                                .setAlert(pushBean.getAlert())
                                .setBadge(1)
                                .setSound("default")
								.addExtras(pushBean.getExtras())
                                .build())
                        .build())
				.setOptions(Options.newBuilder().setApnsProduction(jPushConfig.getApns()).build())
				.build());
	}


	/**
	 * android广播
	 * @author xym
	 * @param pushBean 推送内容
	 * @return
	 */
	public boolean pushAndroid(PushBean pushBean){
		return sendPush(PushPayload.newBuilder()
                .setPlatform(Platform.android())
                .setAudience(Audience.all())
                .setNotification(Notification.android(pushBean.getAlert(), pushBean.getTitle(), pushBean.getExtras()))
                .setOptions(Options.newBuilder().setApnsProduction(jPushConfig.getApns()).build())
                .build());
	}


	/**
	 * android通过registid推送 (一次推送最多 1000 个)
	 * @author xym
	 * @param pushBean 推送内容
	 * @param registids 推送id
	 * @return
	 */
	public boolean pushAndroid(PushBean pushBean, String ... registids){
		return sendPush(PushPayload.newBuilder()
                .setPlatform(Platform.android())
				//pushPayloadBuilder.setAudience(Audience.alias(alias.toString()));
                .setAudience(Audience.alias(registids))
               // .setAudience(Audience.registrationId(registids))
                .setNotification(Notification.android(pushBean.getAlert(), pushBean.getTitle(), pushBean.getExtras()))
                .setOptions(Options.newBuilder().setApnsProduction(jPushConfig.getApns()).build())
                .build());
	}
	//PushPayload

	/**
	 * 调用api推送
	 * @author xym
	 * @param pushPayload 推送实体
	 * @return
	 */
	private boolean sendPush(PushPayload pushPayload){
		//pushHistoryService
		//TbPushHistory pushHistory = new TbPushHistory();
		JsonParser parse =new JsonParser();  //创建json解析器
		JsonObject json=(JsonObject) parse.parse(pushPayload.toString());  //创建jsonObject对象
		log.info("发送极光推送请求: {}", pushPayload);
		PushResult result = null;
		//PushBean pushBean=new PushBean();
		try{
			result = jPushConfig.getJPushClient().sendPush(pushPayload);
		} catch (APIConnectionException e) {
			log.error("极光推送连接异常: ",e.isReadTimedout());
			return  false;
		} catch (APIRequestException e) {
			log.error("极光推送请求异常: ", e.getErrorMessage());
			return  false;
		}
		String platform = json.get("platform").toString();
		//pushHistory.setCreateTime(new Date());
		//pushHistory.setDeviceType(platform);
	/*	if (platform!="all")
		{
			String alias = json.get("audience").getAsJsonObject().get("alias").toString();
			pushHistory.setAlias(alias);
		}*/
		//String notification = json.get("notification").getAsJsonObject().get("android").getAsJsonObject().get("alert").toString();
		String notification = json.get("notification").toString();
		//pushHistory.setMessage(notification);
		String options = json.get("options").toString();
		//pushHistory.setExtras(options);
		if (result!=null && result.isResultOK()) {
			//pushHistory.setStatus(0);
			//pushHistoryService.insert(pushHistory);
			//String alias2 = json.get("notification").getAsJsonObject().get("android").getAsJsonObject().get("title").toString();
			//String alias3 = json.get("options").getAsJsonObject().get("sendno").toString();
			//String alias4 = json.get("options").getAsJsonObject().get("apns_production").toString();
			//JsonObject result=json.get("result").getAsJsonObject();

			//JSONObject object = JSONObject.parseObject(pushPayload.toString());
		//	JsonObject result=json.get("result").getAsJsonObject();
			//Object platform = object.get("platform");
			//Object audience = object.get("audience");
			//Object notification = object.get("notification");
			//object.get("options");
			log.info("极光推送请求成功: {}", result);
			return true;
		}else {
			//pushHistory.setStatus(1);
			//pushHistoryService.insert(pushHistory);
			log.info("极光推送请求失败: {}", result);
			return false;
		}
	}
}

PushController:

package cn.xof.tzy.controller;
import cn.xof.common.Result;
import cn.xof.tzy.entity.PushBean;
import cn.xof.tzy.service.PushService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;

@RestController
@RequestMapping("/push")
@Slf4j
@CrossOrigin
@Api(value ="极光推送",description = "极光推送")
public class PushController {

	@Autowired
	private PushService pushService;

	/**
	 * 推送全部(包括ios和android)
	 * @param pushBean
	 * @return
	 */
	@ApiOperation("推送全部(包括ios和android")
	@RequestMapping(value="/all", method= RequestMethod.POST)
	public Result pushAll(@RequestBody PushBean pushBean) {
		if (pushService.pushAll(pushBean)){
			return Result.success("推送成功!");
		}else {
			return Result.failure("推送失败!");
		}

	}

	/**
	 * 推送全部ios
	 * @param pushBean
	 * @return
	 */
	@ApiOperation("推送全部ios")
	@RequestMapping(value="/ios/all", method= RequestMethod.POST)
	public Result pushIos(PushBean pushBean){
		if (pushService.pushIos(pushBean)){
			return Result.success("推送成功!");
		}else {
			return Result.failure("推送失败!");
		}
	}

	/**
	 * 推送指定ios
	 * @param pushBean
	 * @return
	 */
	@ApiOperation("推送指定ios,多个别名ID")
	@RequestMapping(value="/ios", method= RequestMethod.POST)
	public Result pushIos(PushBean pushBean, String[] registerids){
		 if(pushService.pushIos(pushBean, registerids)){
			 return Result.success("推送成功!");
		 }else {
			 return Result.failure("推送失败!");
		 }
	}

	/**
	 * 推送全部android
	 * @param pushBean
	 * @return
	 */
	@ApiOperation("推送全部android")
	@RequestMapping(value="/android/all", method= RequestMethod.POST)
	public Result pushAndroid(PushBean pushBean){
		if(pushService.pushAndroid(pushBean)){
			return Result.success("推送成功!");
		}else {
			return Result.failure("推送失败!");
		}
	}

	/**
	 * 推送指定android
	 * @param pushBean
	 * @return
	 */
	@ApiOperation("推送指定android,多个别名ID")
	@RequestMapping(value="/android", method= RequestMethod.POST)
	public Result pushAndroid(PushBean pushBean, String[] registerids){
		if(pushService.pushAndroid(pushBean, registerids)){
			return Result.success("推送成功!");
		}else {
			return Result.failure("推送失败!");
		}
	}

	@ApiOperation("多个别名ID,全设备推送")
	@RequestMapping(value="/allbyids", method= RequestMethod.POST)
	public Result pushAllbyids(@RequestBody PushBean pushBean, String[] registerids){
		if(pushService.pushAndroid(pushBean, registerids)){
			log.info(registerids.toString()+"指定别名Android推送成功");

			log.error(registerids.toString()+"指定别名Android推送失败!");
		}
		if(pushService.pushIos(pushBean, registerids)){
			log.info(registerids.toString()+"指定别名Android推送成功");
		}else {
			log.error(registerids.toString()+"指定别名Android推送失败!");
		}
		return Result.success("推送成功!");
	}
	

	//定时任务,每天下午2点,推送全部(包括ios和android)
	@Scheduled(cron = "0 0 14 * * ?")
	public void pushing(){
		PushBean pushBean = new PushBean();
		//可选,通知标题
		pushBean.setTitle("");
		//必填, 通知内容
		pushBean.setAlert("速~来~赚~钱~一大波任务来袭!");
		if (pushService.pushAll(pushBean)){
			log.info("{},推送成功!", LocalDateTime.now());
		}else {
			log.error("{},推送失败!", LocalDateTime.now());
		}
	}
}

PushBean:

package cn.xof.tzy.entity;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.Map;

@Data
@AllArgsConstructor
@NoArgsConstructor
public class PushBean {

	// 必填, 通知内容, 内容可以为空字符串,则表示不展示到通知栏。
	private String alert;
	// 可选, 附加信息, 供业务使用。
	private Map<String, String> extras;
	//android专用// 可选, 通知标题	如果指定了,则通知里原来展示 App名称的地方,将展示成这个字段。
	private String title;

}

yml add configuration:

  #极光推送
  push:
    appkey: bdbdabc57feec51b567620xx
    secret: a291ae194a16e7166575f1xx
    apns: false

Remember to change xym to your own in your config configuration file : @ConfigurationProperties(prefix = “xym.push”)

Six: Test
You can test in the developer service (category: 1, tag 2, alias 3, all , you can choose below when you send a push), you can also test in the code, but if you haven’t purchased it yet, It is recommended to use designated android or designated ios interface test, such as in JPushService :

public boolean pushAndroid(PushBean pushBean, String ... registids){
		return sendPush(PushPayload.newBuilder()
                .setPlatform(Platform.android())
				//pushPayloadBuilder.setAudience(Audience.alias(alias.toString()));
                .setAudience(Audience.alias(“666”))
               // .setAudience(Audience.registrationId(registids))
                .setNotification(Notification.android(pushBean.getAlert(), pushBean.getTitle(), pushBean.getExtras()))
                .setOptions(Options.newBuilder().setApnsProduction(jPushConfig.getApns()).build())
                .build());
	}

Click on advanced features
Click on advanced features

Insert picture description here
Insert picture description here
Everyone try it more, it's fun, and then wait for the integration of the android and ios partners

Aurora used to charge a monthly fee, which is also quite cost-effective. Now it is 48,000 per year. The discount is 28,800 after a discount of 0.6%. This depends on whether the company can buy it. If you don’t buy it, you will use the free version, but you will need technical partners to set it up. Send the category, and if you are interested, you can check the prices of other platforms, welcome to leave a comment

Guess you like

Origin blog.csdn.net/weixin_44082075/article/details/109222401