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