【发布时间】:2017-03-15 05:52:25
【问题描述】:
在IOS10中,我使用了UNMutableNotificationContent并设置了委托,
private func pushNotification() {
let content = UNMutableNotificationContent()
content.title = "aaa"
content.body = "bbb"
content.subtitle = "00000"
let trigger = UNTimeIntervalNotificationTrigger(timeInterval: 1, repeats: false)
let requestIdentifier = "com.aaaaa.bbbbb"
let request = UNNotificationRequest(identifier: requestIdentifier, content: content, trigger: trigger)
UNUserNotificationCenter.current().delegate = self
UNUserNotificationCenter.current().add(request) { error in
if error == nil {
print("Time Interval Notification scheduled: \\\\(requestIdentifier)")
}
}
}
在前台发送通知,调用了以下函数
然后我可以通过“completionHandler”在前台显示通知
@available(iOS 10.0, *)
public func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Swift.Void) {
completionHandler([.alert, .sound])
}
.
.
.
但在 IOS8 和 9 中,我使用了 UILocalNotification
private func pushNotification2() {
let notification = UILocalNotification()
notification.alertAction = "Title Message"
notification.alertBody = "Body Message"
notification.fireDate = NSDate(timeIntervalSinceNow: 0) as Date
notification.soundName = UILocalNotificationDefaultSoundName
notification.category = "INVITE_CATEGORY"
UIApplication.shared.scheduleLocalNotification(notification)
}
并在 AppDelegate 中找到这些
func application(_ application: UIApplication, didReceive notification: UILocalNotification) {
}
func application(_ application: UIApplication, handleActionWithIdentifier identifier: String?, for notification: UILocalNotification, completionHandler: @escaping () -> Void) {
}
func application(_ application: UIApplication, handleActionWithIdentifier identifier: String?, for notification: UILocalNotification, withResponseInfo responseInfo: [AnyHashable : Any], completionHandler: @escaping () -> Void) {
}
仅当我在前台发送通知时才调用第一个函数,但第一个函数没有“completionHandler” 我该怎么办?谢谢你的帮助
【问题讨论】:
标签: ios swift localnotification