Register Devices


Users need to be uniquely identified to receive push notifications.

Every user is assigned a unique device token that you can use to push it at any given time. Once the user has been assigned a device token, it should be stored in your application's backend database.

Please select your iOS project type for the appropriate instructions:

Swift
Objective-C

Import the Pushy SDK by adding the following to the top of AppDelegate.swift:

import Pushy

Note: If you are not using Cocoapods (i.e. manual linking), please use import PushySDK instead.

Register users by initializing the SDK in the didFinishLaunchingWithOptions method of the AppDelegate class and calling pushy.register():

// Initialize Pushy SDK
let pushy = Pushy(UIApplication.shared)

// 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)")
    
    // Persist the token locally and send it to your backend later
    UserDefaults.standard.set(deviceToken, forKey: "pushyToken")
})

You should always call pushy.register() in your didFinishLaunchingWithOptions method to detect updates to the internal APNs device token, even if the device has already been registered in the past.

Import the Pushy SDK by adding the following to the top of AppDelegate.h:

@import Pushy;

Note: If you are not using Cocoapods (i.e. manual linking), please use #import <PushySDK/PushySDK-swift.h> instead.

Register users by initializing the SDK in the didFinishLaunchingWithOptions method of AppDelegate.m and calling [pushy register]:

// Initialize Pushy SDK
Pushy* pushy = [[Pushy alloc]init:[UIApplication sharedApplication]];

// 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);
    
    // Persist the token locally and send it to your backend later
    [[NSUserDefaults standardUserDefaults] setObject:deviceToken forKey:@"pushyToken"];
}];

You should always call [pushy register] in your didFinishLaunchingWithOptions method to detect updates to the internal APNs device token, even if the device has already been registered in the past.


Note: The SDK makes use of method swizzling to hook into the APNs AppDelegate callbacks. If you need to support multiple push notification providers, you have the option to disable method swizzling.

Additional Note: The iOS simulator cannot register for, nor receive push notifications (except on Apple Silicon M1, M2, and T2 Macs, and with Xcode 14+, and macOS 13+).