iPhone Push消息全攻略.1
要做一个iPhone Push消息的需求,从简单test的开始。
1、先添加一个app ID
device token,即设备令牌,不是系统唯一标识(见获取iOS设备的基本信息),需要在应用启动时发起到apple服务器请求,注册自己的设备和应用,并获得这个device token。
//
// com_sencloud_testAppDelegate.m
// test
//
// Created by chen minglei on 13-7-11.
// Copyright (c) 2013
年
chen minglei. All rights reserved.
//
#import
"com_sencloud_testAppDelegate.h"
@implementation
com_sencloud_testAppDelegate
@synthesize
window;
@synthesize
viewController;
- (
void
)applicationDidFinishLaunching:(
UIApplication
*)application {
[
window
addSubview:viewController.view];
[windowmakeKeyAndVisible];
NSLog(@"Registering for push notifications...");
[[UIApplicationsharedApplication]
registerForRemoteNotificationTypes:
(UIRemoteNotificationTypeAlert |
UIRemoteNotificationTypeBadge |
UIRemoteNotificationTypeSound)];
}
- (void)application:(UIApplication *)app didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
NSString *str = [NSString
stringWithFormat:@"Device Token=%@",deviceToken];
NSLog(str);
}
- (void)application:(UIApplication *)app didFailToRegisterForRemoteNotificationsWithError:(NSError *)err {
NSString *str = [NSStringstringWithFormat: @"Error: %@", err];
NSLog(str);
}
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {
for (id key in userInfo) {
NSLog(@"key: %@, value: %@", key, [userInfo objectForKey:key]);
}
}
@end
2013-07-11 21:18:36.139 test[6386:907] Registering for push notifications...
2013-07-11 21:19:05.988 test[6386:907] Device Token=<c8cd88d5 9c0d7407 fc697357 3d3778e5 5e83b92e d40c7588 a595be18 119c6f92>
import javapns.back.PushNotificationManager; import javapns.back.SSLConnectionHelper; import javapns.data.Device; import javapns.data.PayLoad; public class ApnsAct { public static void main(String[] args) throws Exception { try { String deviceToken = "c8cd88d59c0d7407fc6973573d3778e55e83b92ed40c7588a595be18119c6f92"; PayLoad payLoad = new PayLoad(); payLoad.addAlert("Test"); payLoad.addBadge(4); payLoad.addSound("default"); PushNotificationManager pushManager = PushNotificationManager .getInstance(); pushManager.addDevice("iPhone", deviceToken); // Connect to APNs String host = "gateway.sandbox.push.apple.com"; int port = 2195; String certificatePath = "/Users/plan9x/Desktop/test.p12"; String certificatePassword = "test"; pushManager.initializeConnection(host, port, certificatePath, certificatePassword, SSLConnectionHelper.KEYSTORE_TYPE_PKCS12); // Send Push Device client = pushManager.getDevice("iPhone"); pushManager.sendNotification(client, payLoad); pushManager.stopConnection(); pushManager.removeDevice("iPhone"); } catch (Exception e) { e.printStackTrace(); } } }
java.io.IOException
: failed to decrypt safe contents entry:
java.lang.ArithmeticException
: / by zero
at com.sun.net.ssl.internal.pkcs12.PKCS12KeyStore.engineLoad(PKCS12KeyStore.java:1277)
at java.security.KeyStore.load(KeyStore.java:1183)
at javapns.back.SSLConnectionHelper.<init>(Unknown Source)
at javapns.back.PushNotificationManager.initializeConnection(Unknown Source)
Problem with empty or null password in 'APNS.newService().withCert(certificate.p12, password)'
When a password was not defined on keyStore generation I have the following situations:
1 - Using a null password in APNS.newService().withCert(certificate.p12, password) returns a "NullPointerException";
2 - Using an empty password in APNS.newService().withCert(certificate.p12, password) returns "java.io.IOException: failed to decrypt safe contents entry: java.lang.ArithmeticException: / by zero"
withCert
throw an
IllegalArgumentException
instead.