通过 Java 使用 Apple 推送通知服务 [英] Use Apple Push Notification Service through Java

查看:51
本文介绍了通过 Java 使用 Apple 推送通知服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试实现一个 Java 程序,该程序将 Apple 推送通知发送到 iPhone 客户端应用程序...找到以下库:Java APNs

Am trying to implement a Java program which sends an Apple Push Notification to an iPhone client app... Found the following library: Java APNs

创建了以下代码(来自 Javapns)以在我的应用中使用:

Created the following code (from Javapns) to use in my app:

try {
    PayLoad payLoad = new PayLoad();

    payLoad.addAlert("My alert message");
    payLoad.addBadge(45);
    payLoad.addSound("default");

    PushNotificationManager pushManager = PushNotificationManager.getInstance();
    pushManager.addDevice("iPhone", "f4201f5d8278fe39545349d0868a24a3b60ed732");
    log.warn("Initializing connectiong with APNS...");

    // Connect to APNs
    pushManager.initializeConnection(HOST, PORT, 
                                 "/etc/Certificates.p12", "password", 
    SSLConnectionHelper.KEYSTORE_TYPE_PKCS12);

    Device client = pushManager.getDevice("Lambo");

    // Send Push
    log.warn("Sending push notification...");
    PushNotificationManager.getInstance().sendNotification(client, payLoad);
 }
 catch (Exception e) {
    throw new ApnsPushNotificationException("Unable to send push " + e);
 }

当我运行这个应用程序时(你可以通过 Log4j 语句看到),没有发生任何异常:

When I run this app (as you can see through the Log4j statements) there's no exceptions which occur:

  WARN  [MyCode] Initializing connectiong with APNS...
  WARN  [MyCode] Sending push notification...

但是我的客户端应用没有收到任何通知!

But my client app doesn't receive any notifications!

此外,在 iPhone 开发者计划门户 (IDPP) 上执行了以下操作:

Also, did the following on the iPhone Developer Program Portal (IDPP):

  • 创建基于 APNS 的 SSL 证书和密钥

  • Created the APNS based SSL Certificate and Keys

创建并安装配置文件

在服务器上安装了 SSL 证书和密钥.

Installed the SSL Certificate and Key on the server.

已多次阅读 Apple 推送通知服务指南并注意到以下几点:

Have read over the Apple Push Notification Service Guide several times and noticed a few things:

(1) 在第 15 页,它指出设备令牌与设备 UDID 不同(我目前错误地将其作为 PushNotificationManager.addDevice() 方法中的第二个参数传入(见上文)).

(1) On page 15, it states that the device token is not the same as the device UDID (which I am currently incorrectly passing in as the second parameter inside the PushNotificationManager.addDevice() method (see above)).

在第 17 页,它指出:

On page 17, it states:

"APNs 使用唯一设备证书中包含的信息生成设备令牌.设备令牌包含设备的标识符.然后它使用令牌密钥加密设备令牌并将其返回给设备.设备返回设备令牌作为 NSData 对象发送给请求应用程序.然后应用程序必须以二进制或十六进制格式将设备令牌传递给其提供者."

"APNs generates a device token using information contained in the unique device certificate. The device token contains an identifier of the device. It then encrypts the device token with a token key and returns it to the device. The device returns the device token to the requesting application as an NSData object. The application then must deliver the device token to its provider in either binary or hexidecimal format."

(2) 阅读第 33 - 34 页后,我发现我没有包含 Objective-C 代码来让应用程序注册到 APNs.

(2) After reading pages 33 - 34, I discovered that I didn't include the Objective-C code to have the app register with APNs.

我不是 Objective-C 开发人员,所以我可以在这里恢复设备代码还是必须从证书中获取它?

Am not an Objective-C developer, so is this where I can recover the device code or do I have to get it from the certificate?

我从哪里获得设备令牌(抱歉,我是 Java 开发人员,是其他人编写了 Objective-C 客户端应用程序)?

Where do I obtain the device token (sorry, someone else wrote the Objective-C client app and I am a Java Developer)?

问题:

(1) 除了不知道从哪里获取设备令牌和移动客户端代码注册之外,还有什么我没有看过或错过的吗?

(1) With the exception of not knowing where to get the device token and the mobile client code registration, is there anything else that I have not looked over or missed?

(2) 我是否以正确的方式使用 Javapns 库?

(2) Am I using the Javapns library the right way?

感谢您花时间阅读本文...

Thank you for taking the time to read this...

推荐答案

只是一个小技巧,为了将你收到的令牌转换成适合用 javapns 注册的格式,这段代码就可以做到:

Just a little tip, in order to convert your received token into a format suitable for registration with javapns, this code will do the trick:

- (NSString *)convertTokenToDeviceID:(NSData *)token {
NSMutableString *deviceID = [NSMutableString string];

// iterate through the bytes and convert to hex
unsigned char *ptr = (unsigned char *)[token bytes];

for (NSInteger i=0; i < 32; ++i) {
    [deviceID appendString:[NSString stringWithFormat:@"%02x", ptr[i]]];
}

return deviceID;

}

这篇关于通过 Java 使用 Apple 推送通知服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆