使用 Java 和 REST 发送 Apple 推送通知时出现问题
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1372561/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Problem sending Apple Push Notification using Java and REST
提问by mw_javaguy
This is a follow up to my previous postingon StackOverflow.
这是我之前在 StackOverflow上发布的帖子的后续。
Figured its better to start a new post (since I made more progress than before) rather than appending a new question on a previous thread.
认为最好开始一个新帖子(因为我比以前取得了更多的进步)而不是在以前的线程上附加一个新问题。
Am using the Javapns library on Google Code to send an Apple Push Notification through a REST based web service...
我正在使用 Google Code 上的 Javapns 库通过基于 REST 的 Web 服务发送 Apple 推送通知...
Here are the steps that I have completed:
以下是我已经完成的步骤:
iPhone Developer Program Portal (IDPP):
iPhone 开发者计划门户(IDPP):
(1) Created the App ID and APNS based SSL Certificate and Keys.
(1) 创建基于 App ID 和 APNS 的 SSL 证书和密钥。
(2) Created and installed the provisioning profile.
(2) 创建并安装配置文件。
(3) Installed the SSL Certificate and Key on the server.
(3) 在服务器上安装 SSL 证书和密钥。
(4) Set up my iPhone app to register for remote notifications.
(4) 设置我的 iPhone 应用程序以注册远程通知。
XCode:
代码:
Was able to obtain my device token when I built and deployed my app onto my device.
在我构建应用程序并将其部署到我的设备上时,能够获取我的设备令牌。
As soon as my iPhone app deployed, the dialog came up on my iPhone indicating that my app would like to send push notifications and also asked for permission to allow them.
一旦我的 iPhone 应用程序部署,我的 iPhone 上就会出现对话框,表明我的应用程序想要发送推送通知,并要求允许它们。
When I invoked my web service, through my Log4J statements, I was able to see that my REST based web service was indeed invoked but I never received a push notification on my iPhone app!
当我调用我的 Web 服务时,通过我的 Log4J 语句,我能够看到我的基于 REST 的 Web 服务确实被调用了,但我从未在我的 iPhone 应用程序上收到推送通知!
ApnsManager class:
ApnsManager 类:
public class ApnsManager {
/** APNs Server Host **/
private static final String HOST = "gateway.sandbox.push.apple.com";
/** APNs Port */
private static final int PORT = 2195;
public void sendNotification(String deviceToken)
throws Exception {
try {
PayLoad payLoad = new PayLoad();
payLoad.addAlert("My alert message");
payLoad.addBadge(45);
payLoad.addSound("default");
PushNotificationManager pushManager =
PushNotificationManager.getInstance();
pushManager.addDevice("iPhone", deviceToken);
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("iPhone");
// Send Push
log.warn("Sending push notification...");
pushManager.sendNotification(client, payLoad);
pushManager.stopConnection();
}
catch (Exception e) {
e.printStackTrace("Unable to send push ");
}
}
}
RESTful Web Service:
RESTful 网络服务:
@Path(ApnService.URL)
@Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
public class ApnService {
public static final String URL = "/apns";
@GET
@Path("send")
@Consumes({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
public String send() throws JSONException, IOException {
String msg = "";
try {
log.debug("Inside ApnService.send() method.");
log.debug("Sending notification to device");
ApnManager.sendNotification("32b3bf28520b977ab8eec50b482
25e14d07cd78 adb69949379609e40401d2d1de00000000738518e5c
000000003850978c38509778000000000000000000398fe12800398f
e2e0398fe1040000");
} catch(Exception e ) {
e.printStackTrace();
msg = "fail";
}
msg = "success";
StringWriter sw = new StringWriter();
JsonFactory f = new JsonFactory();
JsonGenerator g = f.createJsonGenerator(sw);
g.writeStartObject();
g.writeStringField("status", msg);
g.writeEndObject();
g.close();
return sw.toString();
}
}
Now, when I deploy my app to my app server and open up a rest client and type in:
现在,当我将我的应用程序部署到我的应用程序服务器并打开一个休息客户端并输入:
http: // localhost:8080/myapp/apns/send
http://localhost:8080/myapp/apns/send
The rest client returns this:
其余客户端返回:
HTTP/1.1 200 OK
HTTP/1.1 200 正常
The following log messages are outputted to my console:
以下日志消息输出到我的控制台:
01:47:51,985 WARN [ApnsManager] Initializing connectiong with APNS...
01:47:52,318 WARN [ApnsManager] Sending push notification...
MyAppDelegate.m
MyAppDelegate.m
- (void) applicationDidFinishLaunching :
(UIApplication*) application
{
NSLog( @"LAUNCH" );
// Configure REST engine
RESTAPI* api = [RESTAPI getInstance];
[api setNetworkAddress:kLocalAddress port:kDefaultPort];
UIRemoteNotificationType notificationTypes
= UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound;
if ([[UIApplication sharedApplication] enabledRemoteNotificationTypes]
!= notificationTypes) {
NSLog(@"Registering for remote notifications...");
[[UIApplication sharedApplication]
registerForRemoteNotificationTypes:notificationTypes];
} else {
NSLog(@"Already registered for remote notifications...");
// Uncomment this if you want to unregister
// NSLog(@"Unregistering for remote notifications...");
// [[UIApplication sharedApplication] unregisterForRemoteNotifications];
}
mainWindow = [[[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds] retain];
toolsNav = [[[ToolsNav alloc] init] retain];
[mainWindow addSubview:toolsNav.view];
[mainWindow makeKeyAndVisible];
}
However, I don't receive the push notification on my app (residing on my iPhone)!
但是,我的应用程序(驻留在我的 iPhone 上)没有收到推送通知!
Am really stumped at this point...
在这一点上我真的很难过......
What could I possibly be doing wrong? :(
我可能做错了什么?:(
Is it a problem with the way I set up my RESTful web service (sorry I am a newbie to REST)?
我设置 RESTful Web 服务的方式有问题吗(抱歉,我是 REST 的新手)?
Would really appreciate it if someone could assist me with this...
如果有人可以帮助我解决这个问题,我将不胜感激...
Thank you for taking the time to read this...
感谢您抽出时间来阅读...
采纳答案by mw_javaguy
Discovered the solution! The generated device token string seemed too long.
发现了解决办法!生成的设备令牌字符串似乎太长了。
My NSData to hex code was printing the wrong token (it should be 64 characters, before it was 155 characters).
我的 NSData 到十六进制代码打印了错误的标记(它应该是 64 个字符,之前是 155 个字符)。
Solution:
解决方案:
- (NSString *)hexadecimalDescription
{
NSMutableString *string = [NSMutableString stringWithCapacity:[self length] * 2];
const uint8_t *bytes = [self bytes];
for (int i = 0; i < [self length]; i++)
[string appendFormat:@"%02x", (uint32_t)bytes[i]];
return [[string copy] autorelease];
}
Now, I am receiving the notifications on my Device! :)
现在,我在我的设备上收到通知!:)
Happy programming to all!
祝大家编程愉快!

