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!)")
    }
    
    // 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: Please make sure your AppDelegate class does not override any of the following APNs methods:

  1. application:didRegisterForRemoteNotificationsWithDeviceToken
  2. application:didFailToRegisterForRemoteNotificationsWithError
  3. application:didReceiveRemoteNotification:fetchCompletionHandler

Additional Note: The iOS simulator cannot register for, nor receive push notifications.