Notifiable-iOS is a set of utility classes to easily integrate with Notifiable-Rails.
Objective-C Swift Ruby
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Failed to load latest commit information.
Framework
Notifiable-iOS.xcodeproj
Notifiable-iOS
Notifiable-iOSIntegrationTests
Notifiable-iOSUnitTests
Sample
Support Libraries
.gitignore
.gitmodules
.ruby-gemset
.ruby-version
LICENSE
Notifiable-iOS.podspec
README.md

README.md

Notifiable-iOS

Notifiable-iOS is a set of utility classes to easily integrate with Notifiable-Rails.

It handles device token registration and takes care of retrying failed requests and avoiding duplicate registrations.

Registering existing token for different user will result in token being reassigned.

Setup

Project integration

The Notifiable-iOS for iOS is avaliable on CocoaPods. To install using it, just add the line to your Podfile:

pod 'Notifiable-iOS'

If you are not using CocoaPods, you can clone this project and import the files into your own project. This libraries uses AFNetworking as a dependency and is configured as a submodule.

You can see an example of the implementation in the Sample folder.

Use

To use the FWTNotifiableManager, create a new object passing your server URL, application access id, application secret key. You can, also, provide blocks that will be used to notify your code when the device is registered for remote notifications and when it receives a new notification.

self.manager = FWTNotifiableManager(url: <<SERVER_URL>>, accessId: <<USER_API_ACCESS_ID>>, secretKey: <<USER_API_SECRET_KEY>>, didRegisterBlock: { [unowned self] (manager, token) -> Void in 
    ...
}, andNotificationBlock:{ [unowned self] (manager, device, notification) -> Void in
    ...
})

Foward application events

Forward device token to FWTNotifiableManager:

func application(application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: NSData) 
{
    FWTNotifiableManager.application(application, didRegisterForRemoteNotificationsWithDeviceToken: deviceToken)
}

Foward new notifications to FWTNotifiableManager:

func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject]) {
    if (FWTNotifiableManager.applicationDidReceiveRemoteNotification(userInfo)) {
        print("Notifiable server notification")
    }
}

Listen to application events

To be notified when the device is registered for remote notifications or received a remote notification, you can use the blocks in the FWTNotifiableManager init or register an object as a FWTNotifiableManagerListener

func viewDidLoad() {
    super.viewDidLoad()
    FWTNotifiableManager.registerManagerListener(self)
}

//MARK: FWTNotifiableManagerListener methods
func applicationDidRegisterForRemoteNotificationsWithToken(token: NSData) {
    ...
}

func applicationDidReciveNotification(notification: [NSObject : AnyObject]) {
    ...
}

func notifiableManager(manager: FWTNotifiableManager, didRegisterDevice device: FWTNotifiableDevice) {
    ...
}

func notifiableManager(manager: FWTNotifiableManager, didFailToRegisterDeviceWithError error: NSError) {
    ...
}

Registering a device

After the application:didRegisterForRemoteNotificationsWithDeviceToken: you can use the device token to register this device in the Notifiable-Rails server.

You can register an anonymous device:

override func viewDidLoad() {
    super.viewDidLoad()

    //1 - Config manager
    self.manager = FWTNotifiableManager(URL: serverURL, accessId: accessID, secretKey: secretKey(), didRegisterBlock: { [unowned self] (manager, token) -> Void in
        //3 - Register device
        self.registerDevice(manager, token: token)
    }, andNotificationBlock: nil)

    //2 - Request for permission
    let notificationSettings = UIUserNotificationSettings(forTypes: [.Alert, .Badge, .Sound], categories: nil)
    UIApplication.sharedApplication().registerUserNotificationSettings(notificationSettings)
}

func registerDevice(manager:FWTNotifiableManager, token:NSData) {
    manager. registerAnonymousDeviceWithName("iPhone", locale: NSLocale.autoupdatingCurrentLocale(), deviceInformation: ["onsite":true]) { (device, error) -> Void in
        ...
    }
}

Or register a device associated to a user:

func registerDevice(manager:FWTNotifiableManager, token:NSData) {
    manager.registerDeviceWithName("device", userAlias: "user", locale: NSLocale.autoupdatingCurrentLocale(), deviceInformation: ["onsite":true]) { (device, error) -> Void in
        ...       
    }
}

The deviceInformation dictionary holds some extended parameters that represents the metadata about the device, here you could send the current latitude and longitude of the device, for example.

You can access the registered device informations in the currentDevice property of the manager:

let device = self.manager.currentDevice

Updating the device informations

Once that the device is registered, you can update the device informations:

self.manager.updateDeviceToken(nil, deviceName: "device", userAlias: "user", location: NSLocale.currentLocale(), deviceInformation: ["onsite":true]) { (device, error) -> Void in
    ...
}

You can, also, associate the device to other user:

self.manager.associateDeviceToUser(user, completionHandler: { (device, error) -> Void in
    ...
}

Or anonymise the token:

self.manager.anonymiseTokenWithCompletionHandler { (device, error) -> Void in
    ...
}

Unregister a device

You may wish to unregister a device token (on user logout or in-app opt out perhaps).

self.manager.unregisterTokenWithCompletionHandler { (device, error) -> Void in
    ...
}

Marking a notification as opened

When the application is launched or has received a remote notification, you can relay the fact it was opened by the user to Notifiable-Rails.

The userInfo here should be the payload received from the notification.

func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject]) {       
    self.manager.applicationDidReceiveRemoteNotification(userInfo);
}

List devices associated to user

Once that the device is registered, you can request a list of devices registered for the user:

self.manager.listDevicesRelatedToUserWithCompletionHandler { [weak self] (devices, error) -> Void in
    ...
}

If the device is registered as anonymous, the list will contain only the current device.

LICENSE

Apache License Version 2.0