要作一個iPhone Push消息的需求,從簡單test的開始。html
一、先添加一個app IDjava
device token,即設備令牌,不是系統惟一標識(見獲取iOS設備的基本信息),須要在應用啓動時發起到apple服務器請求,註冊本身的設備和應用,並得到這個device token。ios
//
// 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
git
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>
github
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)
服務器
When a password was not defined on keyStore generation I have the following situations:app
1 - Using a null password in APNS.newService().withCert(certificate.p12, password) returns a "NullPointerException";iphone
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"ide
withCert
throw an
IllegalArgumentException
instead.