UNUserNotificationCenter Swift - 在特定情况下不触发本地通知
UNUserNotificationCenter Swift - Local Notification Not Firing in specific cases
我在应用程序中使用本地通知时遇到问题。
我正在安排包含自定义内容和自定义操作类别的本地通知。它在 iOS 11 中安排和触发,但在 iOS 10 中没有。
根据文档,它也应该在 iOS 10 中工作。
请检查我的代码,并告诉我为什么它在这种特定情况下不起作用。
代码如下。
AppDelegate.swift
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
.......
if #available(iOS 10.0, *) {
UNUserNotificationCenter.current().delegate = self
}
.......
return true
}
func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
completionHandler([.alert, .sound, .badge])
}
func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
print("Local Notification :: ", response)
}
自定义操作代码
func configureNotificationCenter() {
let actionRemindAgain = UNNotificationAction(identifier: NTConstants.LocalNotification.action.remindMeAgain, title: "Remind me Again!⏰", options: [.authenticationRequired])
let actionViewDetails = UNNotificationAction(identifier: NTConstants.LocalNotification.action.viewDetails, title: "View Details!", options: [.foreground])
let actionClose = UNNotificationAction(identifier: NTConstants.LocalNotification.action.dismiss, title: "Dismiss", options: [])
//Event Category
let eventCategory = UNNotificationCategory(identifier: NTConstants.LocalNotification.category.reminder, actions: [actionRemindAgain, actionViewDetails, actionClose], intentIdentifiers: [], options: [])
//Register Category
UNUserNotificationCenter.current().setNotificationCategories([eventCategory])
}
日程代码
func scheduleLocalNotification() {
self.configureNotificationCenter()
//Local Notification
let content = UNMutableNotificationContent()
content.title = notifyTitle
content.subtitle = notifySubTitle
content.body = notifyDesc
//Category
content.categoryIdentifier = "reminder"
//Trigger
let scheduleDate = Date(timeIntervalSince1970: TimeInterval(scheduleTimeInterval / 1000)) //My Schedule Data
let triggerDate = Calendar.current.date(byAdding: .hour, value: -2, to: scheduleDate) // Should be call before 2 hours
let dateComponents = Calendar.current.dateComponents([.year, .month, .day, .hour, .minute], from: triggerDate!)
let trigger = UNCalendarNotificationTrigger(dateMatching: dateComponents, repeats: false)
//Request
let request = UNNotificationRequest(identifier: "notification_\((notifyId))", content: content, trigger: trigger)
//Add Request
UNUserNotificationCenter.current().add(request) { (error) in
if let error = error {
NTPrint("Unable to add notification : \(error.localizedDescription)")
}
else {
NTPrint("Scheduled :: ", request.identifier)
}
}
}
此代码成功运行并安排了本地通知。结果如下:
<UNNotificationRequest: 0x18d65360;
identifier: notification_111,
content: <UNNotificationContent: 0x18daead0;
title: Event..!!,
subtitle: 31 Mar at 2:00 PM to 01 Apr at 12:00 PM,
body: ,
categoryIdentifier: reminder,
launchImageName: ,
peopleIdentifiers: (),
threadIdentifier: ,
attachments: (),
badge: (null),
sound: (null),
hasDefaultAction: YES,
defaultActionTitle: (null),
shouldAddToNotificationsList: YES,
shouldAlwaysAlertWhileAppIsForeground: NO,
shouldLockDevice: NO,
shouldPauseMedia: NO,
isSnoozeable: NO,
fromSnooze: NO,
darwinNotificationName: (null),
darwinSnoozedNotificationName: (null),
trigger: <UNCalendarNotificationTrigger: 0x18d51470;
dateComponents: <NSDateComponents: 0x18da3320>
Calendar Year: 2018
Month: 3
Leap month: no
Day: 31
Hour: 12
Minute: 00,
repeats: NO
>>
中午 12 点设置正确,因为我在 2 小时前设置。
它在 iOS 11 台设备上运行良好。但在 iOS 10.
中不起作用
更新的代码有什么问题吗?或者还有什么需要处理的。
请告诉我解决方案。
提前致谢。
生成本地通知。
Content.body 不能为 null 或空白
万一在解析blank到body时,iOS10不会生成本地推送。但它在 iOS 11.
中有效
Content.body 的值是关键问题。
谢谢。
我在应用程序中使用本地通知时遇到问题。
我正在安排包含自定义内容和自定义操作类别的本地通知。它在 iOS 11 中安排和触发,但在 iOS 10 中没有。
根据文档,它也应该在 iOS 10 中工作。
请检查我的代码,并告诉我为什么它在这种特定情况下不起作用。
代码如下。
AppDelegate.swift
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
.......
if #available(iOS 10.0, *) {
UNUserNotificationCenter.current().delegate = self
}
.......
return true
}
func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
completionHandler([.alert, .sound, .badge])
}
func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
print("Local Notification :: ", response)
}
自定义操作代码
func configureNotificationCenter() {
let actionRemindAgain = UNNotificationAction(identifier: NTConstants.LocalNotification.action.remindMeAgain, title: "Remind me Again!⏰", options: [.authenticationRequired])
let actionViewDetails = UNNotificationAction(identifier: NTConstants.LocalNotification.action.viewDetails, title: "View Details!", options: [.foreground])
let actionClose = UNNotificationAction(identifier: NTConstants.LocalNotification.action.dismiss, title: "Dismiss", options: [])
//Event Category
let eventCategory = UNNotificationCategory(identifier: NTConstants.LocalNotification.category.reminder, actions: [actionRemindAgain, actionViewDetails, actionClose], intentIdentifiers: [], options: [])
//Register Category
UNUserNotificationCenter.current().setNotificationCategories([eventCategory])
}
日程代码
func scheduleLocalNotification() {
self.configureNotificationCenter()
//Local Notification
let content = UNMutableNotificationContent()
content.title = notifyTitle
content.subtitle = notifySubTitle
content.body = notifyDesc
//Category
content.categoryIdentifier = "reminder"
//Trigger
let scheduleDate = Date(timeIntervalSince1970: TimeInterval(scheduleTimeInterval / 1000)) //My Schedule Data
let triggerDate = Calendar.current.date(byAdding: .hour, value: -2, to: scheduleDate) // Should be call before 2 hours
let dateComponents = Calendar.current.dateComponents([.year, .month, .day, .hour, .minute], from: triggerDate!)
let trigger = UNCalendarNotificationTrigger(dateMatching: dateComponents, repeats: false)
//Request
let request = UNNotificationRequest(identifier: "notification_\((notifyId))", content: content, trigger: trigger)
//Add Request
UNUserNotificationCenter.current().add(request) { (error) in
if let error = error {
NTPrint("Unable to add notification : \(error.localizedDescription)")
}
else {
NTPrint("Scheduled :: ", request.identifier)
}
}
}
此代码成功运行并安排了本地通知。结果如下:
<UNNotificationRequest: 0x18d65360;
identifier: notification_111,
content: <UNNotificationContent: 0x18daead0;
title: Event..!!,
subtitle: 31 Mar at 2:00 PM to 01 Apr at 12:00 PM,
body: ,
categoryIdentifier: reminder,
launchImageName: ,
peopleIdentifiers: (),
threadIdentifier: ,
attachments: (),
badge: (null),
sound: (null),
hasDefaultAction: YES,
defaultActionTitle: (null),
shouldAddToNotificationsList: YES,
shouldAlwaysAlertWhileAppIsForeground: NO,
shouldLockDevice: NO,
shouldPauseMedia: NO,
isSnoozeable: NO,
fromSnooze: NO,
darwinNotificationName: (null),
darwinSnoozedNotificationName: (null),
trigger: <UNCalendarNotificationTrigger: 0x18d51470;
dateComponents: <NSDateComponents: 0x18da3320>
Calendar Year: 2018
Month: 3
Leap month: no
Day: 31
Hour: 12
Minute: 00,
repeats: NO
>>
中午 12 点设置正确,因为我在 2 小时前设置。
它在 iOS 11 台设备上运行良好。但在 iOS 10.
中不起作用
更新的代码有什么问题吗?或者还有什么需要处理的。
请告诉我解决方案。
提前致谢。
生成本地通知。
Content.body 不能为 null 或空白
万一在解析blank到body时,iOS10不会生成本地推送。但它在 iOS 11.
中有效Content.body 的值是关键问题。
谢谢。