无法从swift3中的firebase接收通知

时间:2018-01-12 06:58:29

标签: ios firebase swift3

我无法收到来自firebase的通知。我已更新所有pod并上传了APNs证书以供制作。在Xcode中,我自动管理配置文件,并在功能中启用了推送通知,远程通知(在后台模式内)。在设备的设置部分启用通知。 这是我在App委托中的代码。

 import UserNotifications
 import Firebase
 import FirebaseInstanceID
 import FirebaseMessaging

 func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
   if #available(iOS 10.0, *) {
        // For iOS 10 display notification (sent via APNS)
        UNUserNotificationCenter.current().delegate = self
        let authOptions: UNAuthorizationOptions = [.alert, .badge, .sound]
        UNUserNotificationCenter.current().requestAuthorization(
            options: authOptions,
            completionHandler: {_, _ in })
        // For iOS 10 data message (sent via FCM
        Messaging.messaging().delegate = self
    } else {
        let settings: UIUserNotificationSettings =
            UIUserNotificationSettings(types: [.alert, .badge, .sound], categories: nil)
        application.registerUserNotificationSettings(settings)
    }

    application.registerForRemoteNotifications()
    FirebaseApp.configure()

 }

func application(_ application: UIApplication,     
      didReceiveRemoteNotification userInfo: [AnyHashable: Any]) {

    if let messageID = userInfo[gcmMessageIDKey] {
        print("Message ID: \(messageID)")
    }
}


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

     Messaging.messaging().appDidReceiveMessage(userInfo)

    // Print message ID.
    if let messageID = userInfo[gcmMessageIDKey] {
        print("Message ID: \(messageID)")
    }

    completionHandler(UIBackgroundFetchResult.newData)
}

func application(_ application: UIApplication,   
  didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
  Messaging.messaging().apnsToken = deviceToken
}

func applicationDidBecomeActive(_ application: UIApplication) {
    Messaging.messaging().connect { error in

    }
}

@available(iOS 10, *)
 extension AppDelegate : UNUserNotificationCenterDelegate {

// Receive displayed notifications for iOS 10 devices.
func userNotificationCenter(_ center: UNUserNotificationCenter,
                            willPresent notification: UNNotification,
                            withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
    let userInfo = notification.request.content.userInfo
    //Here i do my navigation according to notification type
 }

  func userNotificationCenter(_ center: UNUserNotificationCenter,
                            didReceive response: UNNotificationResponse,
                            withCompletionHandler completionHandler: @escaping () -> Void) {
    let userInfo = response.notification.request.content.userInfo
 }

 extension AppDelegate : MessagingDelegate {
//MARK: FCM Token Refreshed
func messaging(_ messaging: Messaging, didRefreshRegistrationToken fcmToken: String) {
    let token = Messaging.messaging().fcmToken
    let def = UserDefaults.standard
    def.setValue(token, forKey: KDeviceToken)
    print(def.object(forKey: KDeviceToken) ?? String())
}

// Receive data message on iOS 10 devices while app is in the foreground.
func messaging(_ messaging: Messaging, didReceive remoteMessage: MessagingRemoteMessage) {
    NSLog("remoteMessage: \(remoteMessage.appData)")
}
}

0 个答案:

没有答案