已安排本地通知,但未在挂起和未运行模式下传递
Posted
技术标签:
【中文标题】已安排本地通知,但未在挂起和未运行模式下传递【英文标题】:Local notifications are scheduled, but not delivered in suspended and not running modes 【发布时间】:2019-04-10 10:22:11 【问题描述】:我想为我的应用程序添加后台提取,当静默远程通知到达时(内容可用:1),所以当它到达时,我在本地触发后台更新,检查新数据,如果是,则生成本地为用户推送通知。但是,它只有在应用程序处于后台、未挂起或未运行状态时才能完美运行。在那些状态下,我让它获取一些数据,但没有触发本地通知(它是在设备的控制台中创建的),有什么想法可以解决这个问题吗?
这是我生成推送的代码(按预期工作)
func generatePushNotification(with title: String, body: String, badge: Int = 1)
let content = UNMutableNotificationContent()
content.badge = badge as NSNumber
content.title = title
content.body = body
content.sound = UNNotificationSound.default
let trigger = UNTimeIntervalNotificationTrigger(timeInterval: 1,
repeats: false)
let notification = UNNotificationRequest(identifier: "pushLocal", content: content, trigger: trigger)
UNUserNotificationCenter.current().add(notification) (error) in
if error != nil
NSLog((error?.localizedDescription)!)
这是我的 notificationFetch 代码
func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void)
networkManager.getScheduleForGroup(group) (resp, error) in
self.generatePushNotification(with: "Back", body: "ground")
completionHandler(.newData)
completionHandler(.noData)
这是控制台输出:
dasd CANCELED: com.apple.pushLaunch.**:297454 <private>!
SpringBoard Saving notification 802A-AE8C: 0 [ hasAlertContent: 0, shouldPresentAlert: 1 ]
SpringBoard Delivered user visible push notification 802A-AE8C
SpringBoard Load 0 pending notification dictionaries
SpringBoard Adding notification 802A-AE8C to destinations 0 [ hasAlertContent: 0, shouldPresentAlert: 1 hasSound: 0 shouldPlaySound: 1 ]
【问题讨论】:
静默远程通知确实意味着推送通知而不是本地通知。 @AnkitJayaswal 你是什么意思? 静默通知适用于带有content-available
键的推送通知,但不适用于本地通知。更多详情 - ***.com/questions/44944383/…
@AnkitJayaswal 我不想要无声的本地通知,而是希望它带有声音。静音是我的远程通知。
你不应该在闭包之外调用completionHandler(.noData)
。这向 ios 表明您没有数据,将导致您的应用在网络操作完成之前再次暂停
【参考方案1】:
let content = UNMutableNotificationContent()
content.title = title
content.body = body
content.sound = UNNotificationSound.default()
var dateComponents = Calendar.current.dateComponents([.hour, .minute, .second], from: Date())
let trigger = UNCalendarNotificationTrigger(dateMatching: dateComponents, repeats: false)
let request = UNNotificationRequest(identifier: identifier, content: content, trigger: trigger)
UNUserNotificationCenter.current().add(request, withCompletionHandler: (error) in
if let error = error
print(error)
)
UNUserNotificationCenter.current().delegate = self
使用这个
【讨论】:
这没有帮助 让 notification = UILocalNotification() notification.fireDate = NSDate(timeIntervalSinceNow: 1) as Date notification.alertBody = body notification.alertAction = title notification.soundName = UILocalNotificationDefaultSoundName UIApplication.shared.scheduleLocalNotification(notification) 使用这个【参考方案2】://AppDelegate
UNUserNotificationCenter.current().delegate = self
let authOptions: UNAuthorizationOptions = [.alert, .badge, .sound]
UNUserNotificationCenter.current().requestAuthorization(
options: authOptions,
completionHandler: _, _ in )
application.registerUserNotificationSettings(UIUserNotificationSettings(types: [.alert, .badge, .sound], categories: nil))
//在视图控制器中
let content = UNMutableNotificationContent()
content.title = “Notification Title”
content.body = “Notification Body”
content.sound = UNNotificationSound.default()
content.badge = 1
let date = Date()
let localDate = date.toLocalTime()
var triggerDaily = Calendar.current.dateComponents([.hour,.minute,.second,], from: localDate)
triggerDaily.hour = 22
triggerDaily.minute = 00
triggerDaily.second = 00
let trigger = UNCalendarNotificationTrigger(dateMatching: triggerDaily, repeats: true)
let identifier = "Local Notification"
let request = UNNotificationRequest(identifier: identifier, content: content, trigger: trigger)
UNUserNotificationCenter.current().add(request) (error) in
if let error = error
print("Error \(error.localizedDescription)")
//添加扩展以转换时间
extension Date
func convertToGlobalTime() -> Date
let currentTimezone = TimeZone.current
let interval = -TimeInterval(currentTimezone.secondsFromGMT(for: self))
return Date(timeInterval: interval, since: self)
func convertToLocalTime() -> Date
let currentTimezone = TimeZone.current
let interval = TimeInterval(currentTimezone(for: self))
return Date(timeInterval: interval, since: self)
【讨论】:
以上是关于已安排本地通知,但未在挂起和未运行模式下传递的主要内容,如果未能解决你的问题,请参考以下文章