mw_*_*guy 8 java apple-push-notifications
我试图实现一个Java程序,它将Apple推送通知发送到iPhone客户端应用程序...找到以下库:Java APN
创建以下代码(来自Javapns)以在我的应用程序中使用:
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);
}
Run Code Online (Sandbox Code Playgroud)
当我运行这个应用程序时(你可以通过Log4j语句看到),没有例外情况发生:
WARN [MyCode] Initializing connectiong with APNS...
WARN [MyCode] Sending push notification...
Run Code Online (Sandbox Code Playgroud)
但我的客户端应用程序没有收到任何通知!
此外,在iPhone开发人员计划门户(IDPP)上执行了以下操作:
创建了基于APNS的SSL证书和密钥
创建并安装了配置文件
在服务器上安装了SSL证书和密钥.
已多次阅读Apple推送通知服务指南并注意到以下几点:
(1)在页15上,它指出设备令牌与设备UDID(我当前错误地传入了PushNotificationManager.addDevice()方法中的第二个参数(见上文))不同.
在第17页,它指出:
"APN使用唯一设备证书中包含的信息生成设备令牌.设备令牌包含设备的标识符.然后使用令牌密钥加密设备令牌并将其返回给设备.设备将设备令牌返回给设备令牌.请求应用程序作为NSData对象.然后,应用程序必须以二进制或十六进制格式将设备令牌传递给其提供者."
(2)在阅读第33-34页后,我发现我没有包含Objective-C代码以使app注册APN.
我不是Objective-C开发人员,所以我可以恢复设备代码或者我必须从证书中获取它吗?
我在哪里获取设备令牌(抱歉,其他人编写了Objective-C客户端应用程序,我是Java开发人员)?
问题(S):
(1)除了不知道从何处获取设备令牌和移动客户端代码注册外,还有其他我没有看过或错过的内容吗?
(2)我是否正确使用Javapns库?
感谢您抽出时间来阅读...
not*_*oop 18
作为一个可耻的自我广告,我鼓励使用java-apns
图书馆.您的代码将如下所示:
ApnsService service =
APNS.newService()
.withCert("/etc/Certificates.p12", "password")
.withSandboxDestination() // or .withProductionDestination()
.build();
String payload =
APNS.newPayload()
.alertBody("My alert message")
.badge(45)
.sound("default")
.build();
String deviceToken = "f4201f5d8278fe39545349d0868a24a3b60ed732";
log.warn("Sending push notification...");
service.push(deviceToken, payload);
Run Code Online (Sandbox Code Playgroud)
只是一点点提示,为了将收到的令牌转换为适合javapns注册的格式,这段代码就可以解决问题:
- (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;
Run Code Online (Sandbox Code Playgroud)
}
归档时间: |
|
查看次数: |
35820 次 |
最近记录: |