簡體   English   中英

Swift中iOS8的推送通知狀態

[英]Push notification status for iOS8 in Swift

我一直在尋找有關在iOS8的Swift中設置推送通知的適當教程,但是我覺得從Objective-C到Swift以及從iOS7到iOS8都做了很多更改,盡管我找不到關於它的任何更新代碼那。

我想知道用戶是否已按下“推送通知警報”中的“不允許”按鈕和/ 或是否曾經看到過該按鈕。

事實是,在這種情況下,我想顯示一個警報,要求用戶降低通過設置的通知。

問題:我不知道要檢查哪個變量才能知道是否應該顯示彈出窗口。

我可以使用它,但在兩種情況下,哈希值都為0,即從不顯示1-推送通知警報,顯示2-推送通知警報,但用戶推送了“不允許”。

if(UIApplication.sharedApplication().currentUserNotificationSettings().hashValue == 0){
            pushNotificationStatus = "false"
        } else {
            pushNotificationStatus = "true"
        }

您是否有最佳實踐/想法來解決此問題? 謝謝!

//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"
        }
}

從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")
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM