Created
June 20, 2015 12:10
-
-
Save nezhyborets/427a3e8eb403cea42b61 to your computer and use it in GitHub Desktop.
ONXPushManager subclass example
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
// | |
// PushManager.swift | |
// Debts | |
// | |
// Created by Alexei on 13.04.15. | |
// Copyright (c) 2015 Onix. All rights reserved. | |
// | |
import UIKit | |
import AudioToolbox | |
class PushManager: ONXPushManager { | |
class var manager : PushManager { | |
struct Static { | |
static let instance : PushManager = PushManager() | |
} | |
return Static.instance | |
} | |
override func actFromPush(pushInfo: PushInfo) { | |
if let title = pushInfo.userInfo["aps"]?["alert"] as? String { | |
if let window = UIApplication.sharedApplication().delegate?.window { | |
if let controller = window?.rootViewController { | |
AudioServicesPlayAlertSound(1015) | |
TSMessage.showNotificationInViewController(controller.presentedViewController ?? controller, | |
title: title, | |
subtitle: nil, | |
type: .Success) | |
} | |
} | |
} | |
} | |
override func updatePushesWithLatestToken() { | |
let sessionToken = ServerManager.authToken() | |
if sessionToken != nil { | |
if let deviceToken = self.latestToken { | |
var request: BaseRequest? | |
if let savedToken = self.savedPushToken() { | |
if savedToken != deviceToken { | |
//UPDATE REQUEST | |
request = UpdatePushTokenRequest(savedToken: savedToken, newToken: deviceToken) | |
} else { | |
println("updatePushesWithLatestToken - not updating - same token") | |
} | |
} else { | |
//POST REQUEST | |
request = PostDeviceRequest(pushToken: deviceToken) | |
} | |
if let uRequest = request { | |
uRequest.completionBlock = { (responseJson, error) -> () in | |
if let uError = error { | |
Flurry.logError("Registration for push notifications error", | |
message: "response string: \(responseJson); request: \(uRequest)", | |
error: uError) | |
} else { | |
dispatch_async(dispatch_get_main_queue(), { () -> Void in | |
self.savePushToken(deviceToken) | |
}) | |
} | |
} | |
ServerManager().runRequest(uRequest) | |
} | |
} | |
//Debug only | |
// else { | |
// println("updatePushesWithLatestToken - no latestToken"); | |
// | |
// dispatch_async(dispatch_get_main_queue(), { () -> Void in | |
// errorAlert("No token found for Push Notification registration. You won't recieve push notifications for this device. Please try to relogin.") | |
// }) | |
// } | |
} else { | |
println("updatePushesWithLatestToken - no authToken"); | |
} | |
} | |
override func deleteTokenFromBackend() { | |
if let token = self.savedPushToken() { | |
let request = DeletePushTokenReq(tokenToDelete: token) | |
request.completionBlock = { (responseJson, error) -> () in | |
println("deleteTokenFromBackend error \(error)") | |
println("updatePdeleteTokenFromBackendushesWithLatestToken json \(responseJson)") | |
if let uError = error { | |
Flurry.logError("Unregister pushes error", message: "response string: \(responseJson); request: \(request)", error: uError) | |
} | |
} | |
ServerManager().runRequest(request) | |
} | |
self.savePushToken(nil) | |
} | |
override func handleApplicationDidBecomeActive(app: UIApplication) { | |
super.handleApplicationDidBecomeActive(app) | |
UIApplication.sharedApplication().applicationIconBadgeNumber = 0 | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment