Skip to content

Instantly share code, notes, and snippets.

@igordeoliveirasa
Created March 26, 2015 08:35
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save igordeoliveirasa/81ead0da2c2ecdffed28 to your computer and use it in GitHub Desktop.
Save igordeoliveirasa/81ead0da2c2ecdffed28 to your computer and use it in GitHub Desktop.
iOS/Swift - Push Notification - Retrieving and Saving Device Token at NSUserDefaults
//
// 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)")
}
}
@cloudjanak
Copy link

let deviceToken = defaults.objectForKey(UserDefaultsContracts.KEY_DEVICE_TOKEN) as String?

    if (deviceToken == nil) {
        print("There is no deviceToken saved yet.")
        var types: UIUserNotificationType = UIUserNotificationType.Badge | UIUserNotificationType.Alert |
            UIUserNotificationType.Sound

Getting Error on above lines

@fatihyildizhan
Copy link

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