Created
August 21, 2015 13:53
-
-
Save dozzman/f13372a93f3913a2cd72 to your computer and use it in GitHub Desktop.
AppDelegate with push notifications implemented
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
@UIApplicationMain | |
class AppDelegate: UIResponder, UIApplicationDelegate, GGLInstanceIDDelegate { | |
var window: UIWindow? | |
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool { | |
// register for push notifications | |
var types = UIUserNotificationType.Alert | UIUserNotificationType.Sound | UIUserNotificationType.Badge | |
var settings = UIUserNotificationSettings(forTypes: types, categories: nil) | |
application.registerUserNotificationSettings(settings) | |
application.registerForRemoteNotifications() | |
// set up NexmoClient | |
NexmoClient.start(applicationId: "app", sharedSecretKey: "key") | |
return true | |
} | |
func application(application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: NSData) { | |
// this callback returns our APNS device token which we then forward to GCM | |
// so we can receive push notifications from them | |
apnDeviceDoken = deviceToken | |
var instanceIDConfig = GGLInstanceIDConfig.defaultConfig() | |
instanceIDConfig.delegate = self | |
GGLInstanceID.sharedInstance().startWithConfig(instanceIDConfig) | |
var registrationOptions = [kGGLInstanceIDRegisterAPNSOption:deviceToken, | |
kGGLInstanceIDAPNSServerTypeSandboxOption:true] | |
GGLInstanceID.sharedInstance().tokenWithAuthorizedEntity(gcmSenderId, scope: kGGLInstanceIDScopeGCM, options: registrationOptions, handler: self.handleToken) | |
} | |
func handleToken(token: String!, error: NSError!) { | |
// this function is the callback for our GCM token which will be used by sdk service to send push | |
// notifications to this specifid device | |
NexmoClient.setGcmToken(token) | |
println("gcmToken = \(token)") | |
} | |
func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject]) { | |
// this callback receives ALL push notifications sent to this specific device | |
// from ALL sources | |
GCMService.sharedInstance().appDidReceiveMessage(userInfo) | |
if let pin = userInfo["pin"] as? String { | |
println("received pin code \(pin)") | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment