//Write belkow code in Appdelegate.m in didfinishLaunching mehod..
Register for Push Notitications, if running iOS 8
if application.respondsToSelector("registerUserNotificationSettings:") {
let types:UIUserNotificationType = (.Alert | .Badge | .Sound)
let settings:UIUserNotificationSettings = UIUserNotificationSettings(forTypes: types, categories: nil)
application.registerUserNotificationSettings(settings)
application.registerForRemoteNotifications()
} else {
// Register for Push Notifications before iOS 8
application.registerForRemoteNotificationTypes(.Alert | .Badge | .Sound)
}
you can get in didFailToRegisterForRemoteNotificationsWithError
func application(
application: UIApplication,
didRegisterForRemoteNotificationsWithDeviceToken deviceToken: NSData
) {
//Process the deviceToken and send it to your server
let trimEnds = {
deviceToken.description.stringByTrimmingCharactersInSet(
NSCharacterSet(charactersInString: "<>"))
}
let cleanToken = {
trimEnds.stringByReplacingOccurrencesOfString(
" ", withString: "", options: nil)
}
}
func application(
application: UIApplication,
didFailToRegisterForRemoteNotificationsWithError error: NSError
) {
//Log an error for debugging purposes, user doesn't need to know
NSLog("Failed to get token; error: %@", error)
}
On Receiving notification following delegate will call:
func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject]) {
println("Recived: \(userInfo)")
//Parsing userinfo:
var temp : NSDictionary = userInfo
if let info = userInfo["aps"] as? Dictionary<String, AnyObject>
{
var alertMsg = info["alert"] as! String
var alert: UIAlertView!
alert = UIAlertView(title: "", message: alertMsg, delegate: nil, cancelButtonTitle: "OK")
alert.show()
}
}
And You can control the hash value of UIApplication.sharedApplication().currentUserNotificationSettings().
if(UIApplication.instancesRespondToSelector(Selector("registerUserNotificationSettings:"))){
if(UIApplication.sharedApplication().currentUserNotificationSettings().hashValue == 0){
pushNotificationStatus = "false"
} else {
pushNotificationStatus = "true"
}
}