Apple Push Notification Service(APNS)开发指引

notnoop/java-apns github:https://github.com/notnoop/java-apns

简介

java-apns是服务于APNS服务的java客户端,该库目的是提供一套面对苹果服务器的高扩展性、简单、模块化的接口。

这些接口只需要很少的代码去完成大多数的cases,同时你还可以用永自己的网络链接库或者json库重新配置java-apns库。

Links: Installation

细节:

  • 简单易用,高性能的APNS API
  • 支持Apple反馈服务
  • 支持增强的Apple Push Notification
  • 支持MDM和NewStand Notification
  • 方便读取Apple certificates
  • 方便扩展&复用
  • 容易集成在框架中
  • 自定义push负载
  • 支持pooling的链接
  • 遇错重连

Sample Code

To send a notification, you can do it in two steps:

  1. 建立连接

    ApnsService service =
        APNS.newService()
        .withCert("/path/to/certificate.p12", "MyCertPassword")
        .withSandboxDestination()
        .build();

     

  2. 创建&发送消息

    String payload = APNS.newPayload().alertBody("Can't be simpler than this!").build();
    String token = "fedfbcfb....";
    service.push(token, payload);

     

  3. 通过反馈服务获取不活动的设备列表

    Map<String, Date> inactiveDevices = service.getInactiveDevices();
    for (String deviceToken : inactiveDevices.keySet()) {
        Date inactiveAsOf = inactiveDevices.get(deviceToken);
        ...
    }

     

     

自定义发送内容

你可以发送自定义的消息,支持自定义的字段

复制代码
String payload = APNS.newPayload()
            .badge(3)
            .customField("secret", "what do you think?");
            .localizedKey("GAME_PLAY_REQUEST_FORMAT")
            .localizedArguments("Jenna", "Frank")
            .actionKey("Play").build();

service.push(token, payload);
复制代码

 

增强的Notification Push Format

复制代码
 String payload = APNS.newPayload()
            .badge(3)
            .customField("secret", "what do you think?");
            .localizedKey("GAME_PLAY_REQUEST_FORMAT")
            .localizedArguments("Jenna", "Frank")
            .actionKey("Play").build();

 EnhancedApnsNotification notification = new EnhancedApnsNotification(EnhancedApnsNotification.INCREMENT_ID() /* Next ID */,
     new Date().getTime() + 60 * 60 /* Expire in one hour */,
     token /* Device Token */,
     payload);

 service.push(notification);
复制代码
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
根据提供的引用[1],TNetHttpClient是Delphi中的一个HTTP客户端组件,而APNSApple Push Notification Service的缩写,是苹果公司提供的推送服务。因此,TNetHttpClient APNS是使用Delphi中的TNetHttpClient组件来与APNS进行通信。 使用TNetHttpClient APNS的步骤如下: 1.创建TNetHttpClient对象,并设置请求的URL和请求方法为POST。 2.设置请求头部信息,包括Content-Type、Authorization和apns-topic等信息。 3.设置请求体信息,包括设备的token、推送的payload等信息。 4.发送请求并获取响应结果。 以下是一个使用TNetHttpClient APNS发送推送通知的示例代码: ```delphi uses System.Net.HttpClient, System.Net.HttpClientComponent; procedure SendAPNSNotification; var HttpClient: TNetHttpClient; Request: TNetHTTPRequest; Response: IHTTPResponse; APNSUrl: string; APNSPayload: string; APNSToken: string; APNSAuth: string; APNSTopic: string; begin HttpClient := TNetHttpClient.Create(nil); Request := TNetHTTPRequest.Create(nil); try APNSUrl := 'https://api.push.apple.com/3/device/'; APNSPayload := '{"aps":{"alert":"Hello World!"}}'; APNSToken := 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'; APNSAuth := 'Bearer xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'; APNSTopic := 'com.example.app'; Request.MethodString := 'POST'; Request.URL := APNSUrl + APNSToken; Request.ContentType := 'application/json'; Request.CustomHeaders['Authorization'] := APNSAuth; Request.CustomHeaders['apns-topic'] := APNSTopic; Request.Source := TStringStream.Create(APNSPayload, TEncoding.UTF8); Response := HttpClient.Execute(Request); if Response.StatusCode = 200 then ShowMessage('Push notification sent successfully.') else ShowMessage('Failed to send push notification.'); finally Request.Free; HttpClient.Free; end; end; ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值