Push notification retrieved in iOS but not in Firebase

时间:2017-07-10 15:24:18

标签: ios firebase swift3 apple-push-notifications firebase-cloud-messaging

I am facing this strange issue in my application. I am trying to integrate push notification in my application using firebase. I have included Firebase SDK in the project using the downloaded SDK(not using pod). I have included the following frameworks from the downloaded zip file in the application:

enter image description here

In AppDelegate

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate, UNUserNotificationCenterDelegate, MessagingDelegate {

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
    FirebaseApp.configure()
    return true
    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 })
    } else {
        let settings: UIUserNotificationSettings =
            UIUserNotificationSettings(types: [.alert, .badge, .sound], categories: nil)
        application.registerUserNotificationSettings(settings)
    }
    application.registerForRemoteNotifications()
}

// This is called and the access token is received.
func application(_ application: UIApplication,
                 didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
    var token = ""
    for i in 0..<deviceToken.count {
        token = token + String(format: "%02.2hhx", arguments: [deviceToken[i]])
    }
    print(token)
    Messaging.messaging().setAPNSToken(deviceToken, type: .sandbox)
}

// This is not getting called
func messaging(_ messaging: Messaging, didRefreshRegistrationToken fcmToken: String) {
    Messaging.messaging().subscribe(toTopic: "ios_topic")
}

The access token I get in didRegisterForRemoteNotificationsWithDeviceToken is valid and I successfully sent push to the device using that. But the didRefreshRegistrationToken of FireBase is not getting called. I have added -ObjC in linkerflags and FirebaseAppDelegateProxyEnabled as NO in info.plist. I have added the GoogleService-Info.plist file in the project root, and the project is configured correctly in FireBase console.

What is the possible issue here and how to solve it?

1 个答案:

答案 0 :(得分:0)

这是跟随教程时最愚蠢的错误。只需要将Messaging.messaging()的委托设置为AppDelegate。在FirebaseApp.configure()下添加以下代码,问题就解决了。

FirebaseApp.configure()
Messaging.messaging().delegate = self