在 Swift 中推送 iOS8 的通知状态
Posted
技术标签:
【中文标题】在 Swift 中推送 iOS8 的通知状态【英文标题】:Push notification status for iOS8 in Swift 【发布时间】:2015-08-19 06:57:57 【问题描述】:我一直在寻找关于在 ios8 的 Swift 中设置推送通知的适当教程,但我觉得从 Objective-C 到 Swift 以及从 iOS7 到 iOS8 已经进行了很多更改,尽管我找不到任何更新的部分的代码。
我想知道用户是否按下了推送通知警报中的“不允许”按钮和/或者他们是否曾经看到过。
事实是,在这种情况下,我希望显示一个警报,要求用户通过设置重新允许通知。
问题:我不知道要检查哪个变量才能知道是否应该显示弹出窗口。
我可以使用它,但哈希值在两种情况下都为 0,其中 1- 推送通知警报从未显示,2- 推送通知警报已显示但用户推送“不允许”。
if(UIApplication.sharedApplication().currentUserNotificationSettings().hashValue == 0)
pushNotificationStatus = "false"
else
pushNotificationStatus = "true"
您有解决此问题的最佳做法/想法吗? 谢谢你!
【问题讨论】:
这可能会有所帮助:***.com/questions/25111644/… 【参考方案1】://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)
可以进入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)
收到通知后,以下代表将调用:
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()
并且可以控制 UIApplication.sharedApplication().currentUserNotificationSettings() 的哈希值。
if(UIApplication.instancesRespondToSelector(Selector("registerUserNotificationSettings:")))
if(UIApplication.sharedApplication().currentUserNotificationSettings().hashValue == 0)
pushNotificationStatus = "false"
else
pushNotificationStatus = "true"
【讨论】:
我看不出它是如何回答我的问题的。我如何知道警报是否已经显示?谢谢【参考方案2】:iOS 10.0 起可以使用此方法
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")
【讨论】:
以上是关于在 Swift 中推送 iOS8 的通知状态的主要内容,如果未能解决你的问题,请参考以下文章
使用 Swift 2 将不允许 iOS 8 设备注册推送通知