Skip to content

Instantly share code, notes, and snippets.

@celian-m
Last active February 29, 2016 13:37
Show Gist options
  • Save celian-m/fe1be13f3b4905e20d21 to your computer and use it in GitHub Desktop.
Save celian-m/fe1be13f3b4905e20d21 to your computer and use it in GitHub Desktop.
Register for iOS Push Notifications, iOS7 and above

#Registering

- (BOOL)application:(UIApplication *)application
    didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

//Your code...

//Check if OS is Version 7
  if (![[UIApplication sharedApplication]
          respondsToSelector:@selector(registerUserNotificationSettings:)]) {
    [[UIApplication sharedApplication]
        registerForRemoteNotificationTypes:(UIRemoteNotificationTypeBadge |
                                            UIRemoteNotificationTypeSound |
                                            UIRemoteNotificationTypeAlert)];
  } else {
    UIUserNotificationSettings *settings = [UIUserNotificationSettings
        settingsForTypes:UIUserNotificationTypeAlert |
                         UIUserNotificationTypeBadge |
                         UIUserNotificationTypeSound
              categories:nil];
    [[UIApplication sharedApplication]
        registerUserNotificationSettings:settings];
    [[UIApplication sharedApplication] registerForRemoteNotifications];
  }
  return YES;
}

#Get the Token

- (void)application:(UIApplication *)application
    didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
    NSLog(@"My device token is : %@", deviceToken);
}

#Handle errors

- (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error{
    NSLog(@"App did fail to register for remote notifications : %@", error);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment