KQNotification
一个 Swift 类,帮助您创建本地、每日、每周通知,并轻松为用户通知添加动作。
安装
下载 项目,并将 Source 文件夹复制到你的项目中,然后你就可以在任何文件中使用它了
使用方法
注册
在 didFinishLaunchingWithOptions 中注册通知
KQNotification.shared.registerNotification(withOptions: [.alert, .badge, .sound], completion: nil)
时间通知
// Notification after times (seconds)
KQNotification.shared.notification(identifier: "notificationID", title: "KQNotification", body: "Local notification", after: 100, completion: nil)
// Notification after times (seconds) and repeat
KQNotification.shared.repeatsNotification(identifier: "repeatNotificationID", title: "KQNotification", body: "Repeat notification", after: 100, completion: nil)
// Notification after times (seconds) with actions
let snoozeAction = UNNotificationAction(identifier: "Snooze", title: "Snooze", options: [])
let deleteAction = UNNotificationAction(identifier: "Delete", title: "Delete", options: [.destructive])
KQNotification.shared.actionsNotification(identifier: "actionNotificationID", title: "KQNotification", body: "Actions notification", actions: [snoozeAction, deleteAction], after: 60, repeats: true, completion: nil)
每日通知
// Daily notification
KQNotification.shared.dailyNotification(identifier: "dailyNotificationID", title: "KQNotification", body: "Daily notification", date: Date(), completion: nil)
// Daily notification with actions
let snoozeAction = UNNotificationAction(identifier: "Snooze", title: "Snooze", options: [])
let deleteAction = UNNotificationAction(identifier: "Delete", title: "Delete", options: [.destructive])
KQNotification.shared.dailyActionsNotification(identifier: "dailyNotificationID", title: "KQNotification", body: "Daily notification with actions", actions: [snoozeAction, deleteAction], date: Date(), completion: nil)
每周通知
// Daily notification
KQNotification.shared.weeklyNotification(identifier: "weeklyNotificationID", title: "KQNotification", body: "Weekly notification", date: Date(), completion: nil)
// Daily notification with actions
let snoozeAction = UNNotificationAction(identifier: "Snooze", title: "Snooze", options: [])
let deleteAction = UNNotificationAction(identifier: "Delete", title: "Delete", options: [.destructive])
KQNotification.shared.weeklyActionsNotification(identifier: "weeklyNotificationID", title: "KQNotification", body: "Weekly notification with actions", actions: [snoozeAction, deleteAction], date: Date(), completion: nil)
通知代理
// Set delegate
KQNotification.shared.notificationCenter.delegate = self
// Implement UNUserNotificationCenterDelegate
func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
switch response.actionIdentifier {
case UNNotificationDefaultActionIdentifier:
print("Default action")
case UNNotificationDismissActionIdentifier:
print("Dismiss action")
case "Snooze":
print("Snooze")
case "Delete":
print("Delete")
default:
print("Unknown action")
break
}
completionHandler()
}
func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
completionHandler([.alert, .sound])
}
许可协议
MIT许可协议(MIT)
版权所有 (c) 2015 Quoc Khai