didReceiveRemoteNotification无法运行的原因?

时间:2017-07-17 16:42:39

标签: ios swift firebase push-notification firebase-cloud-messaging

目前,我正在使用Firebase在Swift中进行推送通知。我想检索附加到已发送通知的数据。

我一直在我的app委托中使用didReceiveRemoteNotification函数,但是,这个函数似乎永远不会运行。

我已在委托中创建了一个测试,以便标签在触发时更改,但它没有触发一次。我已经尝试关闭,在后台和前台应用程序。

断点也表明它不会在前台发射。

如果有人能够阐明为了启动此功能需要满足哪些规格,那就太棒了。

最后,我的测试代码如下。

AppDelegate.swift中的代码:

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

    //vc is the ViewController.swift
    vc.updateLabel()

}

ViewController.swift中的代码

func updateLabel() {

    notificationLabel.text = "The function has fired"

}

感谢。

编辑 - 我的代表的其他功能:

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
    // Override point for customization after application launch.

    // Use Firebase library to configure APIs
    FirebaseApp.configure()

    if #available(iOS 10.0, *) {
        // For iOS 10 display notification (sent via APNS)
        UNUserNotificationCenter.current().delegate = self as? UNUserNotificationCenterDelegate

        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()

    let token = Messaging.messaging().fcmToken
    print("FCM token: \(token ?? "")")

    return true
}

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

    let notificationType = UIApplication.shared.currentUserNotificationSettings!.types

    if Messaging.messaging().fcmToken != nil {

        print(Messaging.messaging().fcmToken!)
        Messaging.messaging().subscribe(toTopic: "/topics/news")

    }
}

func tokenRefreshNotification(notification: NSNotification) {

    let refreshedToken = InstanceID.instanceID().token()!
    print("Instance ID Token: \(refreshedToken)")

    connectToFCM()

}

func connectToFCM() {

    let notificationType = UIApplication.shared.currentUserNotificationSettings!.types
    Messaging.messaging().shouldEstablishDirectChannel = true

}

0 个答案:

没有答案