2017-08-09 36 views
2

我有兩個本地通知,一個基於日期觸發,另一個基於時間觸發。如何區分兩個本地通知

當它們被觸發didReceive委託調用與UNNotificationDefaultActionIdentifier標識符:

func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping() -> Void) { 
    switch response.actionIdentifier { 
    case UNNotificationDismissActionIdentifier: 
     print("Dismiss Action") 
    case UNNotificationDefaultActionIdentifier: 
     // this part is called when notification is triggered 
    ...................................... 
    default: 
     print("Unknown action") 
    } 

    completionHandler() 
} 

有沒有辦法在這裏面代表對兩個通知區別?

我想根據觸發的通知採取不同的操作。

回答

1

您的迴應UNNotificationResponse。它有兩個不可改變的屬性:

  • actionIdentifier,一個String這是關聯到你已經加入到userNotificationCenter
  • notification的類別是一個UNNotification其中包含 原要求即它是UNNotificationRequest實例。

所以切換使用:response.notification.request.identifier

1

嘗試用

response.notification.request.identifier 

UNNotificationRequest有標識,因爲是展現在UNNotificationRequest.h

希望這有助於