Skip to content

Instantly share code, notes, and snippets.

@takecian
Last active November 13, 2015 13:45
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save takecian/02e4b1d699906ed15d0d to your computer and use it in GitHub Desktop.
Save takecian/02e4b1d699906ed15d0d to your computer and use it in GitHub Desktop.
Utility functions for iOS PushNotification
class PushNotificationManager {
class var isPushNotificationEnable: Bool {
let osVersion = UIDevice.currentDevice().systemVersion
if osVersion < "8.0" {
let types = UIApplication.sharedApplication().enabledRemoteNotificationTypes()
if types == UIRemoteNotificationType.None {
// push notification disabled
return false
}else{
// push notification enable
return true
}
}else{
if UIApplication.sharedApplication().isRegisteredForRemoteNotifications() {
// push notification enable
return true
}else{
// push notification disabled
return false
}
}
}
class func requestPushNotification() {
let application = UIApplication.sharedApplication()
let osVersion = UIDevice.currentDevice().systemVersion
if osVersion < "8.0" {
application.registerForRemoteNotificationTypes(UIRemoteNotificationType.Badge |
UIRemoteNotificationType.Sound |
UIRemoteNotificationType.Alert )
}else{
var types: UIUserNotificationType = UIUserNotificationType.Badge |
UIUserNotificationType.Alert |
UIUserNotificationType.Sound
var settings: UIUserNotificationSettings = UIUserNotificationSettings(forTypes: types, categories: nil )
application.registerUserNotificationSettings(settings)
application.registerForRemoteNotifications()
}
}
class func openAppSettingPage() -> Void {
let application = UIApplication.sharedApplication()
let osVersion = UIDevice.currentDevice().systemVersion
if osVersion < "8.0" {
// not supported
}else{
let url = NSURL(string:UIApplicationOpenSettingsURLString)!
UIApplication.sharedApplication().openURL(url)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment