2017-09-21 38 views
0

我想註冊的類別,但由於某種原因,我不能在我的iPhone上,但它可以在模擬器上工作?我使用以下代碼進行註冊,然後檢查我訂閱的類別。當我檢查,同時運行在我的iPhone我的應用程序,它說0,當我檢查模擬器,它說1如何註冊通知類別?

 let snoozeAction = UNNotificationAction(identifier: "SNOOZE_ACTION", 
              title: "Snooze", 
              options: .foreground) 
    let expiredCategory = UNNotificationCategory(identifier: "TIMER_EXPIRED", 
               actions: [snoozeAction], 
               intentIdentifiers: [], 
               options: UNNotificationCategoryOptions(rawValue: 0)) 
    let center = UNUserNotificationCenter.current() 
    center.setNotificationCategories([expiredCategory]) 

然後,我

UNUserNotificationCenter.current().getNotificationCategories { categories in 
      print("These are the categories\(categories.count)") 
      print(categories.description) 
     } 

檢查我有沒有線索發生了什麼事!

回答

0

試試這個,我正在使用它,它爲我工作。

func notificationSetup() 
{ 
    if #available(iOS 10.0, *) 
    { 
    let center = UNUserNotificationCenter.current() 
     center.delegate = self 
     center.requestAuthorization(options: [.alert,.badge,.sound]) { granted,error in 
     if (error == nil) { 
     UIApplication.shared.registerForRemoteNotifications() 
     } 
    } 
    let snoozeAction = UNNotificationAction(identifier: "SNOOZE_ACTION", title: "Snooze", options: [.foreground, .authenticationRequired]) 
    let categoryOptions = UNNotificationCategoryOptions(rawValue: 0) 
    let expiredCategory = UNNotificationCategory(identifier: "TIMER_EXPIRED", actions: [snoozeAction], intentIdentifiers: [], options: categoryOptions) 
    let categories = Set([expiredCategory]) 
    center.setNotificationCategories(categories) 

    } 
} 
+0

嘿感謝回答但這不是工作的一個真正的iPhone。這雖然工作或模擬器,但...它根本沒有意義。 – user7097242

+0

你可以展示更多的代碼,因爲我在所有工作中都做了同樣的事情,而且工作非常好。 –