【问题标题】:Not receiving push notification from FCM to iOS Swift 3没有收到从 FCM 到 iOS Swift 3 的推送通知
【发布时间】:2017-05-29 10:30:20
【问题描述】:

我正在构建一个带有 FCM 推送通知的 iOS 应用。我遵循了 Google Firebase 文档和其他教程,但我的 AppDelegate 从未执行函数 didReceiveRemoteNotification,因此当我从 Firebase 控制台发送通知时,它什么也不做。

我现在有这个:

AppDelegate.swift

 func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {

        application.registerForRemoteNotifications()            
        FIRApp.configure()
        connectToFcm()

        return true
    }

func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
    token = deviceToken.map { String(format: "%02.2hhx", $0) }.joined()
    print("<-TOKEN->")
    print(token)
}

func connectToFcm() {
    FIRMessaging.messaging().connect { (error) in
        if (error != nil) {
            print("Unable to connect with FCM. \(error)")
        } else {
            print("Connected to FCM.")
        }
    }
}

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

    // Print message ID.
    print("Message ID: \(userInfo["gcm.message_id"]!)")

    // Print full message.
    print("%@", userInfo)
}
  • 我还在我的 CocoaPods 文件中安装了 Firebase。
  • 我在 Firebase 控制台中使用相同的 Build ID 注册了该应用。
  • 我有“必需的后台模式 -> 应用程序下载内容作为响应 推送通知”在我的 info.plist 文件中
  • 我已创建我的 APNS 证书并将其上传到我在 Firebase 控制台中的设置。

当我启动应用程序时,它会成功构建,并在控制台上打印我的这个:

[Firebase/Analytics][I-ACS023012] Firebase Analytics enabled
2017-05-29 12:10:56.101 incidenciasSifu[1811] <Notice> [Firebase/Analytics][I-ACS023012] Firebase Analytics enabled
Optional(Error Domain=com.google.fcm Code=2001 "FIRMessaging is already connected" UserInfo={NSLocalizedFailureReason=FIRMessaging is already connected})
<-TOKEN->
Optional("03545fbbb986e2ffdfa50ac9e3eb6fe07e6fe4694cdfd000d673a0bf5ea53f6a")
Connected to FCM.

我是 Swift 新手,不知道是不是忘记在 AppDelegate 中添加一些代码之类的。

请帮忙!我在这上面花了太多时间:(

【问题讨论】:

    标签: ios swift firebase swift3 firebase-cloud-messaging


    【解决方案1】:

    在注册推送通知之前,您需要要求用户授予您必要的权限。 在您的 didFinishLaunchingWithOptions 方法中尝试此代码:

    if #available(iOS 10.0, *) {
        UNUserNotificationCenter.current().requestAuthorization(options: [.badge, .alert, .sound]) { (granted, error) in
            if granted {
                application.registerForRemoteNotifications()
            }
        }
    } else {
        let notificationTypes: UIUserNotificationType = [.alert, .badge, .sound]
        let settings = UIUserNotificationSettings(types: notificationTypes, categories: nil)
        application.registerUserNotificationSettings(settings)
        application.registerForRemoteNotifications()
    }
    

    检查应用程序启动的原因也很有用。在该方法中添加如下代码:

    if let remoteNotification = launchOptions?[UIApplicationLaunchOptionsKey.remoteNotification] {
        self.application(application, didReceiveRemoteNotification: remoteNotification as! [AnyHashable : Any])
    }
    

    另外你可以添加 didFailToRegisterForRemoteNotificationsWithError 方法来了解可能出错的原因:

    func application(_ application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: Error) {
        print ("Eror: \(error.localizedDescription)")
    }
    

    【讨论】:

    • 好的,我这样做了,除了添加remoteNotification,我必须在哪个函数上添加它?
    • 前两部分应该替换您在 didFinishLaunching 中调用 application.registerForRemoteNotifications()
    【解决方案2】:

    您需要在appdelegate的didFinishLaunchingWithOptions注册通知。

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
    
    
            let center = UNUserNotificationCenter.current()
            center.requestAuthorization(options:[.badge, .alert, .sound]) { (granted, error) in
                //  actions based on whether notifications were authorized or not
            }
            application.registerForRemoteNotifications()
            return true
        }
    

    【讨论】:

    • 我已经有application.registerForRemoteNotifications()但没有center.requestAuthorization,有必要吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-02-25
    • 2018-05-01
    • 2020-05-25
    相关资源
    最近更新 更多