Push Notifications For iOS

If you wish to use any of the Push Campaign or Push Point features of User Hook, you will need to follow a few additional steps while setting up your app.

First, you will need to create the appropriate push certificates through the Apple Member Center. Once you have created your certificate, you will need to upload this certificate through the User Hook admin website.

Sandbox vs Production

While developing your app locally, push messages will be sent through Apple’s Sandbox environment. In order to receive these sandbox push notifications, you will need to upload a certificate for the “Sandbox Certificate”. Once your app is live in iTunes, all push messages will be sent using Apple’s Production servers and will use the corresponding “Production Certificate”. User Hook automatically determines if the app is running as a local development build or as a published live build. You do not need to make any changes inside your app to handle the sandbox/production differences.

Initialize User Hook for Push Notifications

Inside your AppDelegate.m file we need to tell User Hook that you want the app to register for push notifications. This is done by calling the [UserHook registerForPush:] method.

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

    // setup User Hook
    [UserHook setApplicationId:@"YOUR_USERHOOK_APP_ID" apiKey:@"YOUR_USERHOOK_APP_KEY"]

    // setup push messaging
    [UserHook registerForPush:launchOptions];
}

We also need to add the following methods:

#pragma mark - push notifications
- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {

    // register push notification token with User Hook
    [UserHook registerDeviceToken:deviceToken];

}

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

    // handle incoming push notifications
    [UserHook handlePushNotification:userInfo];

}

Handling Push Notification Payloads

When your iOS app receives a push notification, it will have access to the Payload data that was defined in the User Hook admin site. If your app will be using this payload data, you will need to setup a payload handler.

Handling Multiple Push Providers

There may be scenarios where your app needs to use another push message provider at the same time as using User Hook. That is not a problem. Inside of your AppDelegate.m you can inspect the push message to determine if it was sent from User Hook or from another push provider.

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

    // handle incoming push notifications
    if ([UserHook isPushFromUserHook:userInfo]) {
        [UserHook handlePushNotification:userInfo];
    }
    else {
        // push is from a different push provider
        // use the appropriate logic from that push provider
        // to handle push message
    }

}