Documentation

iOS SDK Reference


Register devices, receive push notifications, and more with our iOS SDK.

Our iOS SDK registers your device with the Apple Push Notification Service and assigns a unique token you can use to send notifications to the device. You can also subscribe the device to pub/sub topics.

Please select your project type for the appropriate instructions:

Swift
Objective-C

Import the SDK using the following statement:

import Pushy
#import <Pushy/Pushy-Swift.h>

Initialize the SDK by passing in the shared UIApplication instance:

let pushy = Pushy(UIApplication.shared)
Pushy* pushy = [[Pushy alloc]init:[UIApplication sharedApplication]];
Register Devices

Assign a unique token to the device and register it to receive notifications:

// Register the device for push notifications
pushy.register({ (error, deviceToken) in
    // Handle registration errors
    if error != nil {
        return print ("Registration failed: \(error!)")
    }
    
    // Print device token to console
    print("Pushy device token: \(deviceToken)")
})
// Register the device for push notifications
[pushy register:^(NSError *error, NSString* deviceToken) {
    // Handle registration errors
    if (error != nil) {
        return NSLog (@"Registration failed: %@", error);
    }
    
    // Print device token to console
    NSLog(@"Pushy device token: %@", deviceToken);
}];

In case the device has already been registered, this method will simply return the existing token instead. Refer to the Register Devices documentation page for more information.

Check Registration Status

Determine whether the device has already been registered to receive notifications:

let isRegistered = pushy.isRegistered()
BOOL isRegistered = [pushy isRegistered];
Handle Notifications

Declare a callback that will be invoked when users react to your notifications:

// Handle push notifications
pushy.setNotificationHandler({ (data, completionHandler) in
    // Print notification payload data
    print("Received notification: \(data)")
    
    // You must call this completion handler when you finish processing
    // the notification (after fetching background data, if applicable)
    completionHandler(UIBackgroundFetchResult.newData)
})
// Handle push notifications
[pushy setNotificationHandler:^(NSDictionary *data, void (^completionHandler)(UIBackgroundFetchResult)) {
    // Print notification payload data
    NSLog(@"Received notification: %@", data);
    
    // You must call this completion handler when you finish processing
    // the notification (after fetching background data, if applicable)
    completionHandler(UIBackgroundFetchResultNewData);
}];

Note: This callback will be invoked in the background, without requiring user interaction, if you send the content_available flag with your push notifications.

Refer to the Handle Notifications documentation page for more information.

Manage Topic Subscriptions

Subscribe a registered device to one or more topics:

// Subscribe the device to a topic
pushy.subscribe(topic: "news", handler: { (error) in
    // Handle subscribe errors
    if error != nil {
        return print("Subscribe failed: \(error!)")
    }
    
    // Success
    print("Subscribed successfully")
})
// Subscribe the device to a topic
[pushy subscribeWithTopic:@"news" handler:^(NSError *error) {
    // Handle subscribe errors
    if (error != nil) {
        return NSLog(@"Subscribe failed: %@", error);
    }
    
    // Success
    NSLog(@"Subscribed successfully");
}];

Unsubscribe a registered device from one or more topics:

// Unsubscribe the device from a topic
pushy.unsubscribe(topic: "news", handler: { (error) in
    // Handle unsubscribe errors
    if error != nil {
        return print("Unsubscribe failed: \(error!)")
    }
    
    // Success
    print("Unsubscribed successfully")
})
// Unsubscribe the device from a topic
[pushy unsubscribeWithTopic:@"news" handler:^(NSError *error) {
    // Handle unsubscribe errors
    if (error != nil) {
        return NSLog(@"Unsubscribe failed: %@", error);
    }
    
    // Success
    NSLog(@"Unsubscribed successfully");
}];

Note: You may also pass in a [String] array with multiple topics via the topics: handler: method.


Refer to the Subscribe to Topics documentation page for more information.