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 iOS project type for the appropriate instructions:
Import the SDK using the following statement:
import Pushy
@import Pushy;
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 user for push notifications
pushy.register({ (error, deviceToken) in
// Handle registration errors
if error != nil {
return print ("Registration failed: \(error!.localizedDescription)")
}
// Print device token to console
print("Pushy device token: \(deviceToken)")
})
// Register the user 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];
Unregister the Device
Invoke this method to unregister the device from remote notifications. Calling pushy.register()
on this device in the future will restore the device token which was previously assigned to the device, and make it possible to receive notifications again.
pushy.unregister()
[pushy unregister];
Handle Notifications
Declare a callback that will be invoked when incoming notifications are received:
// Handle incoming notifications
pushy.setNotificationHandler({ (data, completionHandler) in
// Print notification payload data
print("Received notification: \(data)")
// Call this completion handler when you finish processing
// the notification (after fetching background data, if applicable)
completionHandler(UIBackgroundFetchResult.newData)
})
// Handle incoming notifications
[pushy setNotificationHandler:^(NSDictionary *data, void (^completionHandler)(UIBackgroundFetchResult)) {
// Print notification payload data
NSLog(@"Received notification: %@", data);
// 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.
Listen for Notification Click
Declare a callback that will be invoked when the user taps your notifications:
// Handle notification tap event
pushy.setNotificationClickListener({ (data) in
// Show an alert dialog
let alert = UIAlertController(title: "Notification Click", message: data["message"] as? String, preferredStyle: UIAlertController.Style.alert)
alert.addAction(UIAlertAction(title: "OK", style: UIAlertAction.Style.default, handler: nil))
self.window?.rootViewController?.present(alert, animated: true, completion: nil)
// Navigate the user to another page or
// execute other logic on notification click
})
// Handle notification tap event
[pushy setNotificationClickListener:^(NSDictionary *data) {
// Show an alert dialog
UIAlertController * alert = [UIAlertController
alertControllerWithTitle:@"Notification Click"
message:[data objectForKey:@"message"]
preferredStyle:UIAlertControllerStyleAlert];
[alert addAction:[UIAlertAction
actionWithTitle:@"OK"
style:UIAlertActionStyleDefault
handler:nil]];
[self.window.rootViewController presentViewController:alert animated:YES completion:nil];
// Navigate the user to another page or
// execute other logic on notification click
}];
Refer to the Handle Notifications documentation page for more information.
Toggle In-App Notification Banner
By default, the SDK will not display an in-app notification banner at the top of the screen when a notification is received. You can enable the banner by calling:
[pushy toggleInAppBanner:true];
pushy.toggleInAppBanner(true)
Note: Please call this method before pushy.setNotificationHandler()
for it to take effect.
Manage Topic Subscriptions
Subscribe a registered device to one or more topics:
// Subscribe the user to a topic
pushy.subscribe(topic: "news", handler: { (error) in
// Handle subscribe errors
if error != nil {
return print("Subscribe failed: \(error!.localizedDescription)")
}
// Success
print("Subscribed successfully")
})
// Subscribe the user 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 the user 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!.localizedDescription)")
}
// 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");
}];
Topics are case-sensitive and must match the following regular expression: [a-zA-Z0-9-_.]{1,100}
. You may also pass in a [String]
array with multiple topics (limited to 100) via the topics: handler:
method, or call pushy.unsubscribe(topic: "*", handler)
to unsubscribe the device from all topics at once.
Refer to the Subscribe to Topics documentation page for more information.
Get APNs Token
Access the underlying APNs token after successful registration:
[pushy getAPNsToken];
pushy.getAPNsToken()
Note: Please only call this method after successful invocation of pushy.register()
.
Configure App ID
By default, the SDK will automatically authenticate using your iOS app's Bundle ID.
You can manually pass in your Pushy App ID (Pushy Dashboard -> Click your app -> App Settings -> App ID) to override this behavior:
[pushy setAppId:@"YOUR_APP_ID"];
pushy.setAppId("YOUR_APP_ID")
Note: Please call this method before pushy.register()
for it to take effect.
Toggle AppDelegate Method Swizzling
By default, the SDK will use method swizzling to hook into the AppDelegate's APNs callbacks. You can disable method swizzling by calling:
[pushy toggleMethodSwizzling:false];
pushy.toggleMethodSwizzling(false)
Note: Please call this method immediately after initializing the Pushy
class for it to take effect, and make sure to implement the APNs AppDelegate methods, invoking the respective Pushy SDK method within, for the SDK to continue working properly with method swizzling disabled.