2017-05-29 2 views
2

Ich baue eine iOS-App mit FCM-Push-Benachrichtigungen. Ich habe die Google Firebase-Dokumentation und andere Lernprogramme verfolgt, aber mein AppDelegate führt niemals die Funktion didReceiveRemoteNotification aus. Wenn ich also Benachrichtigungen von meiner Firebase-Konsole aus sende, tut das nichts.Keine Push-Benachrichtigung von FCM an iOS erhalten. Swift 3

Ich habe dies, denn jetzt:

AppDelegate.swift

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool { 

     application.registerForRemoteNotifications()    
     FIRApp.configure() 
     connectToFcm() 

     return true 
    } 

func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) { 
    token = deviceToken.map { String(format: "%02.2hhx", $0) }.joined() 
    print("<-TOKEN->") 
    print(token) 
} 

func connectToFcm() { 
    FIRMessaging.messaging().connect { (error) in 
     if (error != nil) { 
      print("Unable to connect with FCM. \(error)") 
     } else { 
      print("Connected to FCM.") 
     } 
    } 
} 

func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable: Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) { 

    // Print message ID. 
    print("Message ID: \(userInfo["gcm.message_id"]!)") 

    // Print full message. 
    print("%@", userInfo) 
} 
  • ich auch auf meine Firebase CocoaPods-Datei installiert.
  • Ich registrierte die App in der Firebase-Konsole mit der gleichen Build-ID.
  • Ich habe „Erforderliche Hintergrund-Modi -> App-Downloads Inhalt in Reaktion Benachrichtigungen zu drücken“ in meiner Datei info.plist
  • habe ich erstellt und hochgeladen mein APNS Zertifikat auf meine Einstellungen in Firebase Console.

Und wenn ich die App starten, baut sie erfolgreich, und druckt meine dies auf der Konsole:

[Firebase/Analytics][I-ACS023012] Firebase Analytics enabled 
2017-05-29 12:10:56.101 incidenciasSifu[1811] <Notice> [Firebase/Analytics][I-ACS023012] Firebase Analytics enabled 
Optional(Error Domain=com.google.fcm Code=2001 "FIRMessaging is already connected" UserInfo={NSLocalizedFailureReason=FIRMessaging is already connected}) 
<-TOKEN-> 
Optional("03545fbbb986e2ffdfa50ac9e3eb6fe07e6fe4694cdfd000d673a0bf5ea53f6a") 
Connected to FCM. 

Ich bin Neuling auf Swift, und ich weiß nicht, ob ich einige hinzufügen, vergessen zu Code in AppDelegate oder so etwas.

Hilfe bitte! Ich verbrachte viel Stunde auf dieser

:(

Antwort

1

Vor Push-Benachrichtigungen Registrierung für Sie Benutzers zu fragen, müssen Sie erforderlichen Berechtigungen erteilen diesen Code in Ihre didFinishLaunchingWithOptions Methode versuchen.

if #available(iOS 10.0, *) { 
    UNUserNotificationCenter.current().requestAuthorization(options: [.badge, .alert, .sound]) { (granted, error) in 
     if granted { 
      application.registerForRemoteNotifications() 
     } 
    } 
} else { 
    let notificationTypes: UIUserNotificationType = [.alert, .badge, .sound] 
    let settings = UIUserNotificationSettings(types: notificationTypes, categories: nil) 
    application.registerUserNotificationSettings(settings) 
    application.registerForRemoteNotifications() 
} 

Auch wäre es sinnvoll sein, den Grund der App Start überprüfen Sie den folgenden Code in dieser Methode hinzufügen.

if let remoteNotification = launchOptions?[UIApplicationLaunchOptionsKey.remoteNotification] { 
    self.application(application, didReceiveRemoteNotification: remoteNotification as! [AnyHashable : Any]) 
} 

Zusätzlich können Sie didFailToRegisterForRemoteNotificationsWithError Methode zu verstehen, den Grund der möglichen Fehler hinzufügen:

func application(_ application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: Error) { 
    print ("Eror: \(error.localizedDescription)") 
} 
+0

Ok, habe ich das, außer remoteNotification hinzufügen, auf welche Funktion muss ich es hinzufügen? – jbono

+0

Erste zwei Stücke sollten Ihren Aufruf von application.registerForRemoteNotifications() in didFinishLaunch ersetzen – Alexey

0

Sie müssen die Benachrichtigung in Appdelegates didFinishLaunchingWithOptions registrieren.

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


     let center = UNUserNotificationCenter.current() 
     center.requestAuthorization(options:[.badge, .alert, .sound]) { (granted, error) in 
      // actions based on whether notifications were authorized or not 
     } 
     application.registerForRemoteNotifications() 
     return true 
    } 
+0

Ich hatte bereits application.registerForRemoteNotifications() aber nicht center.requestAuthorization, ist es notwendig? – jbono

+0

ja es notwendig – KKRocks