2017-08-09 68 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

希望这有助于