Created
March 26, 2015 08:35
-
-
Save igordeoliveirasa/81ead0da2c2ecdffed28 to your computer and use it in GitHub Desktop.
iOS/Swift - Push Notification - Retrieving and Saving Device Token at NSUserDefaults
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// AppDelegate.swift | |
// app | |
// | |
// Created by Igor de Oliveira Sa on 25/02/15. | |
// Copyright (c) 2015 Igor de Oliveira Sa. All rights reserved. | |
// | |
import UIKit | |
import CoreData | |
@UIApplicationMain | |
class AppDelegate: UIResponder, UIApplicationDelegate { | |
var window: UIWindow? | |
let defaults = NSUserDefaults.standardUserDefaults() | |
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool { | |
// Override point for customization after application launch. | |
// PUSH NOTIFICATION | |
let deviceToken = defaults.objectForKey(UserDefaultsContracts.KEY_DEVICE_TOKEN) as String? | |
if (deviceToken == nil) { | |
println("There is no deviceToken saved yet.") | |
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!) { | |
println("Got token data! (deviceToken)") | |
var characterSet: NSCharacterSet = NSCharacterSet( charactersInString: "<>" ) | |
var deviceTokenString: String = ( deviceToken.description as NSString ) | |
.stringByTrimmingCharactersInSet( characterSet ) | |
.stringByReplacingOccurrencesOfString( " ", withString: "" ) as String | |
println( deviceTokenString ) | |
defaults.setObject(deviceTokenString, forKey: UserDefaultsContracts.KEY_DEVICE_TOKEN) | |
} | |
func application(application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: NSError!) { | |
println("Couldn’t register: (error)") | |
} | |
} | |
You can do it like this:
let deviceToken = defaults.objectForKey("DeviceToken") as! String?
if (deviceToken == nil) {
let type: UIUserNotificationType = [UIUserNotificationType.Badge, UIUserNotificationType.Alert, UIUserNotificationType.Sound]
let setting = UIUserNotificationSettings(forTypes: type, categories: nil)
UIApplication.sharedApplication().registerUserNotificationSettings(setting)
UIApplication.sharedApplication().registerForRemoteNotifications()
}
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
let deviceToken = defaults.objectForKey(UserDefaultsContracts.KEY_DEVICE_TOKEN) as String?
Getting Error on above lines