Alternative to UserNotificationCenterDelegate's willPresent when app is in background

走远了吗. 提交于 2019-11-26 14:49:16

问题


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.


回答1:


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.



来源:https://stackoverflow.com/questions/44425744/alternative-to-usernotificationcenterdelegates-willpresent-when-app-is-in-backg

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!