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

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

问题描述

想实现它发送一个苹果推送通知到iPhone客户端应用程序的Java程序......发现了以下库:的 Java的的APN

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

创建以下code(从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):


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

  • Created the APNS based SSL Certificate and Keys

创建和安装的供应配置文件

Created and installed the provisioning profile

安装的SSL证书,并在服务器上的关键。

Installed the SSL Certificate and Key on the server.

已经阅读过苹果推送通知服务指南几次,发现几件事情:

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页,它指出:

的APN利用包含唯一设备证书中的信息的设备令牌。该设备令牌包含设备的标识符,然后它与令牌密钥加密设备令牌,并将其返回给装置,该装置返回装置令牌给发出请求的应用程序作为一个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 code能与APN的应用寄存器

(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开发人员,所以这是我在哪里可以收回设备code或我必须从证书得到它?

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?

我在哪里可以获取设备令牌(不好意思,别人写的Objective-C的客户端应用程序,我是一个Java开发)?

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

问题(S):

(1)由于不知道从哪里获取设备令牌和手机客户端code登记外,还有什么,我还没有看过去还是错过了什么?

(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登记的格式,这code将这样的伎俩:

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使用苹果推送通知服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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