Skip to content

Instantly share code, notes, and snippets.

@fotiDim
Last active April 16, 2018 13:14
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save fotiDim/d18dd0f0ddb91ee3babc to your computer and use it in GitHub Desktop.
Save fotiDim/d18dd0f0ddb91ee3babc to your computer and use it in GitHub Desktop.
Using Azure Notification Hubs in Swift
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
// Override point for customization after application launch.
var types: UIUserNotificationType = UIUserNotificationType.Badge |
UIUserNotificationType.Alert |
UIUserNotificationType.Sound
var settings: UIUserNotificationSettings = UIUserNotificationSettings( forTypes: types, categories: nil )
application.registerUserNotificationSettings( settings )
application.registerForRemoteNotifications()
return true
}
func application( application: UIApplication!, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: NSData! ) {
var hub : SBNotificationHub = SBNotificationHub(connectionString: "!!YOUR TOKEN HERE!!", notificationHubPath: "!!YOUR NOTIFICATION HUB PATH HERE!!")
hub.registerNativeWithDeviceToken(deviceToken, tags: nil) { (error) -> Void in
if (error != nil){
println("Error registering for notifications: %@", error);
}
}
}
func application( application: UIApplication!, didFailToRegisterForRemoteNotificationsWithError error: NSError! ) {
println( error.localizedDescription )
}
func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject]) {
println("User Info: %@", userInfo)
var alert = UIAlertController(title: "Notification", message: "Message", preferredStyle: UIAlertControllerStyle.Alert)
// alert.addAction(UIAlertAction(title: "Click", style: UIAlertActionStyle.Default, handler: nil))
self.window?.rootViewController?.presentViewController(alert, animated: true, completion: nil)
}
@JuanPabloBoero
Copy link

In Swift 2.1 , the line:

var types: UIUserNotificationType = UIUserNotificationType.Badge | UIUserNotificationType.Alert | UIUserNotificationType.Sound

changed to this:

        let types: UIUserNotificationType = [UIUserNotificationType.Badge, UIUserNotificationType.Alert, UIUserNotificationType.Sound]

        or

        let types: UIUserNotificationType = [.Badge, .Alert , .Sound]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment