2017-04-25 43 views
3

我正在使用UNUserNotificationCenterDelegate(> ios 10)和其中一個委託方法,其中我可以檢查來自通知的響應總是actionIdentifier等於「com.apple.UNNotificationDefaultActionIdentifier」無論如何我做的事。 「response.notification.request.content.categoryIdentifier」是正確的,具有期望值,但request.actionIdentifier永遠不會正確(下面示例中的「mycustomactionidentifier」)。有誰知道我是否錯過了一些東西?用戶通知請求總是帶有默認動作標識符

extension NotificationManager: UNUserNotificationCenterDelegate { 


    func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Swift.Void) { 

     completionHandler([.alert,.sound]) 
    } 

    func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping() -> Swift.Void) { 

     if response.notification.request.content.categoryIdentifier == "TEST" { 
      if response.actionIdentifier == "mycustomactionidentifier" { 
       NSLog("it finally works dude!") 
      } 
     } 

     completionHandler() 
    } 
} 

我添加了動作和類別的通知中心:

let uploadAction = UNNotificationAction(identifier: "mycustomactionidentifier", title: "Uploaded", options: []) 
    let category = UNNotificationCategory(identifier: "TEST", actions: [uploadAction], intentIdentifiers: []) 
    center.setNotificationCategories([category]) 

和我發送請求把正確的標識符:

let uploadContent = UNMutableNotificationContent() 
    uploadContent.title = String(number) + " asset(s) added" 
    uploadContent.body = "Check your inventory to manage your assets!" 
    uploadContent.categoryIdentifier = "TEST" 

    let trigger = UNTimeIntervalNotificationTrigger(timeInterval: 6, repeats: false) 

    let uploadRequestIdentifier = "mycustomactionidentifier" 
    let uploadRequest = UNNotificationRequest(identifier: uploadRequestIdentifier, content: uploadContent, trigger: trigger) 
    UNUserNotificationCenter.current().add(uploadRequest, withCompletionHandler: nil) 
+0

當通知進入你的屏幕上它看起來像什麼?你能添加截圖嗎?你點擊了哪個動作? – Honey

+0

你有一個錯誤的比較。您的操作標識符是「mycustomactionidentifier」,但您已選中「mycustomidentifier」。因此編譯器會簡單地忽略你的動作標識符 – Mannopson

+0

嗨@Honey,它來作爲一個正常的通知,與我選擇的標題和正文。無論我點擊通知,它都會返回正確的categoryIdentifier,但帶有錯誤的actionIdentifier! – user2116499

回答

3

首先:註冊您的自定義操作:

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool { 
    // Override point for customization after application launch. 

    UNUserNotificationCenter.current().delegate = self 
    UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .sound]) { (granted, error) in 
     if granted { 
     // Access granted 
     } else { 
     // Access denied 
     } 
    } 

    self.registerNotificationAction() 

    return true 
} 

func registerNotificationAction() { 

    let first = UNNotificationAction.init(identifier: "first", title: "Action", options: []) 
    let category = UNNotificationCategory.init(identifier: "categoryIdentifier", actions: [first], intentIdentifiers: [], options: []) 
    UNUserNotificationCenter.current().setNotificationCategories([category]) 
} 

,並創建一個唯一的標識符的內容:

func scheduleNotification() { 

    // Create a content 
    let content = UNMutableNotificationContent.init() 
    content.title = NSString.localizedUserNotificationString(forKey: "Some title", arguments: nil) 
    content.body = NSString.localizedUserNotificationString(forKey: "Body of notification", arguments: nil) 
    content.sound = UNNotificationSound.default() 
    content.categoryIdentifier = "categoryIdentifier" 

    // Create an unique identifier for each notification 
    let identifier = UUID.init().uuidString 

    // Notification trigger 
    let trigger = UNTimeIntervalNotificationTrigger.init(timeInterval: 5, repeats: false) 

    // Notification request 
    let request = UNNotificationRequest.init(identifier: identifier, content: content, trigger: trigger) 

    // Add request 
    UNUserNotificationCenter.current().add(request, withCompletionHandler: nil) 

} 

最後:處理與它們的默認和自定義操作的通知。

extension AppDelegate: UNUserNotificationCenterDelegate { 

    func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping() -> Void) { 

     if response.notification.request.content.categoryIdentifier == "categoryIdentifier" { 

      switch response.actionIdentifier { 
      case UNNotificationDefaultActionIdentifier: 
       print(response.actionIdentifier) 
       completionHandler() 
      case "first": 
       print(response.actionIdentifier) 
       completionHandler() 
      default: 
       break; 
      } 
     } 
    } 

    func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) { 

     completionHandler([.alert, .sound]) 
    } 
} 

希望它有幫助!

第二版

這裏的結果:這將是我們的UNNotificationDefaultActionIdentifier

UNNotificationDefaultActionIdentifier

而這一次擴大了通知的版本中,我們可以處理兩個動作:

Expanded notification

+0

嗨Mannopson,我的確和你說的一樣,但是當我發送一個帶有動作標識符「X」的通知時,當我點擊通知時,我得到一個「com.apple」。UNNotificationDefaultActionIdentifier「而不是」didReceive response func「中的」X「 – user2116499

+0

@ user2116499每當通知傳遞給用戶時,用戶點擊它,UNNotificationDefaultActionIdentifier將被調用。因爲它是默認操作,它由我的建議是完美的作品 – Mannopson

+0

嗨@Mannopson,你創建了一個標識符=「first」的動作,當你點擊通知時,是不是會出現動作標識符=「第一」?這應該是行爲,如果不是這樣,那麼你的答案是正確的。謝謝! – user2116499

1

正如Mannopson所說,您可以註冊一個默認的動作標識符。 不過我以爲你需要的是另一件事:

response.notification.request.identifier

Apple's actionIdentifier description說這個參數可以包含一個你UNNotificationAction對象之一的標識符,或者它可能包含一個系統定義的標識符。這意味着你需要註冊一個,希望我是正確的(因爲我是一個快速新手)