当应用程序处于后台时,UserNotificationCenterDelegate将替代UserPresent [英] Alternative to UserNotificationCenterDelegate's willPresent when app is in background

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

问题描述

我试图弄清楚我是否可以通过本地通知完成目标,或者是否需要切换到远程通知。



我正在练习iOS 10 / Swift 3,它构建了一个闹钟程序,可以在一天中的某个时间播放RSS更新的电台节目的最新一集。当应用程序位于前台时,可以通过UNUserNotificationCenterDelegate的willPresent函数轻松执行。我只是使用willPresent来获取最新的剧集,并通过AVAudio播放器播放。



当然,如果应用程序仅在前台工作,则此功能非常有限。我希望应用程序在后台或关闭时以相同的方式工作。



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



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

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

switch actionIdentifier {
case UNNotificationDismissActionIdentifier://通知被用户解雇
//做点什么
completionHandler()
case UNNotificationDefaultActionIdentifier://应用程序是从通知中打开的
//执行某些操作
completionHandler()
默认值:
completionHandler()
}
}






当app在后台时回调,前景或(可能)暂停状态和推送通知(远程或无声通知)到达:



在iOS10之前,当您点击通知应用程序(_:didReceiv eRemoteNotification:fetchCompletionHandler:)将被调用。



但是因为iOS 10 应用程序(_:didReceiveRemoteNotification:fetchCompletionHandler :) 点击时未调用。它只被调用远程通知的到达。 (它在前台和后台都被称为)






对于 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将替代UserPresent的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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