基于SpringBoot给苹果消息通知服务器APNS发送消息通知
一.导入依赖/jar包
< dependency>
< groupId> com.eatthepath</ groupId>
< artifactId> pushy</ artifactId>
< version> 0.15.0</ version>
</ dependency>
< dependency>
< groupId> com.notnoop.apns</ groupId>
< artifactId> apns</ artifactId>
< version> 1.0.0.Beta6</ version>
</ dependency>
二.发送消息通知工具类
@Slf4j
@Component
public class APNsUtil {
private static ApnsClient apnsClient = null ;
private static final String p12Password = "xinao111222" ;
public static PushNotificationResponse < SimpleApnsPushNotification > ApplePush ( String deviceToken, String topic, String alert, int badge, Long time, DeliveryPriority priority, PushType pushType) throws ExecutionException , InterruptedException {
Date invalidationTime = new Date ( System . currentTimeMillis ( ) + time) ;
Instant instant = invalidationTime. toInstant ( ) ;
String payload= "{\"aps\":{\"alert\":\"" + alert + "\",\"badge\":" + badge + ",\"sound\": \"default\"}}" ;
SimpleApnsPushNotification msg = new SimpleApnsPushNotification ( deviceToken, topic, payload, instant, priority, pushType) ;
PushNotificationFuture < SimpleApnsPushNotification , PushNotificationResponse < SimpleApnsPushNotification > > future = getAPNSConnect ( ) . sendNotification ( msg) ;
PushNotificationResponse < SimpleApnsPushNotification > response = future. get ( ) ;
return response;
}
public static ApnsClient getAPNSConnect ( ) {
if ( apnsClient == null ) {
try {
EventLoopGroup eventLoopGroup = new NioEventLoopGroup ( 4 ) ;
String resourceLocation = null ;
if ( "prod" . equals ( SysProperties . getEnvironment ( ) ) ) {
resourceLocation = "/home/appadmin/enn/xinaoAPNS.p12" ;
}
if ( "test" . equals ( SysProperties . getEnvironment ( ) ) ) {
resourceLocation = "/data/lipei/web/app/zzfw/enn/xinaoAPNS.p12" ;
File file = new File ( "src/main/resources/template/xinaoAPNS.p12" ) ;
if ( file. exists ( ) ) {
resourceLocation = ResourceUtils . CLASSPATH_URL_PREFIX + "template/xinaoAPNS.p12" ;
}
}
File file = ResourceUtils . getFile ( resourceLocation) ;
apnsClient = new ApnsClientBuilder ( )
. setApnsServer ( ApnsClientBuilder . PRODUCTION_APNS_HOST)
. setClientCredentials ( file, p12Password)
. setConcurrentConnections ( 4 )
. setEventLoopGroup ( eventLoopGroup) . build ( ) ;
} catch ( Exception e) {
e. printStackTrace ( ) ;
log. error ( "Get IOS Connect Exception, Please Check:{}" , e. getMessage ( ) ) ;
}
}
return apnsClient;
}
public static void sendIosMsg ( String deviceToken, String msgBody, int badge) {
PushNotificationResponse < SimpleApnsPushNotification > response = null ;
try {
response = APNsUtil. ApplePush ( deviceToken, "cn.enn.epei" , msgBody, badge, 60 * 1000L , DeliveryPriority . IMMEDIATE, PushType . ALERT) ;
} catch ( Exception e) {
e. printStackTrace ( ) ;
log. error ( "Ios Send Msg Fail{}" , e. getMessage ( ) ) ;
return ;
}
log. info ( "执行结果:{}" , response. getRejectionReason ( ) ) ;
System . err. println ( "如果返回的消息中success为true那么成功,否则失败!" + response) ;
log. info ( "如果返回的消息中success为true那么成功,否则失败!{}" , JSON. toJSONString ( response) ) ;
}
}