如果App终止,则不会调用UNUserNotificationCenter didRecieve Response

时间:2016-07-31 04:35:39

标签: apple-push-notifications swift3 ios10 xcode8 xcode8-beta3

如果应用程序未处于活动状态(甚至不在后台或说已终止),则单击操作按钮 3D Touch中的通知中的聊天时不会调用UNUserNotificationCenter功能。我在Xcode中使用“附加到名称处理”来在应用程序终止时调试应用程序。这是代码:

import UIKit
import Mixpanel
import UserNotifications

@UIApplicationMain

class AppDelegate: UIResponder, UIApplicationDelegate, UNUserNotificationCenterDelegate {

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
        //setup mixpanel

        self.handlePushNotificationWithUserInfo(launchOptions: launchOptions)

        //ask for push notification perms
        return true
    }

当通知弹出窗口(从MixPanel发送)时,首先调用此函数,

致电1:

    func handlePushNotificationWithUserInfo(launchOptions: [NSObject: AnyObject]?) {
        //Handle PushNotification when app is opened
    }

然后它就到了,

致电2:

    //register for notification
    func application(_ application: UIApplication, didRegister notificationSettings: UIUserNotificationSettings) {

        if #available(iOS 10.0, *) {
            let center = UNUserNotificationCenter.current()
            center.requestAuthorization(options: [.alert, .sound]) { (granted, error) in
                // Enable or disable features based on authorization.
            }
            center.delegate = self

            let actionChat = UNNotificationAction(identifier: Constants.ActionType.CHAT.rawValue, title: "Chat", options: [.foreground])
            let categoryOptions = UNNotificationCategoryOptions(rawValue: 0)
            let customerSupportCategory = UNNotificationCategory(identifier: Constants.NotificationType.CUSTOMER_SUPPORT.rawValue, actions: [actionChat], intentIdentifiers: [], options: categoryOptions)
            center.setNotificationCategories([customerSupportCategory])
        }

        application.registerForRemoteNotifications()
    }

致电3:

    // remote notification
    func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
        ....Some Code....
    }

以下功能未被调用。 但是,如果应用程序在后台运行,则会调用以下函数,并且一切正常,否则应用程序进入前台,之后聊天

    // action buttons in enhanced Notification
    @available(iOS 10, *)
    func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: () -> Void) {
        guard let action = Constants.ActionType(rawValue: response.actionIdentifier) else {
            completionHandler()
            return
        }
        switch action {
        case .CHAT:
            _ = self.handleRemoteUrl(NSURL(string: "chat") as? URL)
        default:
            _ = self.handleRemoteUrl(NSURL(string: "chat") as? URL)
        }
        completionHandler()
    }

    @available(iOS 10.0, *)
    func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: (UNNotificationPresentationOptions) -> Void) {
        completionHandler([.alert, .sound])
    }
}

这个函数永远不会被调用,因为它在iOS 10中通过userNotificationCenter()折旧,不确定。请解释一下..

    func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject]) {
        ....Some Code....
    }

我正在使用iPhone 6s iOS 10作为调试设备。 XCode 8 beta-3

3 个答案:

答案 0 :(得分:10)

从我自己的实验中,在Swift 3&amp ;;中接收本地通知Xcode 8如下:

<强>一致性

  • 符合UNUserNotificationCenterDelegate

    class AppDelegate: UIResponder, UIApplicationDelegate, UNUserNotificationCenterDelegate { .... }
    
  • 注册为通知中心的代表:

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
    
        let center = UNUserNotificationCenter.current()
        center.delegate = self
    
        return true
    }
    

委派方法

  • 回复错过的通知(例如,在发送通知时用户查看应用)

    func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: (UNNotificationPresentationOptions) -> Void) {
        print(notification.request.content.userInfo)
    }
    
  • 回复已采取行动的通知(例如,用户已打开的通知)

    func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: () -> Void) {
        print(response.notification.request.content.userInfo)
    }
    

答案 1 :(得分:1)

Swift 3.1更新

  • 符合UNUserNotificationCenterDelegate

  • 注册为通知中心的代表:

UNUserNotificationCenter.current().delegate = self
  • 委派方法
public func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
    print(response.notification.request.content.categoryIdentifier)
}

public func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
    print(notification.request.content.categoryIdentifier)
}

答案 2 :(得分:0)

当应用未被用户运行或杀死并且收到通知时,在这种情况下,您需要处理didFinishLaunchingWithOptions并检查应用是否通过通知打开并采取适当的行动。

//检查是否从通知

启动
if let notification = launchOptions?[UIApplicationLaunchOptionsRemoteNotificationKey] as? [String: AnyObject] {
       notificationReceived(notification)
    }