I have been looking for a proper tutorial on setting push notification in Swift for iOS8 but I feel like a lot of changes have been made from Objective-C to Swift and iOS7 to iOS8, though I can't find any updated piece of code on that.
I am trying to know if the user has pushed the "Don't Allow" button in the Push Notification Alert and/ or if they ever saw it .
The fact is that I would like, in that case, to show an alert asking the user to reallow notifications through settings.
Problem: I don't know which variable to check to know if I should show the popup or not.
I could use this but the hashvalue is at 0 in both cases where 1- push notification alert was never shown, 2- push notification alert was shown but user pushed "Don't Allow".
if(UIApplication.sharedApplication().currentUserNotificationSettings().hashValue == 0){
pushNotificationStatus = "false"
} else {
pushNotificationStatus = "true"
}
Would you have any best practice/idea to solve this? Thanks you!
//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"
}
}
Since iOS 10.0 you can use this method
UNUserNotificationCenter.current().getNotificationSettings(completionHandler: { settings in
switch settings.authorizationStatus {
case .notDetermined:
print("Not determined")
case .denied: //In this case user has already seen this alert, and answered "Don't allow"
print("Denied")
case .authorized:
print("Authorised")
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.