2016-09-08 5 views
0

Hallo Ich mache eine iPhone-App, wo ich Push-Benachrichtigungen über Google Firebase senden. Ich verwende Swift und Xcode für die Programmierung. Wenn ich die App öffne, werde ich aufgefordert, Push-Benachrichtigungen zuzulassen, erhalte jedoch keine, wenn ich sie von der Firebase-Konsole aus sende. Ich habe mich gefragt, ob du mir helfen kannst. Ich benutze Ad-hoc-Export, um eine .isa auf das iPhone meines Freundes zu übertragen und so zu testen. Ich folgte genau dem Firebase-Tutorial - fügte die .plist, den Kakao-Pod hinzu und ich habe ein Zertifikat in die Projekteinstellungen hochgeladen. Ich habe ein bezahltes Apple Entwicklerkonto.iOS-Push-Benachrichtigung über Firebase funktioniert nicht

import UIKit 
import CoreData 
import Firebase 
import FirebaseMessaging 


@UIApplicationMain 
class AppDelegate: UIResponder, UIApplicationDelegate { 

var window: UIWindow? 


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

    let notificationTypes: UIUserNotificationType = [UIUserNotificationType.Alert,UIUserNotificationType.Badge,UIUserNotificationType.Sound] 

    let notificationSettings = UIUserNotificationSettings(forTypes:notificationTypes, categories:nil) 

    application.registerForRemoteNotifications() 
    application.registerUserNotificationSettings(notificationSettings) 
    return true 
} 


func applicationWillResignActive(application: UIApplication) { 
} 

func applicationDidEnterBackground(application: UIApplication) { 
} 

func applicationWillEnterForeground(application: UIApplication) { 
} 

func applicationDidBecomeActive(application: UIApplication) { 
} 

func applicationWillTerminate(application: UIApplication) { 
} 

func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject], 
       fetchCompletionHandler completionHandler: (UIBackgroundFetchResult) -> Void) { 
    print("MessageID : \(userInfo["gcm_message_id"]!)") 
    print("%@", userInfo) 

    } 
} 
+0

werden alle diese http://shubhank101.github.io/iOSAndroidChaosOverFlow/2016/07/Push-Notification-in-iOS-using-FCM-(Swift) – Shubhank

Antwort

0

Es gibt noch ein paar Dinge, die Sie tun müssen. Wenn Sie registerForRemoteNotifications erhalten, erhalten Sie ein APNS Token, das Sie Firebase geben müssen. Dies geschieht in application didRegisterForRemoteNotificationsWithDeviceToken.

import UIKit 
import CoreData 
import Firebase 
import FirebaseMessaging 

@UIApplicationMain 
class AppDelegate: UIResponder, UIApplicationDelegate { 

    var window: UIWindow? 

    override init() { 

     super.init() 

     FIRApp.configure() 
    } 

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

     application.registerForRemoteNotifications() 
     application.registerUserNotificationSettings(
      UIUserNotificationSettings(
       forTypes: [.Alert, .Badge, .Sound], 
       categories: nil 
      ) 
     ) 

     NSNotificationCenter.defaultCenter().addObserver(self, 
      selector: #selector(tokenRefreshNotification), 
      name: kFIRInstanceIDTokenRefreshNotification, 
      object: nil 
     ) 

     return true 
    } 

    func applicationDidEnterBackground(application: UIApplication) { 

     FIRMessaging.messaging().disconnect() 
    } 

    func applicationDidBecomeActive(application: UIApplication) { 

     FIRMessaging.messaging().connectWithCompletion { (error) in 

      switch error { 
      case .Some: 
       print("Unable to connect with FCM. \(error)") 
      case .None: 
       print("Connected to FCM.") 
      } 
     } 
    } 

    func applicationWillResignActive(application: UIApplication) {} 

    func applicationWillEnterForeground(application: UIApplication) {} 

    func applicationWillTerminate(application: UIApplication) {} 

    func application(application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: NSData) { 

     FIRInstanceID.instanceID().setAPNSToken(deviceToken, type: .Sandbox) 
    } 

    func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject], 
        fetchCompletionHandler completionHandler: (UIBackgroundFetchResult) -> Void) { 

     print("MessageID : \(userInfo["gcm.message_id"]!)") 
     print("%@", userInfo) 
    } 

    func tokenRefreshNotification(notification: NSNotification) { 

     if let refreshedToken = FIRInstanceID.instanceID().token() { 
      print("Instance ID token: \(refreshedToken)") 
     } 

     applicationDidBecomeActive(UIApplication.sharedApplication()) 
    } 
} 
+0

Vielen Dank für die Antwort. Habe die Lösung ausprobiert und einen Laufzeitfehler bekommen Thread 1: EXC_BAD_INSTRUCTION (Code = EXC_I386_INVOP, Subcode = 0x0) und es markiert diese Codezeile als rot: 'print (" MessageID: \ (userInfo ["gcm_message_id"]!) ")' Irgendwelche Ideen? –

+0

Es sollte 'print (" MessageID: \ (userInfo ["gcm.message_id"]!) ")' Sein – Callam