问题:我的代码似乎执行正常,没有错误,并且显示正确的日志结果,或者显示,但iPhone上没有推送通知。我正在发送来自运行在Mac:Mountain Lion上的Apache Tomcat的通知。
注意:我可以使用相同的证书运行它,除了php中的.pem格式。 Javapns不接受.pem所以我使用.p12文件。由于我的证书的.pem版本在php中工作,我开始怀疑这整个问题是javapns的缺点。
背景
下面的代码执行时没有错误,notifications.isSuccessful()
实际上返回true。但是我的设备上没有收到任何消息,也没有错误。当我以前使用了错误的密钥时,我得到了适当的错误,当我的令牌字符串太长时,我收到了错误。现在应用程序最终运行,没有例外,但没有任何东西到达设备。
String keyFilePath = request.getServletContext().getRealPath("")+System.getProperty("file.separator")+"keys"+System.getProperty("file.separator")+"key.p12";
List<PushedNotification> notifications = Push.combined (pushNotificationMessage, 1, "Sound.wav", keyFilePath, "Password", false, userToken);
for (PushedNotification notification : notifications) {
if (notification.isSuccessful()) {
System.out.println("Push notification sent successfully to: " +
notification.getDevice().getToken());
} else {
String invalidToken = notification.getDevice().getToken();
Exception theProblem = notification.getException();
theProblem.printStackTrace();
/* If the problem was an error-response packet returned by Apple, get it */
ResponsePacket theErrorResponse = notification.getResponse();
if (theErrorResponse != null) {
System.out.println(theErrorResponse.getMessage());
}
}
}
日志
2014-05-16 20:29:21 DEBUG Payload:219 - Adding alert [blah blah blah]
2014-05-16 20:29:21 DEBUG Payload:193 - Adding badge [1]
2014-05-16 20:29:21 DEBUG Payload:205 - Adding sound [Sound.wav]
2014-05-16 20:29:21 DEBUG ConnectionToAppleServer:94 - Creating SSLSocketFactory
2014-05-16 20:29:21 DEBUG ConnectionToAppleServer:149 - Creating SSLSocket to gateway.sandbox.push.apple.com:2195
2014-05-16 20:29:22 DEBUG PushNotificationManager:111 - Initialized Connection to Host: [gateway.sandbox.push.apple.com] Port: [2195]: 5dec07cc[SSL_NULL_WITH_NULL_NULL: Socket[addr=gateway.sandbox.push.apple.com/17.172.232.45,port=2195,localport=51223]]
2014-05-16 20:29:22 DEBUG PushNotificationManager:538 - Building Raw message from deviceToken and payload
2014-05-16 20:29:22 DEBUG PushNotificationManager:617 - Built raw message ID 1 of total length 154
2014-05-16 20:29:22 DEBUG PushNotificationManager:396 - Attempting to send notification: {"aps":{"sound":"Sound.wav","alert":"blah blah blah","badge":1}}
2014-05-16 20:29:22 DEBUG PushNotificationManager:397 - to device: cb45519c4516907a03b3b5c2f0e48aa51f6f3d900cb7598f4e3c4482b33afea8
2014-05-16 20:29:22 DEBUG PushNotificationManager:415 - Flushing
2014-05-16 20:29:22 DEBUG PushNotificationManager:417 - At this point, the entire 154-bytes message has been streamed out successfully through the SSL connection
2014-05-16 20:29:22 DEBUG PushNotificationManager:420 - Notification sent on first attempt
2014-05-16 20:29:22 DEBUG PushNotificationManager:222 - Reading responses
2014-05-16 20:29:27 DEBUG PushNotificationManager:200 - Closing connection
Push notification sent successfully to: cb45519c4516907a03b3b5c2f0e48aa51f6f3d900cb7598f4e3c4482b33afea
注册
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
UIRemoteNotificationType notificationTypes = [[UIApplication sharedApplication] enabledRemoteNotificationTypes];
if (notificationTypes != (UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound) ) {
[[UIApplication sharedApplication] registerForRemoteNotificationTypes:(UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound) ];
NSLog(@"Reregistering for notifications");
} else {
NSLog(@"Notifications Found");
}
return YES
}
获取令牌
-(void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken
{
NSString *stringToken = [[[[deviceToken description]
stringByReplacingOccurrencesOfString: @"<" withString: @""]
stringByReplacingOccurrencesOfString: @">" withString: @""]
stringByReplacingOccurrencesOfString: @" " withString: @""];
//[NSString stringWithFormat:@"%@",deviceToken];
[self sendPNToken:[NSString stringWithFormat:@"%@",stringToken]];
NSLog(@"StringToken: %@",stringToken);
}
系统
Mountain Lion / Mac,Apache Tomcat,JDK 1.7