APNs
APNs
Device
Provider
DeviceToken
Payload
整体流程大体分为五个步骤:
1:
2:
3:
4:
5:
介绍完APNS的概况,下面再了解一下具体的实现方法:
注:先申请APNS的证书,再进行以下操作。
1. 将app注册notification里面, 并从APNS上获取测试机的deviceToken.
-
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOp
tions:(NSDictionary *)launchOptions { -
[[UIApplication sharedApplication] registerForRemoteNotific ationTypes:(UIRemoteNotificationType Badge | UIRemoteNotificationType Sound)]; -
// other codes here. -
return YES; -
}
-
-
- (void)application:(UIApplication *)application didRegisterForRemoteNoti
ficationsWithDeviceToken :(NSData *)deviceToken { -
NSLog(@"deviceToken: %@", deviceToken); -
}
-
-
- (void)application:(UIApplication *)application didFailToRegisterForRemo
teNotificationsWithError :(NSError *)error { -
NSLog(@"Error in registration. Error: %@", error); -
}
-
-
- (void)application:(UIApplication *)application didReceiveRemoteNotifica
tion:(NSDictionary *)userInfo -
{
-
NSLog(@" 收到推送消息 : %@",[[userInfo objectForKey:@"aps"] objectForKey:@"alert"]); -
if ([[userInfo objectForKey:@"aps"] objectForKey:@"alert"]!=NULL) { -
UIAlertView* alert = [[UIAlertView alloc] initWithTitle:@"推送通知" -
message:[[userInfo objectForKey:@"aps"] objectForKey:@"alert"] -
delegate:self -
cancelButtonTitle:@" 关闭" -
otherButtonTitles:@" 更新状态",nil]; -
[alert show]; -
[alert release]; -
} -
}
启动程序,将app注册到通知项后,在console里面找到打印的deviceToken:
-
deviceToken: <6974ac11 870e09fa 00e2238e 8cfafc7d 2052e342 182f5b57 fabca445 42b72e1b>
2. 生成app在服务端需要的许可证
1)进入Provisioning Portal, 下载Certificates在development下的证书。
2) 找到需要测试的app id,然后enable它在development下的Apple Push Notification service: Development Push SSL Certificate。需要输入1)中的签名证书才可以生成一个aps_developer_identity.cer.
3) 双击aps_developer_identity.cer,会打开系统的key chain. 在My certificates下找到Apple Development Push Services。需要为certificate和它之下的private key各自export出一个.p12文件。(会出现设置密码过程)
4)需要将上面的2个.p12文件转成.pem格式:
-
openssl pkcs12 -clcerts -nokeys -out cert.pem -in cert.p12
-
openssl pkcs12 -nocerts -out key.pem -in key.p12
5)如果需要对 key不进行加密:
-
openssl rsa -in key.pem -out key.unencrypted.pem
6)然后就可以 合并两个.pem文件, 这个ck.pem就是服务端需要的证书了。
-
cat cert.pem key.unencrypted.pem > ck.pem
3. 服务端push通知到ANPS. 在cocoachina找到了两种方法: