java 整合極光推送
- maven依賴
<!--jPush-->
<dependency>
<groupId>cn.jpush.api</groupId>
<artifactId>jpush-client</artifactId>
<version>3.2.17</version>
</dependency>
<dependency>
<groupId>cn.jpush.api</groupId>
<artifactId>jiguang-common</artifactId>
<version>1.0.3</version>
</dependency>
<!--jPush 極光推送 end -->
- 工具類
package com.laituo.juyou.app.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.IosAlert;
import cn.jpush.api.push.model.notification.IosNotification;
import cn.jpush.api.push.model.notification.Notification;
import org.springframework.beans.factory.annotation.Value;
import java.util.List;
/**
* 極光推送工具類
* @auth ywh
* @Date 2021/4/12
*/
public class JPushUtils {
/**
* 極光平臺申請的 appKey 和 masterSecret
*/
@Value("${com.jpush.appKey}")
private String APP_KEY;
@Value("${com.jpush.masterSecrt}")
private String MASTER_SECRET;
//創建JPushClient(極光推送的實體)
private final JPushClient jPushClient = new JPushClient(this.MASTER_SECRET,this.APP_KEY);
public static void main(String[] args) {
try {
//別名推送示例
JPushClient jcTest = new JPushClient("你的MASTER_SECRET","你的APP_KEY");
JPushVo jv = new JPushVo();
jv.setExtraParam("隱藏引數");
jv.setMsgContent("訊息內容測驗");
jv.setMsgTitle("=====訊息標題=====");
jv.setNotificationTitle("通知頭");
jv.setAlias("4960");//別名-對應平臺userId
PushPayload pushPayload = buildPushAliasAlertTitle(jv);
System.out.println("構建訊息通知列印:"+pushPayload);
PushResult pushResult = jcTest.sendPush(pushPayload);
System.out.println("通知回傳結果列印:"+pushResult);
if (pushResult.getResponseCode() == 200) {
System.out.println("=================SUCCESS=================");
}
} catch(Exception e){
e.printStackTrace();
System.out.println("=================ERROR=================");
}
System.out.println("=================結束=================");
}
/**
* 極光推送
* @param JPushVo 發送資訊自定義物件
* @return 發送是否成功 boolean
*/
public boolean sendToMessage(JPushVo JPushVo) {
boolean result = false;
try {
PushPayload pushPayload = buildPushAllRegistrationIdAlertTitle(JPushVo);
System.out.println(pushPayload);
PushResult pushResult = this.jPushClient.sendPush(pushPayload);
System.out.println(pushResult);
if (pushResult.getResponseCode() == 200) {
result = true;
}
} catch (APIConnectionException | APIRequestException e) {
e.printStackTrace();
}
return result;
}
/**
* 構建發送訊息
* @param JPushVo 發送資訊
*/
private static PushPayload buildPushAllRegistrationIdAlertTitle(JPushVo JPushVo) {
List<String> registrationIds = JPushVo.getRegistrationIds();
String notificationTitle = JPushVo.getNotificationTitle();
String msgTitle = JPushVo.getMsgTitle();
String msgContent = JPushVo.getMsgContent();
String extraParam = JPushVo.getExtraParam();
//ios標題、二級標題、內容區分
IosAlert iosAlert = IosAlert.newBuilder()
.setTitleAndBody(msgTitle, "subTitle測驗",msgContent)
.build();
// 創建一個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.all()) //代表所有人 1
//.setAudience(Audience.alias(alias)) //別名推送 2
.setAudience(Audience.registrationId(registrationIds))//指定registrationId集合 3
// jpush的通知,android的由jpush直接下發,iOS的由apns服務器下發,Winphone的由mpns下發
.setNotification(Notification.newBuilder()
// 指定當前推送的android通知
.addPlatformNotification(AndroidNotification.newBuilder()
.setAlert(msgContent)
.setTitle(notificationTitle)
// 此欄位為透傳欄位,不會顯示在通知欄,用戶可以通過此欄位來做一些定制需求,如特定的key傳要指定跳轉的頁面(value)
.addExtra("extraParam", extraParam)
.build())
// 指定當前推送的iOS通知
.addPlatformNotification(IosNotification.newBuilder()
// 傳一個IosAlert物件,指定title、subTitle、content等
.setAlert(iosAlert)
// 直接傳alert
// 此項是指定此推送的badge自動加1
// .incrBadge(1)
// 此欄位的值default表示系統默認聲音;傳sound.caf表示此推送以專案里面打包的sound.caf聲音來提醒,
// 如果系統沒有此音頻則以系統默認聲音提醒;此欄位如果傳空字串,iOS9及以上的系統是無聲音提醒,以下的系統是默認聲音
// .setSound("sound.caf")
// 此欄位為透傳欄位,不會顯示在通知欄,用戶可以通過此欄位來做一些定制需求,如特定的key傳要指定跳轉的頁面(value)
//.addExtras()//可傳多個值,鍵值對方式 Map<String,String>
.addExtra("extraParam", extraParam)
.build())
// 此項說明此推送是一個background推送,想了解background看:http://docs.jpush.io/client/ios_tutorials/#ios-7-background-remote-notification
// 取消此注釋,訊息推送時ios將無法在鎖屏情況接收
// .setContentAvailable(true)
.build())
// Platform指定了哪些平臺就會像指定平臺中符合推送條件的設備進行推送, jpush的自定義訊息,
// sdk默認不做任何處理,不會有通知提示,建議看檔案http://docs.jpush.io/guideline/faq/的
// [通知與自定義訊息有什么區別?]了解通知和自定義訊息的區別
.setMessage(Message.newBuilder()
.setMsgContent(msgContent)
.setTitle(msgTitle)
.addExtra("extraParam", extraParam)
.build())
.setOptions(Options.newBuilder()
// 此欄位的值是用來指定本推送要推送的apns環境,false表示開發,true表示生產;對android和自定義訊息無意義
.setApnsProduction(false)
// 此欄位是給開發者自己給推送編號,方便推送者分辨推送記錄
.setSendno(100001)
// 此欄位的值是用來指定本推送的離線保存時長,如果不傳此欄位則默認保存一天,最多指定保留十天;
.setTimeToLive(86400)
.build())
.build();
}
}
JPushVo 為自定義物體
import lombok.Data;
import java.util.List;
/**
* @auth ywh
* @Date 2021/4/12
*/
@Data
public class JPushVo {
//發送方式1:發送人員集合 registrationId
private List<String> registrationIds;
//發送方式2:別名 - 用userId做別名 (方便使用)
private String alias;
//發送方式3:標簽 - 暫時不用
private String tag;
//通知標題
private String notificationTitle;
//訊息標題
private String msgTitle;
//訊息內容
private String msgContent;
//隱藏引數、不面向用戶
private String extraParam;
}
參考鏈接(點我)
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/277059.html
標籤:其他
