Skip to content

DimensionSrl/Acapulco

Repository files navigation

Acapulco is a Swift library to register and manage push notifications.

Features

  • Ask for permissions to receive push notifications.
  • Register to receive push notifications.
  • Text, badges and sounds.
  • Silent notifications.
  • Send received receipt.
  • Send red receipt.
  • Support for optional parameters inside the notification payload.
  • Open Web page when receiving url parameter.
  • Debug notification payload.

Requirements

  • iOS 8.0+
  • Swift 1.2
  • Xcode 6.3

Communication

  • If you found a bug, open an issue.
  • If you have a feature request, open an issue.
  • If you want to contribute, submit a pull request.

Sample Project

In order to jump start, you can create the certificates in the iOS Development Portal and use the provided sample project to test the notification system. Keep in mind that you need to change the bundle-id of the application, generate the corresponding provisioning profiles and the certificates (distribution and push) as explained in the Local and Remote Notification Programming Guide.

Installation

Manually

You can integrate Alamofire into your project manually, just drag and drop the Acapulco.swift file in your project.

CocoaPods

Cocoapods support is coming, stay tuned.


Usage

Setup remote Push Notification server

In order to maintain the registered device archive and send push notifications you need a remote endpoint. Introducing ThunderBay!

ThunderBay

To speed up the process you can use the sample ThunderBay push notification server and deploy it to your server. However we've setup a demo instance that will be purged frequently, to let you jump start. You can find it at acapulco.dimension.it.

Register for remote notifications

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
    // Attempt to enable push notifications
    let types : UIUserNotificationType = .Badge | .Sound | .Alert
    let settings = UIUserNotificationSettings(forTypes: types, categories: nil)
    UIApplication.sharedApplication().registerUserNotificationSettings(settings)
    return true
}    

Those instructions are intended for iOS 8+.

Setup Acapulco

serverAddress is your endpoint. applicationKey is the application identifier, in this case provided by ThunderBay.

func application(application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: NSData) {
    Acapulco.sharedInstance.registerAPNSToken(deviceToken, serverAddress:"acapulco.dimension.it", applicationKey: "8ef1bd2601579e98")
}

Handle foreground notifications

func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject]) {        
        Acapulco.sharedInstance.didReceiveNotification(userInfo)
        let notification = NotificationFactory.PushReceivedNotification(userInfo)
        NSNotificationCenter.defaultCenter().postNotification(notification) // Post the notification
}

Handle background notifications

func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject], fetchCompletionHandler completionHandler: (UIBackgroundFetchResult) -> Void) {
    Acapulco.sharedInstance.didReceiveNotification(userInfo)
    NSUserDefaults.standardUserDefaults().setObject(userInfo, forKey: "backgroundNotification") // Save the last notification for later user
    let notification = NotificationFactory.PushReceivedNotification(userInfo)
    NSNotificationCenter.defaultCenter().postNotification(notification) // Post the notification
    completionHandler(UIBackgroundFetchResult.NewData)
}

Handle application launches to register again and update the values

We save the last notification received.

func applicationDidBecomeActive(application: UIApplication) {   
    if let backgroundNotification : NSObject = NSUserDefaults.standardUserDefaults().objectForKey("backgroundNotification") as? NSObject {            
        NSUserDefaults.standardUserDefaults().removeObjectForKey("backgroundNotification") // We don't need it anymore
        let notification = NotificationFactory.PushReceivedNotification(backgroundNotification as! [NSObject : AnyObject])
        NSNotificationCenter.defaultCenter().postNotification(notification) // Post the notification
    }
}

Setup callback to get notification type registration result

func application(application: UIApplication, didRegisterUserNotificationSettings notificationSettings: UIUserNotificationSettings) {
    application.registerForRemoteNotifications()
}

Creators

Acapulco has been developed with love in Trento, Italy by DIMENSION S.r.l..

Contributors

License

Acapulco is released under the MIT license.

About

Swift library to register and manage push notifications.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages