8

如果应用程序未处于活动状态(甚至不在后台或说已终止),则在 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
    }

当 Notification Pop-up (Sent from 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....
    }

但是下面的函数没有被调用。但是,如果应用程序在后台运行,则调用下面的函数并且一切正常 OTHERWISE 应用程序进入前台,之后聊天不会

    // 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

4

3 回答 3

12

根据我自己的实验,在 Swift 3 & 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)
    }
    
于 2016-08-09T20:59:14.043 回答
2

斯威夫特 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)
}
于 2017-07-17T11:08:55.203 回答
0

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

// 检查是否从通知启动

if let notification = launchOptions?[UIApplicationLaunchOptionsRemoteNotificationKey] as? [String: AnyObject] {
       notificationReceived(notification)
    }
于 2016-09-20T23:49:13.043 回答