A1: No, this does not have to be at the beginning of the application. You can call registerForRemoteNotificationTypes from anywhere in the code.
[[UIApplication sharedApplication] registerForRemoteNotificationTypes:(UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound)];
You will need to handle the following delegate methods (in the delegate) that are called upon successful registration / failure for push notification.
A2: Yes you can. Two scenarios are possible. If your application is not running, you will process a push notification in the didFinishLaunchingWithOptions file. In this case, if the user selects “open” in the message or clicks on “Banners” (depending on user settings), your application will automatically start and you will be able to process user parameters sent in a push notification.
NSDictionary *params = [[launchOptions objectForKey:@"UIApplicationLaunchOptionsRemoteNotificationKey"] objectForKey:@"appsInfo"]; if (params) {
If your application is already running, the push notification will be delivered to the application:didReceiveRemoteNotification: delegate method, where you can simply present the UIAlertView with the message in the push notification and process delegate delegates alertView OK / Cancel by clicking in the standard way.
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo { NSDictionary *apsInfo = [userInfo objectForKey:@"apsinfo"]; // This appsInfo set by your server while sending push NSString *alert = [apsInfo objectForKey:@"alert"]; UIApplicationState state = [application applicationState]; if (state == UIApplicationStateActive) { application.applicationIconBadgeNumber = 0; AudioServicesPlaySystemSound(kSystemSoundID_Vibrate); UIAlertView *alertview = [[UIAlertView alloc] initWithTitle:@"Push Notification" message:alert delegate:self cancelButtonTitle:@"NO" otherButtonTitles:@"YES"]; [alertview show]; [alertview release]; } else { [self setTabs:contentsInfo]; } } - (void)alertView:(UIAlertView *)alertView willDismissWithButtonIndex:(NSInteger)buttonIndex { if (buttonIndex != [alertView cancelButtonIndex]) { // User pressed YES, do your stuffs } }
A3: If the user refused to accept the push notification from your application, then his didFailToRegisterForRemoteNotificationsWithError and, therefore, you will not receive the user devToken, which must be on your server to send a push notification to this user. If the user first accepts, but later, if he changes the settings to turn off your push notification, the Apple server will not send your push notification to this user. In this case, the user UDID will appear in the feedback service, and ideally, your server should remove this user UDID from the database and stop sending push notifications to these users. If you continue to send invalid push notifications, the Apple server may disconnect your connection and you will not be able to send any push notifications.
For details on the implementation, see Apple Push Notification .