Initializing and Registering a Device

Initializing and Registering a Device

You need to register each device that you want to send push notifications to. The register operation takes care of two tasks:

  • Registers (initializes) the device with its vendor's push notification service—This step includes acquiring a push notification token from the vendor's push notifications service that authorizes Telerik Platform to send messages to the device.
  • Creates a device registration in Telerik Platform—This step includes saving the device's push notification token along with other details in Telerik Platform.

A device registration describes a single device that has been registered for push notifications in your Telerik Platform application. The object that represents the registration contains required system properties such as token, device model, operating system type, time zone, etc. as well as custom parameters that you have added during registration.

Later, when you send notifications to devices that run your app, you can use both the system and the custom parameters to precisely target who receives what.

Keep in mind that you cannot test push notifications in the iOS simulator. You need a physical iOS device and an Apple developer’s account.

Registering a device with Telerik Platform includes implementing these events of the UIApplicationDelegate Backend Services iOS SDK protocol to handle important events about push notifications:

  • didRegisterForRemoteNotificationsWithDeviceToken
  • didFailToRegisterForRemoteNotificationsWithError
  • didReceiveRemoteNotification

Contents:

Initializing the Device

Initialization calls the Apple Push Notification Service (APNS) and returns a push token. To initialize the device, call the following Backend Services iOS SDK method:

[application registerForRemoteNotifications];
application.registerForRemoteNotifications()

Registering the Device

The first time the application runs it prompts the user to allow push notifications. On confirmation, the following app delegate method is called, in which we set the device token in the Everlive instance and then we register the device with Telerik Platform:

- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)newDeviceToken
    [Everlive setDeviceToken:newDeviceToken];

    [[Everlive sharedInstance]registerDevice:^(BOOL success, NSError *error) 
    {
        if (error != nil) {
            [self.infoLabel setText:error.localizedDescription];
        } else {
            [self.infoLabel setText:@"Your device is now registered and ready to receive push notifications."];
        }
    }];

func application(application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: NSData) {
    Everlive.setDeviceToken(deviceToken)
    Everlive.sharedInstance().registerDevice { (success: Bool, error : NSError!) -> Void in
        if((error) != nil){
            self.infoLabel.text = error.localizedDescription
        } else {
            self.infoLabel.text = "Your device is now registered and ready to receive push notifications."
        }
    }
}

To include custom parameters in the device registration, call registerDeviceWithParameters instead of registerDevice:

NSDictionary *dictionary = [NSDictionary dictionaryWithObjectsAndKeys:@"Friends", @"appName", nil];

[[Everlive sharedInstance]registerDeviceWithParameters:dictionary block:^(BOOL success, NSError *error) {
    if (error != nil) {
            [self.infoLabel setText:error.localizedDescription];
        } else {
            [self.infoLabel setText:@"Your device is now registered and ready to receive push notifications."];
        }
    }];
Everlive.sharedInstance().registerDeviceWithParameters(customParams , block: { (success: Bool, error : NSError!) -> Void in

            if((error) != nil){
                self.infoLabel.text = error.localizedDescription
            } else {
                self.infoLabel.text = "Your device is now registered and ready to receive push notifications."
            }
        })

In case there is a mismatch between the provisioning profile/App ID and the device that the application is deployed on, the didFailToRegisterForRemoteNotificationsWithError app delegate method is called instead:

- (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error
{
    NSLog(@"Registration:%@", error);
}
func application(application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: NSError) {
     println("Registration:%@", error);
}

Integrating Interactive Push Notifications

iOS versions 8 and later support a feature called Interactive Push. To utilize it, you need to declare at least one custom category as well as custom actions before initializing and registering for push notifications.

To declare a custom category:

UIMutableUserNotificationCategory *myCategory = [[UIMutableUserNotificationCategory alloc] init];
myCategory.identifier = @"MY_CATEGORY";
var myCategory:UIMutableUserNotificationCategory = UIMutableUserNotificationCategory()
myCategory.identifier = "MY_CATEGORY"

To define a set of custom actions:

UIMutableUserNotificationAction *firstAction = [[UIMutableUserNotificationAction alloc] init];
firstAction.identifier = @"FIRST_ACTION";
firstAction.title = @"First Action";
firstAction.activationMode = UIUserNotificationActivationModeForeground;
firstAction.destructive = YES;
firstAction.authenticationRequired = NO;

UIMutableUserNotificationAction *secondAction = [[UIMutableUserNotificationAction alloc] init];
secondAction.identifier = @"SECOND_ACTION";
secondAction.title = @"Second Action";
secondAction.activationMode = UIUserNotificationActivationModeForeground;
secondAction.destructive = NO;
secondAction.authenticationRequired = NO;

UIMutableUserNotificationAction *thirdAction = [[UIMutableUserNotificationAction alloc] init];
thirdAction.identifier = @"THIRD_ACTION";
thirdAction.title = @"Third Action";
thirdAction.activationMode = UIUserNotificationActivationModeBackground; 
thirdAction.destructive = NO;
thirdAction.authenticationRequired = NO;
// create notification actions
var firstAction:UIMutableUserNotificationAction = UIMutableUserNotificationAction() 
firstAction.identifier = "FIRST_ACTION"
firstAction.title = "First Action"
firstAction.activationMode = UIUserNotificationActivationMode.Foreground
firstAction.destructive = true
firstAction.authenticationRequired = false

var secondAction:UIMutableUserNotificationAction = UIMutableUserNotificationAction()
secondAction.identifier = "SECOND_ACTION"
secondAction.title = "Second Action"
secondAction.activationMode = UIUserNotificationActivationMode.Foreground
secondAction.destructive = false
secondAction.authenticationRequired = false

var thirdAction:UIMutableUserNotificationAction = UIMutableUserNotificationAction()
thirdAction.identifier = "THIRD_ACTION"
thirdAction.title = "Third Action"
thirdAction.activationMode = UIUserNotificationActivationMode.Background 
thirdAction.destructive = false
thirdAction.authenticationRequired = false

To set the category's default and minimal context actions:

[myCategory setActions:@[firstAction, secondAction, thirdAction] forContext:UIUserNotificationActionContextDefault];

[myCategory setActions:@[thirdAction, firstAction] forContext:UIUserNotificationActionContextMinimal];
var defaultActions:NSArray = [firstAction, secondAction, thirdAction] // will be shown in a alert style notification as options
var minimalActions:NSArray = [thirdAction, firstAction] // will be shown in the lock screen and in the banner

// set the minimum actions in regard to the different contexts in which the notification is displayed
myCategory.setActions(defaultActions, forContext: UIUserNotificationActionContext.Default)
myCategory.setActions(minimalActions, forContext: UIUserNotificationActionContext.Minimal)

Finally you need to register the notification settings for the application, effectively registering the Category that you created:

 NSSet *categories = [NSSet setWithObjects:myCategory, nil];
 UIUserNotificationType notificationTypes = UIUserNotificationTypeAlert | UIUserNotificationTypeBadge;
 UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes: notificationTypes categories:categories];

 // register the category
 [[UIApplication sharedApplication]registerUserNotificationSettings:settings];

 // register for notifications
 [application registerForRemoteNotifications];
let categories:NSSet = NSSet(object: myCategory)

// settings for user notifications
let notificationTypes:UIUserNotificationType = UIUserNotificationType.Alert | UIUserNotificationType.Badge
let notificationSettings:UIUserNotificationSettings = UIUserNotificationSettings(forTypes: notificationTypes, categories: categories)

// register a category        
UIApplication.sharedApplication().registerUserNotificationSettings(notificationSettings);

// register for notifications
application.registerForRemoteNotifications()

See Also


Start a free trial Request a demo
Contact us: +1-888-365-2779
sales@telerik.com
Copyright © 2016-2017, Progress Software Corporation and/or its subsidiaries or affiliates. All rights reserved.