iOS Push Notification Client

Prepare Development

  1. Enable Push notification of your App
  2. Prepare development, production certificate for Push Notification
  3. Import certificate and provisioning profile
  4. Write code and testing

No need to set capabilities.

XCode Setting

We have to set app identifier which is enabled of Push Notification

Code

Implement delegate methods
We need to change some method according to OS version(over 8.0 or not)

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Remote Notification
    if ([application respondsToSelector:@selector(registerUserNotificationSettings:)]) {
        // use registerUserNotificationSettings 
        // For iOS8
        UIUserNotificationSettings *settings = [UIUserNotificationSettings
                                                  settingsForTypes:UIUserNotificationTypeBadge | UIUserNotificationTypeAlert categories:nil];
        [[UIApplication sharedApplication] registerUserNotificationSettings:settings];
    }
    else {
        // for iOS7 before
        // use registerForRemoteNotifications
        [[UIApplication sharedApplication]
         registerForRemoteNotificationTypes:(UIRemoteNotificationTypeAlert |
                                             UIRemoteNotificationTypeBadge)];
    }
    
    // Get Payload
    UILocalNotification *notification =
    [launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey];
    
    if (notification) {
        // Get Notification
        application.applicationIconBadgeNumber = 1;
    }
}

Device Token Delegate

#pragma mark - Remote Notification
- (void)application:(UIApplication *)application didRegisterUserNotificationSettings:(UIUserNotificationSettings *)notificationSettings {
    [application registerForRemoteNotifications];
}

- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
    
    // Send Device Token
    NSLog(@"Device Token %@", deviceToken);
}

- (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error {
    TBELog(error);
}

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {
}

Testing

We need to use device. We cannot test in simulator.


Device Token Handled

To send device token to our server, we need to change NSData * to NSString *(64bit).
This NSData is not string data.

NSString * tokenAsString = [deviceToken description];  // deviceToken is NSData device token
tokenAsString = [tokenAsString stringByTrimmingCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@"<>"]];
tokenAsString = [tokenAsString stringByReplacingOccurrencesOfString:@" " withString:@""];

Lifecycle

The user taps notification

  1. – (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
  2. – (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo