Die UNUserNotificationCenter-Funktion wird nicht aufgerufen, wenn Sie auf Aktionsschaltfläche Chat in Benachrichtigung nach 3D-Touch klicken, wenn die App nicht aktiv ist (auch nicht im Hintergrund oder etwa beendet). Ich habe in Xcode "An Prozess mit Namen anhängen" verwendet, um die App zu debuggen, wenn die App beendet wurde. Hier ist der Code:
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
}
Wenn das Benachrichtigungs-Popup (von MixPanel gesendet) aufgerufen wird, wird diese Funktion zuerst aufgerufen.
Anruf 1:
func handlePushNotificationWithUserInfo(launchOptions: [NSObject: AnyObject]?) {
//Handle PushNotification when app is opened
}
Dann geht es hier,
Anruf 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()
}
Anruf 3:
// remote notification
func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
....Some Code....
}
Unterhalb der Funktion wird nicht aufgerufen. Wenn jedoch App läuft im Hintergrund, dann wird die folgende Funktion aufgerufen und alles funktioniert gut OTHERWISE App kommt in den Vordergrund und Chat danach nicht mehr.
// 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])
}
}
Diese Funktion wird nie aufgerufen, weil sie in iOS 10 über userNotificationCenter () abgewertet wird, jedoch nicht sicher. Bitte erkläre dies auch ..
func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject]) {
....Some Code....
}
Ich verwende iPhone 6s iOS 10 als Debugging-Gerät.XCode 8 beta-3
Nach meinem eigenen Experiment erhalten Sie lokale Benachrichtigungen in Swift 3 und Xcode 8 wie folgt:
Konformität
Konform zu UNUserNotificationCenterDelegate
class AppDelegate: UIResponder, UIApplicationDelegate, UNUserNotificationCenterDelegate { .... }
Registrieren Sie sich als Delegierter des Benachrichtigungszentrums:
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
let center = UNUserNotificationCenter.current()
center.delegate = self
return true
}
Delegieren von Methoden
Auf versäumte Benachrichtigungen reagieren (z. B. Benutzer-App, wenn Benachrichtigung gesendet wird)
func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: (UNNotificationPresentationOptions) -> Void) {
print(notification.request.content.userInfo)
}
Antworten auf durchgeführte Benachrichtigungen (z. B. vom Benutzer geöffnete Benachrichtigung)
func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: () -> Void) {
print(response.notification.request.content.userInfo)
}
Swift 3.1 Update
Konform zu UNUserNotificationCenterDelegate
Registrieren Sie sich als Delegierter des Benachrichtigungszentrums:
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)
}
Wenn die App nicht von einem Benutzer ausgeführt oder beendet wird und eine Benachrichtigung empfangen wird, müssen Sie in einem solchen Szenario mit didFinishLaunchingWithOptions umgehen und prüfen, ob die App über eine Benachrichtigung geöffnet wird, und entsprechend handeln.
// Überprüfen Sie, ob die Benachrichtigung gestartet wurde
if let notification = launchOptions?[UIApplicationLaunchOptionsRemoteNotificationKey] as? [String: AnyObject] {
notificationReceived(notification)
}