使用Java和REST发送Apple推送通知时出现问题
这是我之前在StackOverflow上的posting的后续操作。使用Java和REST发送Apple推送通知时出现问题
更好地开始一个新的职位(因为我比以前做了更多的进步),而不是在以前的线程上附加一个新的问题。
正在使用谷歌代码的Javapns库发送通过一个基于REST的Web服务的苹果推送通知......
这里是我已完成的步骤:
iPhone开发者计划门户网站( IDPP):
(1)创建了基于App ID和APNS的SSL证书和密钥。
(2)创建并安装配置文件。
(3)在服务器上安装SSL证书和密钥。
(4)设置我的iPhone应用程序以注册远程通知。
的XCode:
能够获得我的设备令牌,当我构建和部署我的应用程序到我的设备。
只要我的iPhone应用程序部署完成,对话框就出现在我的iPhone上,表明我的应用程序想要发送推送通知,并且还要求允许它们。
当我通过Log4J语句调用我的Web服务时,我能够看到基于REST的Web服务确实被调用,但我从未在我的iPhone应用程序上收到推送通知!
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服务:
@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();
}
}
现在,当我在我的应用程序部署到我的应用程序服务器,并打开了一个REST客户端并键入:
http:// localhost:8080/myapp/apns/send
其余客户端返回此:
HTTP/1.1 200 OK
以下日志信息输出到我的控制台:
01:47:51,985 WARN [ApnsManager] Initializing connectiong with APNS...
01:47:52,318 WARN [ApnsManager] Sending push notification...
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];
}
但是,我没有收到我的应用程序(驻留在我的iPhone上)的推送通知!
我真的难住在这一点上...
我可能会做错什么? :(
它是与我建立了我的RESTful Web服务的方式有问题(对不起,我是新手休息)?
会很感激,如果有人可以帮助我这个...
感谢您在百忙之中阅读本文时...
发现的解决方案!生成的设备标识字符串似乎太长。
我NSData的十六进制代码是印刷错误的令牌(应该是64字符,它是155个字符之前)。
解决方案:
- (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];
}
现在,我收到我的设备的通知! :)
快乐编程给所有!
令牌字符串包含一个空格。当你在这里复制代码时引入了它吗?尝试删除它。 – notnoop 2009-09-03 15:55:48
嗨,Saeed ...是的,当我复制代码(并重新设置了代码以使其更具视觉效果)时,引入了令牌字符串的空间。在我的实际代码中没有空间。我仍然卡住,所以我会很感激,如果有人可以帮助我这一点。我有一点小小的跑步,当我运行Web服务时,它询问我是否希望连接到端口2195并发送推送通知。它只是不会出现在我的iPhone的应用程序。有什么我做错了吗?它是REST代码吗? – 2009-09-04 09:53:49
您还应该包含您已实施的UIApplicationDelegate的“处理远程通知”方法的代码。 – bpapa 2009-09-04 13:47:35