2016-09-08 77 views
0

您好,我正在製作iPhone應用,並通過Google的Firebase發送推送通知。我正在使用Swift和Xcode進行編程。當我打開應用程序時,系統會要求我提供推送通知,但是我從Firebase控制檯發送它們時沒有收到任何推送通知。我想知道你能否幫我解決問題。我正在使用Ad Hoc導出功能將.isa文件傳輸到我朋友的iPhone,並以此方式進行測試。我完全遵循了Firebase教程 - 添加了.plist,可可豆和我在項目設置中上傳了一個證書。我有一個付費的蘋果開發者帳戶。通過Firebase無法正常工作的iOS推送通知

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

遵循這一切http://shubhank101.github.io/iOSAndroidChaosOverFlow/2016/07/Push-Notification-in-iOS-using-FCM-(Swift) – Shubhank

回答

0

還有一些事情你需要做。當您使用registerForRemoteNotifications時,您將收到需要提供給Firebase的APNS令牌。這在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

謝謝你的答案。試過你的解決方案,我得到了一個運行時錯誤線程1:EXC_BAD_INSTRUCTION(代碼= EXC_I386_INVOP,子代碼= 0x0),它將這行代碼標記爲紅色: 'print(「MessageID:\(userInfo [」gcm_message_id「]!) 「)' 有什麼想法? –

+0

它應該是'print(「MessageID:\(userInfo [」gcm.message_id「]!)」)' – Callam