当应用程序在后台时替代 UserNotificationCenterDelegate 的 willPresent [英] Alternative to UserNotificationCenterDelegate's willPresent when app is in background

本文介绍了当应用程序在后台时替代 UserNotificationCenterDelegate 的 willPresent的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想弄清楚我是否可以通过本地通知实现我的目标,或者我是否需要切换到远程通知.

我正在通过构建一个闹钟程序来练习 iOS 10/Swift 3,该程序在一天中的固定时间播放 RSS 更新的广播节目的最新一集.当应用程序在前台时,这很容易通过 UNUserNotificationCenterDelegate 的 willPresent 函数执行.我只是使用 willPresent 来获取最新的剧集,然后通过 AVAudio Player 播放.

当然,如果应用只在前台运行,这个功能是非常有限的.我希望应用程序在后台或关闭时以相同的方式工作.

我可以从文档中看到当应用程序不在前台时 willPresent 不会运行.当应用程序在后台时,是否有另一种方法让本地通知在推送通知之前执行代码?还是我必须切换到远程通知?我看到

例如为您提供用户点击保存的回调.

func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping() -> Void) {让 actionIdentifier = response.actionIdentifier切换动作标识符{case UNNotificationDismissActionIdentifier://通知被用户拒绝//做点什么完成处理程序()case UNNotificationDefaultActionIdentifier://应用程序是从通知中打开的//做点什么完成处理程序()默认:完成处理程序()}}

<小时>

当应用程序处于后台、前台或(可能)挂起状态并且推送通知(远程或静默通知)到达时的回调:

在 iOS10 之前,当您点击通知时,application(_:didReceiveRemoteNotification:fetchCompletionHandler:) 将被调用.

但由于 iOS 10 application(_:didReceiveRemoteNotification:fetchCompletionHandler:) 不会在 tapping 时调用.它仅在远程通知到达时调用.(它在前台和后台都被调用)

<小时>

对于iOS 10 之前,您可以只使用旧的didReceiveLocalNotification 函数并捕获任何通知的到达.

I am trying to figure out whether I can accomplish my goal through Local Notifications, or whether I need to switch to Remote Notifications.

I'm practicing iOS 10 / Swift 3 by building an alarm program that plays the latest episode of a RSS-updated radio show at a set time of day. When the app is in the foreground, this is easy to execute through UNUserNotificationCenterDelegate's willPresent function. I just use willPresent to fetch the latest episode, and play it through an AVAudio Player.

Of course, if the app only works in the foreground, this functionality is very limited. I would want the app to work the same way when in the background or closed.

I can see from the documentation that willPresent does not run when the app isn't in the foreground. Is there another way to have Local Notifications execute code prior to pushing the notification when the app is in the background? Or will I have to switch to Remote Notifications? I see this answer to a related question but I'm wondering if there's a more elegant approach.

解决方案

For iOS 10 local notifications your out of luck.

For iOS 10 remote notificationsregardless of user interaction you can receive callbacks using application(_:didReceiveRemoteNotification:fetchCompletionHandler:). (It's kind of confusing that they deprecated most notification related methods but not this one)


Callback for when app is in foreground and you're about to show the notification:

func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
    let content = notification.request.content
    // Process notification content

    completionHandler([.alert, .sound]) // Display notification as regular alert and play sound
}


Callback for when app is either in background or in foreground and user tapped on an action:

e.g. gives you a callback that user tapped on Save.

func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
    let actionIdentifier = response.actionIdentifier

    switch actionIdentifier {
    case UNNotificationDismissActionIdentifier: // Notification was dismissed by user
        // Do something
        completionHandler()
    case UNNotificationDefaultActionIdentifier: // App was opened from notification
        // Do something
        completionHandler()
    default:
        completionHandler()
    }
}


Callback for when app is either in background, foreground or (possibly) suspended state and a Push Notifications (Remote or silent notification) arrives:

Prior to iOS10 when you tapped on the notification the application(_:didReceiveRemoteNotification:fetchCompletionHandler:) would get called.

But since iOS 10 application(_:didReceiveRemoteNotification:fetchCompletionHandler:) isn't called upon tapping. It's only called upon arrival of a remote Notification. (It get's called both in foreground and in background)


For pre iOS 10, you can just use the old didReceiveLocalNotification function and capture the arrival of any notification.

这篇关于当应用程序在后台时替代 UserNotificationCenterDelegate 的 willPresent的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆