Skip to content

Azure Notification Hubs SDK for iOS v3.0.0-preview1

Pre-release
Pre-release
Compare
Choose a tag to compare
@mpodwysocki mpodwysocki released this 14 May 15:32
· 7 commits to v3-preview1 since this release
25d5b46

The Azure Notification Hubs SDK for iOS 3.0.0-preview1 is the first preview for the new 3.0.0 SDK. This SDK contains a new API to help developers familiar with AppCenter Push migrate their apps the underlying technology, Azure Notification Hubs.

Getting started with the SDK once manually installed via the WindowsAzureMessaging.framework.zip file or using the AzureNotificationHubs-iOS CocoaPod, you can get started with the following code where you initialize the MSNotificationHub with a listen only access policy such as DefaultListenSharedAccessSignature, and the hub name. In addition, in order to receive push notifications, you must implement the MSNotificationHubDelegate protocol.

This can be done with Objective-C such as the following:

#import <WindowsAzureMessaging/WindowsAzureMessaging.h>

// Later in the AppDelegate
static NSString *const kConnectionString = @""; // A listen only access policy connection string
static NSString *const kHubName = @""; // The Azure Notification Hub Name

// Initialize the Notification Hub
[MSNotificationHub setDelegate:this];
[MSNotificationHub initWithConnectionString:kConnectionString hubName:kHubName];

// Listen for push notifications from APNS
- (void)notificationHub:(MSNotificationHub *)notificationHub didReceivePushNotification:(MSNotificationHubMessage *)message fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler {
    NSLog(@"Received notification: %@: %@", notification.title, notification.body);

    // Set completion handler to no data
    completionHandler(UIBackgroundFetchResultNoData);
}

And Swift as the following code once importing the SDK and creating the Bridging Header via importing Objective-C code into Swift.

import WindowsAzureMessaging

static let connectionString = "{Connection String}"
static let hubName = "{Hub Name}"

MSNotificationHub.setDelegate(self)
MSNotificationHub.initWithConnectionString(connectionString, hubName: hubName)

func notificationHub(_ notificationHub: MSNotificationHub!, didReceivePushNotification notification: MSNotificationHubMessage!, fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
    // Log the data
    NSLog("Received notification: %@; %@", notification.title ?? "<nil>", notification.body)

    // Set completion handler to no-data
    completionHandler(.noData)
}

Registering your device is handled automatically behind the scenes using swizzling. You can enhance the targeting experience for your device such as using tags. For example, you can add tags for your app using the following API with Objective-C.

// Add a single tag
[MSNotificationHub addTag:@"tag1"];

// Add multiple tags
[MSNotificationHub addTags:@[@"tag2", @"tag3", "tag4"]];

// Remove a tag
[MSNotificationHub removeTag:@"tag1"];

// Remove multiple tags
[MSNotificationHub addTags:@[@"tag2", @"tag3", "tag4"]];

// Get all tags
NSSet *tags = [MSNotificationHub getTags];

The same can be done using Swift with the following code.

// Add a tag
MSNotificationHub.addTag("iOS")

// Add multiple tags
MSNotificationHub addTags(["tag2", "tag3", "tag4"])

// Remove a tag
MSNotificationHub.removeTag("iOS");

// Remove multiple tags
MSNotificationHub.addTag(["tag2", "tag3", "tag4"])

// Get all tags
var tags = MSNotificationHub.getTags();