看下这一段,https://developer.apple.com/library/ios/documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/Chapters/IPhoneOSClientImp.html#//apple_ref/doc/uid/TP40008194-CH103-SW2,
An application must register with Apple Push Notification service for the operating systems on a device and on a computer to receive remote notifications sent by the application’s provider. Registration has three stages:
The app registers for remote notifications.
The system sets up remote notifications for the app and, if registration is successful, passes a device token to the app delegate.
The app sends its device token to the push provider.
The actions that take place during this sequence are illustrated by Figure 3-3 in “Token Generation and Dispersal.”
Device tokens can change. Your app needs to reregister every time it is launched—in iOS by calling the registerForRemoteNotificationTypes: method of UIApplication,
从官方的app的代码来看:
// Delegation methods
- (void)application:(UIApplication *)app didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)devToken {
const void *devTokenBytes = [devToken bytes];
self.registered = YES;
[self sendProviderDeviceToken:devTokenBytes]; // custom method
}
- (void)application:(UIApplication *)app didFailToRegisterForRemoteNotificationsWithError:(NSError *)err {
NSLog(@"Error in registration. Error: %@", err);
}
这两个都是在app启动的时候,如果app调用了以下的方法来注册push notification,那么,第一个delegate,返回成功的token,第二个delegate告诉app,注册失败,而成功之后,就是把token发送到你的服务器端,通常而言,你可以用这样的处理方法,一旦app启动,那么应该从服务器端禁止push notification,一旦app转移到background状态,就在服务器端启动push notification,这个在app里面都有处理的delegate,需要服务器编写代码实现这一点。
[[UIApplication sharedApplication] registerForRemoteNotificationTypes:(UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound)];