java服务端voip实现_JAVA后台对接苹果APNS(VOIP)实现推送

本文展示了如何使用Java服务端实现VOIP类型的Apple Push Notification Service (APNS) 推送。通过创建`ApnsUtils`类,设置设备Token、推送主题、payload等内容,利用ApnsClient进行推送,并处理推送响应,包括错误检查和原因解析。
摘要由CSDN通过智能技术生成

@Slf4j

public class APNsUtils {

private static ApnsClient apnsClient = null;

public static void main(String[] args) throws Exception {

//IOS等终端设备注册后返回的DeviceToken

String deviceToken ="5d7cf67dd6ab56c6d699f86806682e7d99e019216df734f4c57196b84790b718";

// String deviceToken = "74abd7d51c58a8db995fa53c3508a72481a9d4ad56e7ed1f7d12362f798a6906";

/**

* 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 = "com.voip.test.voip";

String payload = "{ \"aps\" : {\"alert\" : \"测试\", \"sound\" : \"default\", \"badge\" :1},\"liguoxin\":\"liguoxin\" }";

//有效时间

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 = VOIP;

//推送的合并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> future = getAPNSConnect().sendNotification(msg);

PushNotificationResponse 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 {

/**

* Development server: api.sandbox.push.apple.com:443

* Production server: api.push.apple.com:443

*

* api.development.push.apple.com这个域名苹果官网现在找不到了

*/

String SANDBOX_APNS_HOST = "api.sandbox.push.apple.com";

// /Users/liguoxin/Desktop/p12/deve_push.p12

// /Users/liguoxin/Desktop/p12/distri_push.p12

//四个线程

EventLoopGroup eventLoopGroup = new NioEventLoopGroup(4);

apnsClient = new ApnsClientBuilder().setApnsServer(ApnsClientBuilder.DEVELOPMENT_APNS_HOST)

.setClientCredentials(new File("/Users/liguoxin/Desktop/p12/distri_push.p12"),"111111")

.setConcurrentConnections(4).setEventLoopGroup(eventLoopGroup).build();

// EventLoopGroup eventLoopGroup = new NioEventLoopGroup(4);

// apnsClient = new ApnsClientBuilder().setApnsServer(ApnsClientBuilder.DEVELOPMENT_APNS_HOST)

// .setSigningKey(ApnsSigningKey.loadFromPkcs8File(new File("/Users/liguoxin/Desktop/p12/deve_push.p8"),

// "我的temid", "我的keyid"))

// .setConcurrentConnections(4).setEventLoopGroup(eventLoopGroup).build();

} catch (Exception e) {

log.error("ios get pushy apns client failed!");

e.printStackTrace();

}

}

return apnsClient;

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值