Baconbeastnz Baconbeastnz - 4 months ago 9x
iOS Question

iOS Registering for Push Notifications within Application

Q1. Do I have to do this at the start of my App? Or can I trigger the prompt to allow/not-allow at any point in my app?

Q2. Is there a way to find out if the user clicked yes/no ? (callback?)

Q3. If the user has already clicked no, (in a previous session), will my prompt actually fire? Or do I need to tell the user to go to their phone settings and enable it there?

Reason being is I have an app which has a section inside it called "Notifications" whereby they can enable/disable to receive notifications on certain things, so I only want to prompt them to enable etc. when they are in this section not at the start of the App.


A1: No, it doesn't have to be at the start of the app. You can invoke registerForRemoteNotificationTypes from anywhere in the code.

[[UIApplication sharedApplication] registerForRemoteNotificationTypes:(UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound)];

You will need to handle following delegates methods(in delegate) which gets called on successful/fail registering for push notification.

// Delegation methods
- (void)application:(UIApplication *)app didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)devToken {
    const void *devTokenBytes = [devToken bytes];
    self.registered = YES;
    [self sendProviderDeviceToken:devTokenBytes]; // this will send token to your server's database

- (void)application:(UIApplication *)app didFailToRegisterForRemoteNotificationsWithError:(NSError *)err {
    NSLog(@"Error in registration. Error: %@", err);

A2: Yes you can. There are two possible scenarios. If your application is not running, you will handle push notification in didFinishLaunchingWithOptions. In this case if user has selected "open" in the message alert or clicked on Banners(depends on user's settings), your application will automatically start and you can handle user parameters passed in push notification.

 /* Push notification received when app is not running */
 NSDictionary *params = [[launchOptions objectForKey:@"UIApplicationLaunchOptionsRemoteNotificationKey"] objectForKey:@"appsInfo"];

    if (params) {
        // Use params and do your stuffs

If your application is already running, push notification will be delivered to application:didReceiveRemoteNotification: delegate method where you can simply present UIAlertView with the message in push notification and handle alertView delegates OK/Cancel button click in 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;


        UIAlertView *alertview = [[UIAlertView alloc] initWithTitle:@"Push Notification"
        [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 user has refused to accept push notification from your app then its didFailToRegisterForRemoteNotificationsWithError and you hence you will not get user's devToken which is required to be on your server to send push notification to that user. If user initially accept but later on if he changed settings to disable your push notification then Apple server will not send your push notification to that user. In this case that user's UDID will appear in Feedback service and ideally your server should remove that user's UDID from the database and stop sending push notification to that users going forward. If you keep sending invalid push notification, Apple server might drop your connection silently and you will not able to send any push notifications.

See Apple Push Notification docs for details on implementation.