代码实现
maven引入第三方库
<dependency>
<groupId>com.eatthepath</groupId>
<artifactId>pushy</artifactId>
<version>0.15.4</version>
</dependency>
代码示例
InputStream certificateStream;
String deviceToken = "";
try {
URL url = new URL("");
HttpURLConnection uc = (HttpURLConnection) url.openConnection();
uc.setDoInput(true);
uc.connect();
certificateStream = uc.getInputStream();
final ApnsClient apnsClient = new ApnsClientBuilder()
.setApnsServer(ApnsClientBuilder.DEVELOPMENT_APNS_HOST)
.setClientCredentials(certificateStream, "1234")
.build();
final SimpleApnsPushNotification pushNotification = new SimpleApnsPushNotification(
deviceToken,
"",
"{\"aps\":{\"alert\":\"Hello World!!!\"}}"
);
final PushNotificationFuture<SimpleApnsPushNotification, PushNotificationResponse<SimpleApnsPushNotification>>
sendNotificationFuture = apnsClient.sendNotification(pushNotification);
sendNotificationFuture.thenAccept(response -> {
System.out.println(response);
});
} catch (Exception e) {
e.printStackTrace();
}
```APNs 通知的 payload 是一个 JSON 对象,你可以在其中指定多个字段来控制通知的显示方式。对于 iOS 10 及更高版本,你通常会使用 aps 字典来包含通知的具体信息,其中包括 title、body(通过 alert 字典的 text 字段指定)以及其他可能的字段,如 sound、badge 等。
以下是一个示例 JSON payload,展示了如何指定标题和内容:
```json
{
"aps": {
"alert": {
"title": "Your Notification Title",
"body": "This is the notification body text."
},
"sound": "default",
},
}