一、基础配置

1.依赖配置

<!-- 极光推送开始 -->
<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>com.google.code.gson</groupId><artifactId>gson</artifactId><version>2.3</version>
</dependency>
<dependency><groupId>cn.jpush.api</groupId><artifactId>jpush-client</artifactId><version>3.3.10</version>
</dependency>
<!-- 极光推送结束 -->

2、极光推送配置类  JpsuhConfig.java

package com.service.msg.config.jpush;import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;import cn.jiguang.common.ClientConfig;
import cn.jpush.api.JPushClient;/*** 极光推送配置类* @author: huang ******* @date:   2023年1月12日 上午10:28:53*/@Configuration
public class JpsuhConfig {@Value("${jpush.appKey}")private String appKey;@Value("${jpush.masterSecret}")private String masterSecret;public JPushClient createClient() {JPushClient jpushClient = new JPushClient("b671d2c5dcb1b6a72e1bde53", "aabbb7684426998689f16a02", null, ClientConfig.getInstance());return jpushClient;}
}

3、全局变量 AppKey 和 Master Secret 变量配置

3.1.两种配置方式

  • application.yml 配置文件配置
  • jpush.properties 配置文件配置
  • AppKey:移动端APP应用的唯一标识key
  • Master Secret:极光推送secret

3.2.本文是采用 yml格式配置

jpush:appKey: aabb******26998689f16a02masterSecret: b671d2c5dcb1b6*****de53apnsProduction: false 

apnsProduction: false  开发环境是FALSE;生产环境是TRUE;

4.封装极光推送工具类  JpushUtil.java

package com.service.msg.utils;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.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;
import com.alibaba.fastjson.JSONArray;
import java.util.List;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;/*** 极光推送工具包** @author: huang ******* @date: 2023年1月12日 上午10:28:53*/
public class JpushUtil {private static final Logger log = LoggerFactory.getLogger(JpushUtil.class);/** 上线之后要改为true */@Value("${jpush.apnsProduction}")private static final Boolean APNS_PRODUCTION = false;/** 上线之后要改为true */private static final int SUCCESS_CODE = 200;/*** 推送给别名的用户 @Description:** @param jPushClient* @param aliasList* @param notificationTitle* @param msgTitle* @param msgContent* @param extraKey* @param extrasparam* @return* @throws*/public static int sendToAliasList(JPushClient jPushClient,List<String> aliasList,String notificationTitle,String msgTitle,String msgContent,String extraKey,String extrasparam) {int result = 0;try {PushPayload pushPayload =buildPushObjectAllAliasList(aliasList, notificationTitle, msgTitle, msgContent, extraKey, extrasparam);log.info("推送给设备标识参数的用户" + pushPayload);PushResult pushResult = jPushClient.sendPush(pushPayload);log.info("推送结果" + pushResult);if (pushResult.getResponseCode() == SUCCESS_CODE) {result = 1;}} catch (APIConnectionException e) {e.printStackTrace();} catch (APIRequestException e) {e.printStackTrace();}return result;}/*** 推送给别名的用户(加入未读消息数量统计) @Description:** @param jPushClient* @param aliasList* @param notificationTitle* @param msgTitle* @param msgContent* @param extraKey* @param extrasparam* @oaram countParam* @return* @throws*/public static int sendToAliasList1(JPushClient jPushClient,List<String> aliasList,String notificationTitle,String msgTitle,String msgContent,String extraKey,String extrasparam,String countKey,String countParam) {int result = 0;try {PushPayload pushPayload =buildPushObjectAllAliasList1(aliasList,notificationTitle,msgTitle,msgContent,extraKey,extrasparam,countKey,countParam);log.info("推送给设备标识参数的用户" + pushPayload);PushResult pushResult = jPushClient.sendPush(pushPayload);log.info("推送结果" + pushResult);if (pushResult.getResponseCode() == SUCCESS_CODE) {result = 1;}} catch (APIConnectionException e) {e.printStackTrace();} catch (APIRequestException e) {e.printStackTrace();}return result;}/*** 发送给所有用户 @Description:** @param jPushClient* @param notificationTitle* @param msgTitle* @param msgContent* @param extraKey* @param extrasparam* @return* @throws*/public static int sendToAll(JPushClient jPushClient,String notificationTitle,String msgTitle,String msgContent,String extraKey,String extrasparam) {int result = 0;try {PushPayload pushPayload =buildPushObject(notificationTitle, msgTitle, msgContent, extraKey, extrasparam);log.info("" + pushPayload);PushResult pushResult = jPushClient.sendPush(pushPayload);log.info("" + pushResult);if (pushResult.getResponseCode() == SUCCESS_CODE) {result = 1;}} catch (Exception e) {e.printStackTrace();}return result;}/*** 推送给Tag参数的用户** @param tagsList Tag或Tag组* @param msgContent 消息内容* @param extrasparam 扩展字段* @return 0推送失败,1推送成功*/public static int sendToTagList(JPushClient jPushClient,List<String> tagsList,String notificationTitle,String msgTitle,String msgContent,String extraKey,String extrasparam) {int result = 0;try {PushPayload pushPayload =buildPushObjectAllTagList(tagsList, notificationTitle, msgTitle, msgContent, extraKey, extrasparam);log.info("" + pushPayload);PushResult pushResult = jPushClient.sendPush(pushPayload);log.info("" + pushResult);if (pushResult.getResponseCode() == SUCCESS_CODE) {result = 1;}} catch (APIConnectionException e) {e.printStackTrace();} catch (APIRequestException e) {e.printStackTrace();}return result;}/*** 发送给所有安卓用户 @Description:** @param jPushClient* @param title* @param alert* @return* @throws*/public static PushResult sendToAllAndroid(JPushClient jPushClient, String title, String alert) {int result = 0;PushResult pushResult = null;try {PushPayload pushPayload = buildPushObjectAndroid(title, alert);log.info("" + pushPayload);pushResult = jPushClient.sendPush(pushPayload);log.info("" + pushResult);if (pushResult.getResponseCode() == SUCCESS_CODE) {result = 1;}} catch (Exception e) {e.printStackTrace();}return pushResult;}/*** 发送给所有IOS用户** @param msgContent 消息内容* @param extrasparam 扩展字段* @return 0推送失败,1推送成功*/public static int sendToAllIos(JPushClient jPushClient,String notificationTitle,String msgTitle,String msgContent,String extraKey,String extrasparam) {int result = 0;try {PushPayload pushPayload =buildPushObjectIos(notificationTitle, msgTitle, msgContent, extraKey, extrasparam);log.info("" + pushPayload);PushResult pushResult = jPushClient.sendPush(pushPayload);log.info("" + pushResult);if (pushResult.getResponseCode() == SUCCESS_CODE) {result = 1;}} catch (Exception e) {e.printStackTrace();}return result;}/*** 推送给设备标识参数的用户 @Description:** @param jPushClient* @param registrationId* @param notificationTitle* @param msgTitle* @param msgContent* @param extraKey* @param extrasparam* @return* @throws*/public static int sendToRegistrationId(JPushClient jPushClient,String registrationId,String notificationTitle,String msgTitle,String msgContent,String extraKey,String extrasparam) {int result = 0;try {PushPayload pushPayload =buildPushRegistrationId(registrationId, notificationTitle, msgTitle, msgContent, extraKey, extrasparam);log.info("" + pushPayload);PushResult pushResult = jPushClient.sendPush(pushPayload);log.info("" + pushResult);if (pushResult.getResponseCode() == SUCCESS_CODE) {result = 1;}} catch (Exception e) {e.printStackTrace();}return result;}/*** 推送给设备标识参数的用户 @Description:** @param registrationId* @param notificationTitle* @param msgTitle* @param msgContent* @param extraKey* @param extrasparam* @return* @throws*/private static PushPayload buildPushRegistrationId(String registrationId,String notificationTitle,String msgTitle,String msgContent,String extraKey,String extrasparam) {log.info("----------向所有平台单个或多个指定别名用户推送消息中......");// 创建一个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(registrationId))// jpush的通知,android的由jpush直接下发,iOS的由apns服务器下发,Winphone的由mpns下发.setNotification(Notification.newBuilder()// 指定当前推送的android通知.addPlatformNotification(AndroidNotification.newBuilder().setAlert(notificationTitle).setTitle(notificationTitle)// 此字段为透传字段,不会显示在通知栏。用户可以通过此字段来做一些定制需求,如特定的key传要指定跳转的页面(value).addExtra(extraKey, extrasparam).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(extraKey, extrasparam)// 此项说明此推送是一个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(extraKey, extrasparam).build()).setOptions(Options.newBuilder()// 此字段的值是用来指定本推送要推送的apns环境,false表示开发,true表示生产;对android和自定义消息无意义.setApnsProduction(APNS_PRODUCTION)// 此字段是给开发者自己给推送编号,方便推送者分辨推送记录.setSendno(1)// 此字段的值是用来指定本推送的离线保存时长,如果不传此字段则默认保存一天,最多指定保留十天;.setTimeToLive(86400).build()).build();}/*** 向所有平台单个或多个指定别名用户推送消息* 消息体封装** @param aliasList* @param notificationTitle* @param msgTitle* @param msgContent* @param extraKey* @param extrasparam* @return* @throws*/private static PushPayload buildPushObjectAllAliasList(List<String> aliasList,String notificationTitle,String msgTitle,String msgContent,String extraKey,String extrasparam) {log.info("----------向所有平台单个或多个指定别名用户推送消息中......");// 创建一个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(msgTitle)// 此字段为透传字段,不会显示在通知栏。用户可以通过此字段来做一些定制需求,如特定的key传要指定跳转的页面(value).addExtra(extraKey, extrasparam).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(extraKey, extrasparam)// 此项说明此推送是一个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(extraKey, extrasparam).build()).setOptions(Options.newBuilder()// 此字段的值是用来指定本推送要推送的apns环境,false表示开发,true表示生产;对android和自定义消息无意义.setApnsProduction(APNS_PRODUCTION)// 此字段是给开发者自己给推送编号,方便推送者分辨推送记录.setSendno(1)// 此字段的值是用来指定本推送的离线保存时长,如果不传此字段则默认保存一天,最多指定保留十天;.setTimeToLive(86400).build()).build();}/*** 向所有平台单个或多个指定别名用户推送消息* 消息体封装** @param aliasList* @param notificationTitle* @param msgTitle* @param msgContent* @param extraKey* @param extrasparam* @param countParam* @return* @throws*/private static PushPayload buildPushObjectAllAliasList1(List<String> aliasList,String notificationTitle,String msgTitle,String msgContent,String extraKey,String extrasparam,String countKey,String countParam) {log.info("----------向所有平台单个或多个指定别名用户推送消息中......");// 创建一个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(msgTitle)// 此字段为透传字段,不会显示在通知栏。用户可以通过此字段来做一些定制需求,如特定的key传要指定跳转的页面(value).addExtra(extraKey, extrasparam).addExtra(countKey, countParam).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(extraKey, extrasparam).addExtra(countKey, countParam)// 此项说明此推送是一个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(extraKey, extrasparam).addExtra(countKey, countParam).build()).setOptions(Options.newBuilder()// 此字段的值是用来指定本推送要推送的apns环境,false表示开发,true表示生产;对android和自定义消息无意义.setApnsProduction(APNS_PRODUCTION)// 此字段是给开发者自己给推送编号,方便推送者分辨推送记录.setSendno(1)// 此字段的值是用来指定本推送的离线保存时长,如果不传此字段则默认保存一天,最多指定保留十天;.setTimeToLive(86400).build()).build();}/*** 向所有平台所有用户推送消息* 消息体封装** @param notificationTitle* @param msgTitle* @param msgContent* @param extraKey* @param extrasparam* @return* @throws*/public static PushPayload buildPushObject(String notificationTitle,String msgTitle,String msgContent,String extraKey,String extrasparam) {log.info("----------向所有平台所有用户推送消息中......");return PushPayload.newBuilder().setPlatform(Platform.android_ios()).setAudience(Audience.all()).setNotification(Notification.newBuilder().addPlatformNotification(AndroidNotification.newBuilder().setAlert(notificationTitle).setTitle(notificationTitle)// 此字段为透传字段,不会显示在通知栏。用户可以通过此字段来做一些定制需求,如特定的key传要指定跳转的页面(value).addExtra(extraKey, extrasparam).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(extraKey, extrasparam)// 此项说明此推送是一个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(extraKey, extrasparam).build()).setOptions(Options.newBuilder()// 此字段的值是用来指定本推送要推送的apns环境,false表示开发,true表示生产;对android和自定义消息无意义.setApnsProduction(APNS_PRODUCTION)// 此字段是给开发者自己给推送编号,方便推送者分辨推送记录.setSendno(1)// 此字段的值是用来指定本推送的离线保存时长,如果不传此字段则默认保存一天,最多指定保留十天,单位为秒.setTimeToLive(86400).build()).build();}/*** 向所有平台单个或多个指定Tag用户推送消息* 消息体封装** @param tagsList* @param notificationTitle* @param msgTitle* @param msgContent* @param extraKey* @param extrasparam* @return* @throws*/private static PushPayload buildPushObjectAllTagList(List<String> tagsList,String notificationTitle,String msgTitle,String msgContent,String extraKey,String extrasparam) {log.info("----------向所有平台单个或多个指定Tag用户推送消息中.......");// 创建一个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(extraKey, extrasparam).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(extraKey, extrasparam)// 此项说明此推送是一个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(extraKey, extrasparam).build()).setOptions(Options.newBuilder()// 此字段的值是用来指定本推送要推送的apns环境,false表示开发,true表示生产;对android和自定义消息无意义.setApnsProduction(APNS_PRODUCTION)// 此字段是给开发者自己给推送编号,方便推送者分辨推送记录.setSendno(1)// 此字段的值是用来指定本推送的离线保存时长,如果不传此字段则默认保存一天,最多指定保留十天;.setTimeToLive(86400).build()).build();}/*** 向android平台所有用户推送* 消息体封装** @param title 通知标题* @param alert 通知内容* @return*/private static PushPayload buildPushObjectAndroid(String title, String alert) {log.info("----------向android平台所有用户推送消息中......");JSONArray array = new JSONArray();array.add("西安");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(title).setTitle(title).setAlert(alert)// 此字段为透传字段,不会显示在通知栏。用户可以通过此字段来做一些定制需求,如特定的key传要指定跳转的页面(value)//                                        .addExtra(extraKey, extrasparam).build()).build())// Platform指定了哪些平台就会像指定平台中符合推送条件的设备进行推送。 jpush的自定义消息,// sdk默认不做任何处理,不会有通知提示。建议看文档http://docs.jpush.io/guideline/faq/的// [通知与自定义消息有什么区别?]了解通知和自定义消息的区别//              .setMessage(Message.newBuilder().setMsgContent(msgContent).setTitle(msgTitle)//                     .addExtra(extraKey, extrasparam).build()).setOptions(Options.newBuilder()// 此字段的值是用来指定本推送要推送的apns环境,false表示开发,true表示生产;对android和自定义消息无意义.setApnsProduction(APNS_PRODUCTION)// 此字段是给开发者自己给推送编号,方便推送者分辨推送记录.setSendno(1)// 此字段的值是用来指定本推送的离线保存时长,如果不传此字段则默认保存一天,最多指定保留十天,单位为秒.setTimeToLive(86400).build()).build();}/*** 向ios平台所有用户推送消息 @Description:** @param notificationTitle* @param msgTitle* @param msgContent* @param extraKey* @param extrasparam* @return* @throws*/private static PushPayload buildPushObjectIos(String notificationTitle,String msgTitle,String msgContent,String extraKey,String extrasparam) {log.info("----------向ios平台所有用户推送消息中.......");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(extraKey, extrasparam)// 此项说明此推送是一个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(extraKey, extrasparam).build()).setOptions(Options.newBuilder()// 此字段的值是用来指定本推送要推送的apns环境,false表示开发,true表示生产;对android和自定义消息无意义.setApnsProduction(APNS_PRODUCTION)// 此字段是给开发者自己给推送编号,方便推送者分辨推送记录.setSendno(1)// 此字段的值是用来指定本推送的离线保存时长,如果不传此字段则默认保存一天,最多指定保留十天,单位为秒.setTimeToLive(86400).build()).build();}
}

工具类封装总结:

所有的封装都是围绕 极光推送 入参对象 PushPayload 来进行的,根据不同的推送业务封装不同的应用场景,最终都是调用同一个接口进行推送的:

jPushClient.sendPush(pushPayload);

具体的PushPayload 对象的结构文档URL如下:

极光文档 (jiguang.cn)

5、业务从应用

package com.service.msg.biz.jiguang.service.impl;import cn.jpush.api.push.PushResult;
import javax.annotation.Resource;
import jsh.mg.msg.service.msg.biz.jiguang.dto.NotificationVo;
import jsh.mg.msg.service.msg.biz.jiguang.dto.ReverseBandingParamsDto;
import jsh.mg.msg.service.msg.biz.jiguang.dto.UploadBandingParamsDto;
import jsh.mg.msg.service.msg.biz.jiguang.service.JiguangService;
import jsh.mg.msg.service.msg.config.jpush.JpsuhConfig;
import jsh.mg.msg.service.msg.utils.JpushUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;/*** 极光推送service实现.** @author ***** @since 2023/01/12 17:16*/
@Service
public class JiguangServiceImpl implements JiguangService {private static final Logger log = LoggerFactory.getLogger(JiguangServiceImpl.class);@Resource private JpsuhConfig jpsuhConfig;@Overridepublic PushResult sendMsg(NotificationVo notificationVo) {PushResult pushResult =JpushUtil.sendToAllAndroid(jpsuhConfig.createClient(), notificationVo.getTitle(), notificationVo.getAlert());if (pushResult.getResponseCode() == 200) {log.info("推送成功{}" + notificationVo.getTitle() + notificationVo.getAlert());}return pushResult;}}

通过 main 方法调用触发接口即可完成推送,注意入参格式

以上是极光推送后台基本推送环境搭建完成

二、移动端APP应用的创建

1、在极光推送官网创建新的APP应用

URL:极光开发者服务 (jiguang.cn)

第一步:进入创建应用

第二步:基本信息维护

第三步:服务集成

第四步:维护应用的包名

第四步:下载应用到手机

第五步:把生成的应用的 AppKey 和 Master Secret 维护到后台的配置文件中即可

以上是安卓手机应用的创建

2、创建苹果手机应用

第一步:创建集成服务应用

到这里,就要在苹果电脑生成文件,辅助操作,咱不是做苹果应用开发的,就说到这里

辅助文档URL:Apple iOS推送证书配置和生成教程 - 简书 (jianshu.com)

参照以上文档操作,需要有iOS开发者账号才行的

截止现在,调用后台服务接口,就可以可以给安全应用推送消息了

分享到这里就结束了,希望可以帮到你

极光推送零基础极速上手开发指南,快速搭建后台推送服务相关推荐

  1. 零基础免费通过hexo+github快速搭建个人博客(超详细图解+B站视频讲解资源)

    我的个人博客效果预览​https://furfur-jiang.github.io/ 加载可能会比较慢,因为是挂载到github上的 样式基于hexo-theme-matery主题 ,这个主题我特别喜 ...

  2. 尚硅谷Java零基础极速入门七天版笔记

    Java零基础极速入门 文章目录 Java零基础极速入门 1 Java快速入门 1.1计算机语言 1.2 Java语言 1.3 JVM 1.4 环境配置 2 基础语法 2.1 变量 2.2 标识符 2 ...

  3. 【视频回放与课件】零基础入门AI开发

    今天上午,受广州图书馆邀请,在第一讲<零代码上手人工智能>的基础上,以<零基础入门AI开发>为主题,分四步解锁人工智能学习的概念与开发工具,让您在一小时内轻松掌握人工智能开发要 ...

  4. r matlab spss,特别放送 | 零基础编程入门:Python、Matlab、R、SPSS资料大放送

    原标题:特别放送 | 零基础编程入门:Python.Matlab.R.SPSS资料大放送 我们一直相信: 一切不能实实在在帮助到同学的资料 都是耍流氓 小助手的目标是: 做一次又一次真正有价值.愉悦感 ...

  5. 零基础学ios开发培训要培训多久

    零基础学ios开发培训要培训多久     想参加ios开发培训,不知道ios开发培训需要多长时间能学会呢?零基础学ios培训要培训多久?学完IOS技术课程后是否可以快速找到高薪工作,一系列的问题都是菜 ...

  6. 零基础学python需要多久-零基础学习Python开发需要多长时间?

    原标题:零基础学习Python开发需要多长时间? Python开发技术目前可谓是互联网行业编程界的新宠,不少零基础想要进入IT行业的人员都纷纷加入学习Python开发.零基础学习Python开发技术需 ...

  7. 自学python需要多长时间-零基础学习Python开发需要多长时间?

    原标题:零基础学习Python开发需要多长时间? Python开发技术目前可谓是互联网行业编程界的新宠,不少零基础想要进入IT行业的人员都纷纷加入学习Python开发.零基础学习Python开发技术需 ...

  8. python零基础实例-零基础学习Python开发练习100题实例(1)

    零基础学习Python开发练习100题实例(1) 2018-02-25 09:37:59 2864浏览 1.题目:有四个数字:1.2.3.4,能组成多少个互不相同且无重复数字的三位数?各是多少? 程序 ...

  9. python基础代码事例-零基础学习Python开发练习100题实例(2)

    零基础学习Python开发练习100题实例(2) 2018-02-26 13:11:39 1934浏览 11.题目:古典问题:有一对兔子,从出生后第3个月起每个月都生一对兔子,小兔子长到第三个月后每个 ...

最新文章

  1. 人脸识别是怎么识别人脸的?
  2. 1.5 关于这门课-深度学习-Stanford吴恩达教授
  3. idea maven 创建webapp项目没有src目录
  4. oracle rac standby,oracle RAC数据库建立STANDBY(二)
  5. android mysql代码_LitePal——Android数据库框架完整使用手册(示例代码)
  6. Python闭包装饰器笔记
  7. 即插即用 | 超越CBAM,全新注意力机制,GAM不计成本提高精度(附Pytorch实现)...
  8. 热烈庆祝排名进入3000!
  9. linux串口结构termios,struct termios结构体—Linux串口.doc
  10. vs 2010 专业版 密钥
  11. js去空格 回车 制表符 换页符
  12. Excel 里筛选手机号码所属运营商
  13. 14期《未来,我来》1月刊
  14. CTF常用脚本工具(附下载地址)
  15. 金九银十的你准备好了吗?Python 100道基础面试题先收藏!【附答案】
  16. win系统下制作OS X(黑苹果)系统安装U盘制作的2个方法
  17. word题注“一-1”改为“1-1”
  18. 编程语言排行榜没有html,TIOBE:2019年12月全球编程语言排行榜
  19. 如何在 JavaScript 中获取当前日期?
  20. markdown语法最全汇总

热门文章

  1. MySQL - 索引类型详解
  2. 终身学习(LifeLong Learning)/ 增量学习(Incremental Learning)、在线学习(Online Learning)
  3. 新玺配资:周五大盘弱势反弹
  4. nginx下增加https端口的方法
  5. 用python封装一个学生类
  6. 《五分钟商学院》管理篇学习笔记
  7. 扫地机器人的回充方法实现
  8. boolean类型和int类型
  9. 【Matlab】 MATLAB代码速览
  10. NOI-1.4(07) 收集瓶盖赢大奖