2016-07-31 62 views
5

Die Funktion UNUserNotificationCenter wird nicht beim Klicken aufgerufen Aktion Schaltfläche Chat in Benachrichtigung nach 3D Berühren, wenn App nicht aktiv ist (nicht einmal im Hintergrund oder sagen wurde beendet). Ich verwendete "anhängen, um nach Namen zu verarbeiten" in Xcode, um App zu debuggen, als App beendet wurde. Hier ist der Code:UNUserNotificationCenter didRecieve Antwort wird nicht aufgerufen, wenn App beendet wird

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 Benachrichtigung Pop-up (Sent von MixPanel) diese Funktion zuerst genannt wird,

Anruf 1:

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

dann es hier geht,

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.... 
    } 

Aber unten Funktion wird nicht genannt. Aber wenn App im Hintergrund läuft dann unter Funktion aufgerufen wird und funktioniert alles gut OTHERWISE App kommt in den Vordergrund und Chat danach nicht.

// 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 nicht aufgerufen werden kann, weil es in iOS 10 über userNotificationCenter() abgeschrieben ist, nicht sicher. Bitte erläutern Sie auch diese ..

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

Ich bin mit iPhone 6s iOS 10 als Debug-Gerät. XCode 8 beta-3

+0

Wenn App nicht oder vom Benutzer getötet läuft und Benachrichtigung empfangen wird dann in einem solchen Szenario, das Sie in didFinishLaunchingWithOptions behandeln müssen und prüfen, ob App über Melde- und Tat geöffnet passend. – user1140780

Antwort

6

Aus meiner eigenen Experimente Empfang lokale Benachrichtigungen in Swift 3 & Xcode 8 sind wie folgt:

Conformance

  • zu UNUserNotificationCenterDelegate Conform

    class AppDelegate: UIResponder, UIApplicationDelegate, UNUserNotificationCenterDelegate { .... } 
    
  • Register als Delegierter des Notification Center:

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

Delegatmethoden

  • Respond zu verpassten Benachrichtigungen (z Benutzersicht app wenn Benachrichtigung gesendet)

    func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: (UNNotificationPresentationOptions) -> Void) { 
        print(notification.request.content.userInfo) 
    } 
    
  • Respond actioned Benachrichtigungen (z.B.Benutzer geöffnet Benachrichtigung)

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

Ja, eigentlich habe ich alle diese Schritte gemacht .. Später habe ich meinen Fehler gefunden, aber noch keine Antwort und Sühne hinzugefügt, also habe ich nur hinzugefügt ** RegisterNotification.sharedHandler.registerFor3dTouchInNotification (appDelegate: self) ** in ** func handlePushNotificationWithUserInfo (launchOptions: [NSObject: AnyObject]?) ** welche Kategorie registrieren jedes Mal wenn wir benachrichtigt werden, nicht sicher, ob es richtig ist aber es löste mein Problem .. –

0

Wenn App nicht läuft oder getötet durch Benutzer und Benachrichtigung empfangen wird dann in einem solchen Szenario, das Sie in didFinishLaunchingWithOptions behandeln müssen und prüfen, ob App via Benachrichtigung geöffnet und entsprechend handeln.

// wenn von der Anmeldung gestartet prüfen

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

nicht mehr der Fall mit 'UNUserNotificationCenter', Delegat wird immer aufgerufen .. . –

1

Swift 3.1-Update

  • zu UNUserNotificationCenterDelegate Conform

  • Register als Delegierter des Notification Center:

UNUserNotificationCenter.current().delegate = self 
  • Delegatmethoden
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) 
}