在iOS 10中,苹果更新了其通知库,并将本地通知和推送通知分离为新的框架。
这个库可以轻松设置本地通知,并包括使用[ .None, .Minute, .Hourly, .Daily, .Monthly, .Yearly]进行重复通知的便捷配置。
它还包括所有新特性,包括插入附件和更改通知的启动图像。
特性
- 易于重复通知
- 基于位置的通知
- 类别操作按钮
- 队列强制64个通知限制
要求
- iOS 10.0+
- Xcode 8.0+
安装
CocoaPods
您可以使用 CocoaPods 将 DLLocalNotifications
添加到您的 Podfile
中来安装它
platform :ios, '10.0'
use_frameworks!
target 'MyApp' do
pod 'DLLocalNotifications'
end
注意:您的 iOS 部署目标必须为 10.0+
用法
一次性触发通知(任何日期)
在一定时间间隔内从指定日期重复到另一个日期的通知
注意:如果需要通知重复,则需要基于日期组件创建通知
// The date you would like the notification to fire at
let triggerDate = Date().addingTimeInterval(300)
let firstNotification = DLNotification(identifier: "firstNotification", alertTitle: "Notification Alert", alertBody: "You have successfully created a notification", date: triggerDate)
let scheduler = DLNotificationScheduler()
scheduler.scheduleNotification(notification: firstNotification)
scheduler.scheduleAllNotifications()
基于日期组件的重复通知
重复配置在 repeats 参数中选取,可以是 [ .none, .minute, .hourly, .daily, .monthly, .yearly]。
// The date you would like the notification to fire at :35 mins every hour
var dateComponents = DateComponents()
dateComponents.minute = 35
dateComponents.second = 0
let firstNotification = DLNotification(identifier: "hourlyNotification", alertTitle: "Notification Alert", alertBody: "You have successfully created a notification", fromDateComponents: dateComponents, repeatInterval: .hourly)
let scheduler = DLNotificationScheduler()
scheduler.scheduleNotification(notification: firstNotification)
scheduler.scheduleAllNotifications()
在特定时间间隔内重复从某一天到另一天的通知
这可以用于为指定时间段内的特定时间间隔设置重复通知。
let scheduler = DLNotificationScheduler()
// This notification repeats every 15 seconds from a time period starting from 15 seconds from the current time till 5 minutes from the current time
scheduler.repeatsFromToDate(identifier: "First Notification", alertTitle: "Multiple Notifications", alertBody: "Progress", fromDate: Date().addingTimeInterval(15), toDate: Date().addingTimeInterval(300) , interval: 15, repeats: .none )
scheduler.scheduleAllNotifications()
注意:由于这个库负责处理 64 个通知限制,您还需要在 AppDelegate 文件中调用 scheduler.scheduleAllNotifications()。
修改通知元素
您可以在安排通知之前修改通知元素。公共可访问变量包括
repeatInterval, alertBody, alertTitle, soundName, fireDate, attachments, launchImageName, category
let firstNotification = DLNotification(identifier: "firstNotification", alertTitle: "Notification Alert", alertBody: "You have successfully created a notification", date: Date(), repeats: .minute)
// You can now change the repeat interval here
firstNotification.repeatInterval = .yearly
// You can add a launch image name
firstNotification.launchImageName = "Hello.png"
let scheduler = DLNotificationScheduler()
scheduler.scheduleNotification(notification: firstNotification)
scheduler.scheduleAllNotifications()
基于位置的提醒
当用户进入地理围栏区域时触发通知。
let center = CLLocationCoordinate2D(latitude: 37.335400, longitude: -122.009201)
let region = CLCircularRegion(center: center, radius: 2000.0, identifier: "Headquarters")
region.notifyOnEntry = true
region.notifyOnExit = false
let locationNotification = DLNotification(identifier: "LocationNotification", alertTitle: "Notification Alert", alertBody: "You have reached work", region: region )
let scheduler = DLNotificationScheduler()
scheduler.scheduleNotification(notification: locationNotification)
scheduler.scheduleAllNotifications()
向通知添加操作按钮
let scheduler = DLNotificationScheduler()
let standingCategory = DLCategory(categoryIdentifier: "standingReminder")
standingCategory.addActionButton(identifier: "willStand", title: "Ok, got it")
standingCategory.addActionButton(identifier: "willNotStand", title: "Cannot")
scheduler.scheduleCategories(categories: [standingCategory])
在安排通知之前,不要忘记使用以下方式设置通知类别
notification.category = "standingReminder"
取消通知
scheduler.cancelNotification(notification: notification)
贡献
我们希望您为 DLLocalNotifications 贡献,更多详细信息请参阅 LICENSE
文件。
元数据
Devesh Laungani – @d7laungani
在MIT许可下分发。更多信息请参阅 LICENSE
。