前提是調試或發布時,證書已經啟動了push服務
1.注冊遠程通知類型
UIRemoteNotificationType apn_type = (UIRemoteNotificationType)(UIRemoteNotificationTypeAlert|UIRemoteNotificationTypeSound|UIRemoteNotificationTypeBadge);
[[UIApplication sharedApplication] registerForRemoteNotificationTypes:apn_type];
2.實現appDelegate的委托方法
- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData*)deviceToken { NSString *token = [[deviceToken description] stringByTrimmingCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@"<>"]]; self.deviceToken = [token stringByReplacingOccurrencesOfString:@" " withString:@""]; NSLog(@"deviceToken:%@", self.deviceToken); //這里可以把deviceToken發給自己的服務器 }
獲取失敗回調
- (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError*)error { NSLog(@"didFailToRegisterForRemoteNotificationsWithError:%@", [error localizedDescription]); }
接收到遠程通知
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo { NSLog(@"receive remote notification:%@", userInfo); }
