Skip to content

Instantly share code, notes, and snippets.

@peerax
Forked from vzqwer/APNSTest.rb
Last active August 29, 2015 14:13
Show Gist options
  • Save peerax/8ff1cc1f717467837598 to your computer and use it in GitHub Desktop.
Save peerax/8ff1cc1f717467837598 to your computer and use it in GitHub Desktop.
# Install APNS gem:
# sudo gem install APNS
require 'APNS'
# gateway.sandbox.push.apple.com is default
# APNS.host = 'gateway.push.apple.com' # Production
APNS.host = 'gateway.sandbox.push.apple.com' # Development
# this is also the default. Shouldn't ever have to set this, but just in case Apple goes crazy, you can.
APNS.port = 2195
# this is the certificate file
APNS.pem = 'APNSTestingCert-Dev.pem'
device_token = 'YourLongDeviceTokenGoesHere'
APNS.send_notification(device_token, 'Hello iPhone!' )
# APNS.send_notification(device_token, :alert => 'Hello iPhone!', :badge => 1, :sound => 'default')
let SETTINGS_KEY_DEVICE_TOKEN = "com.example.deviceToken"
import UIKit
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
// Override point for customization after application launch.
// ios8
if (UIApplication.sharedApplication().respondsToSelector(Selector("registerForRemoteNotifications")))
{
var types: UIUserNotificationType = UIUserNotificationType.Badge |
UIUserNotificationType.Alert |
UIUserNotificationType.Sound
var settings: UIUserNotificationSettings = UIUserNotificationSettings( forTypes: types, categories: nil )
application.registerUserNotificationSettings( settings )
application.registerForRemoteNotifications()
}
// ios7
else
{
application.registerForRemoteNotificationTypes( UIRemoteNotificationType.Badge |
UIRemoteNotificationType.Sound |
UIRemoteNotificationType.Alert )
}
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 )
NSUserDefaults.standardUserDefaults().setObject(deviceTokenString, forKey: SETTINGS_KEY_DEVICE_TOKEN)
NSNotificationCenter.defaultCenter().postNotificationName(SETTINGS_KEY_DEVICE_TOKEN, object: nil)
}
func application(application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: NSError)
{
println("Couldn't register: \(error)")
UIAlertView(title: "Error", message: "Couldn't register: \(error)", delegate: nil, cancelButtonTitle: "Ok").show()
}
func applicationWillResignActive(application: UIApplication) {
}
func applicationDidEnterBackground(application: UIApplication) {
}
func applicationWillEnterForeground(application: UIApplication) {
}
func applicationDidBecomeActive(application: UIApplication) {
}
func applicationWillTerminate(application: UIApplication) {
}
}
import UIKit
class ViewController: UIViewController {
@IBOutlet weak var lblDeviceAPNSToken: UILabel!
var deviceToken: String? = ""
override func viewDidLoad() {
super.viewDidLoad()
NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector("updateDeviceToken"), name: SETTINGS_KEY_DEVICE_TOKEN, object: nil)
if (self.areNotificationsEnabled())
{
self.updateDeviceToken()
}
let tapRecognizer = UITapGestureRecognizer(target: self, action: Selector("shareDeviceToken"))
self.lblDeviceAPNSToken.addGestureRecognizer(tapRecognizer)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
func updateDeviceToken()
{
if let deviceToken: String = NSUserDefaults.standardUserDefaults().objectForKey(SETTINGS_KEY_DEVICE_TOKEN)? as? String
{
self.lblDeviceAPNSToken.text = deviceToken
self.deviceToken = deviceToken
}
}
@IBAction func shareDeviceToken()
{
var sharingItems = [AnyObject]()
if let text = deviceToken {
sharingItems.append(text)
}
let activityViewController = UIActivityViewController(activityItems: sharingItems, applicationActivities: nil)
self.presentViewController(activityViewController, animated: true, completion: nil)
}
deinit
{
NSNotificationCenter.defaultCenter().removeObserver(self)
}
func areNotificationsEnabled() -> Bool
{
// ios8
if (UIApplication.sharedApplication().respondsToSelector(Selector("currentUserNotificationSettings")))
{
let noticationSettings = UIApplication.sharedApplication().currentUserNotificationSettings()
if (noticationSettings == nil
|| noticationSettings.types == UIUserNotificationType.None)
{
return false
}
return true
}
// ios7
let types = UIApplication.sharedApplication().enabledRemoteNotificationTypes()
if (types == nil || types == UIRemoteNotificationType.None)
{
return false
}
else
{
return true
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment