User Notification request always come with default action identifier

感情迁移 提交于 2019-12-04 04:21:11

Firstly: Register your custom actions:

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])
}

And create a content with an unique identifier:

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 a 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)

}

Lastly: Handle the notification with their default and custom actions.

   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])
    }
}

Hope it helps!

Second Edition

Here's the results: This is going to be our UNNotificationDefaultActionIdentifier:

And this one is expanded version of the notification, we could handle both actions:

As Mannopson said, you can register a default action identifier. However I though what you need is another thing :

response.notification.request.identifier

From Apple's actionIdentifier description said This parameter may contain one the identifier of one of your UNNotificationAction objects or it may contain a system-defined identifier. which means you need to register one, hope I am right(as I am a newbie to swift)

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!