apns推送 java,通过Java使用苹果推送通知服务

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

Provider code:

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);

}

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!

IDPP Registration Process:

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

Created the APNS based SSL Certificate and Keys

Created and installed the provisioning profile

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) 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)).

On page 17, it states:

"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."

iPhone OS Client Implementation

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

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?

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

Question(s):

(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) Am I using the Javapns library the right way?

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

解决方案

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;

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值