可爱熊个人博客

Archive for 三月, 2012

APNS推送通知的流程

下面是我的所有部署配置过程。

1. 将app注册notification里面, 并从APNS上获取测试机的deviceToken.

复制代码

  1. – (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
  2.     [[UIApplication sharedApplication] registerForRemoteNotificationTypes:(UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound)];
  3.         // other codes here.
  4.     return YES;
  5. }
  6. – (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
  7.     NSLog(@”deviceToken: %@”, deviceToken);
  8. }
  9. – (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error {
  10.     NSLog(@”Error in registration. Error: %@”, error);
  11. }
  12. – (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
  13. {
  14.     NSLog(@”收到推送消息 : %@”,[[userInfo objectForKey:@”aps”] objectForKey:@”alert”]);
  15.     if ([[userInfo objectForKey:@”aps”] objectForKey:@”alert”]!=NULL) {
  16.         UIAlertView* alert = [[UIAlertView alloc] initWithTitle:@”推送通知”
  17.                                                         message:[[userInfo objectForKey:@”aps”] objectForKey:@”alert”]
  18.                                                        delegate:self
  19.                                               cancelButtonTitle:@”关闭”
  20.                                               otherButtonTitles:@”更新状态”,nil];
  21.         [alert show];
  22.         [alert release];
  23.     }
  24. }

启动程序,将app注册到通知项后,在console里面找到打印的deviceToken: Read More…