日日操夜夜添-日日操影院-日日草夜夜操-日日干干-精品一区二区三区波多野结衣-精品一区二区三区高清免费不卡

公告:魔扣目錄網為廣大站長提供免費收錄網站服務,提交前請做好本站友鏈:【 網站目錄:http://www.ylptlb.cn 】, 免友鏈快審服務(50元/站),

點擊這里在線咨詢客服
新站提交
  • 網站:51998
  • 待審:31
  • 小程序:12
  • 文章:1030137
  • 會員:747

pom.xml

<!-- 極光推送 begin -->
<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.4</version>
</dependency>

Application.yml

jpush:
 appKey: xxx
 masterSecret: xxxx
 apnsProduction: false # 是否生成環境,true表示生成環境

MyJPushClient

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 org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import JAVA.util.List;
/**
 * 極光推送客戶端
 *
 * @author Mengday Zhang
 * @version 1.0
 * @since 2019-04-01
 */
@Component
public class MyJPushClient {
 @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;
 private static final Logger logger = LoggerFactory.getLogger(MyJPushClient.class);
 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<String> alias, String notificationTitle, String msgTitle, String msgContent, String extras) {
 PushPayload pushPayload = buildPushObject_all_aliasList_alertWithTitle(alias, notificationTitle, msgTitle, msgContent, extras);
 this.sendPush(pushPayload);
 }
 /**
 * 推送到tag列表
 *
 * @param tagsList Tag或Tag組
 * @param notificationTitle 通知內容標題
 * @param msgTitle 消息內容標題
 * @param msgContent 消息內容
 * @param extras 擴展字段
 */
 public void sendToTagsList(List<String> 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<String> 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<String> 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();
 }
 /**
 * 向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();
 }
 public static void main(String[] args) {
// MyJPushClient jPushUtil = new MyJPushClient();
// List<String> aliasList = Arrays.asList("239");
// String notificationTitle = "notificationTitle";
// String msgTitle = "msgTitle";
// String msgContent = "msgContent";
// jPushUtil.sendToAliasList(aliasList, notificationTitle, msgTitle, msgContent, "exts");
 }
}

test

@RunWith(SpringRunner.class)
@SpringBootTest
public class JPushApplicationTests {
 @Autowired
 private MyJPushClient jPushClient;
 @Test
 public void testJPush() {
 List<String> aliasList = Arrays.asList("239");
 String notificationTitle = "notification_title";
 String msgTitle = "msg_title";
 String msgContent = "msg_content";
 jPushClient.sendToAliasList(aliasList, notificationTitle, msgTitle, msgContent, "exts");
 }
}
(建議收藏) | Spring Boot集成極光推送Java-SDK

 

分享到:
標簽:Spring Boot
用戶無頭像

網友整理

注冊時間:

網站:5 個   小程序:0 個  文章:12 篇

  • 51998

    網站

  • 12

    小程序

  • 1030137

    文章

  • 747

    會員

趕快注冊賬號,推廣您的網站吧!
最新入駐小程序

數獨大挑戰2018-06-03

數獨一種數學游戲,玩家需要根據9

答題星2018-06-03

您可以通過答題星輕松地創建試卷

全階人生考試2018-06-03

各種考試題,題庫,初中,高中,大學四六

運動步數有氧達人2018-06-03

記錄運動步數,積累氧氣值。還可偷

每日養生app2018-06-03

每日養生,天天健康

體育訓練成績評定2018-06-03

通用課目體育訓練成績評定