|
|
@@ -0,0 +1,100 @@
|
|
|
+package com.nb.app.assistant.utils;
|
|
|
+
|
|
|
+import cn.hutool.core.io.FileUtil;
|
|
|
+import com.turo.pushy.apns.*;
|
|
|
+import com.turo.pushy.apns.auth.ApnsSigningKey;
|
|
|
+import com.turo.pushy.apns.util.SimpleApnsPushNotification;
|
|
|
+import com.turo.pushy.apns.util.concurrent.PushNotificationFuture;
|
|
|
+import io.netty.channel.EventLoopGroup;
|
|
|
+import io.netty.channel.nio.NioEventLoopGroup;
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
+
|
|
|
+import java.io.File;
|
|
|
+import java.util.Date;
|
|
|
+import java.util.UUID;
|
|
|
+
|
|
|
+import static com.turo.pushy.apns.PushType.*;
|
|
|
+import static com.turo.pushy.apns.PushType.ALERT;
|
|
|
+
|
|
|
+
|
|
|
+@Slf4j
|
|
|
+public class ApnsUtils {
|
|
|
+ private static ApnsClient apnsClient = null;
|
|
|
+ public static void sendMsg(String deviceToken,String msgContent) throws Exception {
|
|
|
+ //IOS等终端设备注册后返回的DeviceToken
|
|
|
+ /**
|
|
|
+ * Use the voip push type for notifications that provide information about an incoming Voice-over-IP (VoIP)
|
|
|
+ * call. For more information, see Responding to VoIP Notifications from PushKit.
|
|
|
+ * If you set this push type, the apns-topic header field must use your app’s bundle ID with .voip
|
|
|
+ * appended to the end. If you’re using certificate-based authentication,
|
|
|
+ * you must also register the certificate for VoIP services.
|
|
|
+ * The topic is then part of the 1.2.840.113635.100.6.3.4 or 1.2.840.113635.100.6.3.6 extension.
|
|
|
+ */
|
|
|
+ //这是你的主题,大多数情况是bundleId,voip需要在bundleId加上.voip。对应文档中的apns-topic
|
|
|
+ //此处可以参考https://developer.apple.com/documentation/usernotifications/setting_up_a_remote_notification_server/sending_notification_requests_to_apns?language=objc
|
|
|
+
|
|
|
+ String topic = "tuoren.PumpIOS";
|
|
|
+ String payload = "{ \n" +
|
|
|
+ " \"aps\":{ \n" +
|
|
|
+ " \"alert\":{ \n" +
|
|
|
+ " \"title\":\"驼人镇痛泵报警\",\n" +
|
|
|
+ " \"subtitle\":\"75415123E11248\",\n" +
|
|
|
+ " \"body\":\"故障报警\"\n" +
|
|
|
+ " },\n" +
|
|
|
+ " \"badge\":1, \n" +
|
|
|
+ " \"sound\":\"default\", \n" +
|
|
|
+ " }\n" +
|
|
|
+ "}\n" ;
|
|
|
+ //有效时间
|
|
|
+ Date invalidationTime= new Date(System.currentTimeMillis() + 60 * 60 * 1000L );
|
|
|
+ //发送策略 apns-priority 10为立即 5为省电
|
|
|
+ DeliveryPriority priority= DeliveryPriority.IMMEDIATE;
|
|
|
+ //推送方式,主要有alert,background,voip,complication,fileprovider,mdm
|
|
|
+ PushType pushType = ALERT;
|
|
|
+ //推送的合并ID,相同的 apns-collapse-id会在App中合并
|
|
|
+ String collapseId= UUID.randomUUID().toString();
|
|
|
+ //apnsId 唯一标示,如果不传,APNs会给我们生成一个
|
|
|
+ UUID apnsId = UUID.randomUUID();
|
|
|
+ //构造一个APNs的推送消息实体
|
|
|
+
|
|
|
+ SimpleApnsPushNotification msg = new SimpleApnsPushNotification(deviceToken,topic,payload,invalidationTime,priority,pushType,collapseId,apnsId);
|
|
|
+ //开始推送
|
|
|
+ PushNotificationFuture<SimpleApnsPushNotification, PushNotificationResponse<SimpleApnsPushNotification>> future = getAPNSConnect().sendNotification(msg);
|
|
|
+ PushNotificationResponse<SimpleApnsPushNotification> response = future.get();
|
|
|
+ System.out.println(response.getRejectionReason());
|
|
|
+ //如果返回的消息中success为true那么成功,否则失败!
|
|
|
+ //如果失败不必惊慌,rejectionReason字段中会有失败的原因。对应官网找到原因即可
|
|
|
+ //https://developer.apple.com/documentation/usernotifications/setting_up_a_remote_notification_server/handling_notification_responses_from_apns?language=objc
|
|
|
+
|
|
|
+ System.out.println("------------->"+response);
|
|
|
+
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ public static ApnsClient getAPNSConnect() {
|
|
|
+
|
|
|
+ if (apnsClient == null) {
|
|
|
+ try {
|
|
|
+ //四个线程
|
|
|
+ File file = FileUtil.file("AuthKey_X4S8H58U59.p8");
|
|
|
+
|
|
|
+ EventLoopGroup eventLoopGroup = new NioEventLoopGroup(4);
|
|
|
+ apnsClient = new ApnsClientBuilder()
|
|
|
+ .setApnsServer(ApnsClientBuilder.DEVELOPMENT_APNS_HOST)
|
|
|
+ .setSigningKey(ApnsSigningKey.loadFromPkcs8File(file,
|
|
|
+ "JRWKVG95GL", "X4S8H58U59"))
|
|
|
+ .setConcurrentConnections(4)
|
|
|
+ .setEventLoopGroup(eventLoopGroup)
|
|
|
+ .build();
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.error("ios get pushy apns client failed!");
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return apnsClient;
|
|
|
+ }
|
|
|
+
|
|
|
+ public static void main(String[] args) throws Exception {
|
|
|
+ sendMsg("ad8b799839398e10d78c55041dbbe6483563cf75428e2ebe21162a75f83598b2","");
|
|
|
+ }
|
|
|
+}
|