【问题标题】:FirebasePush Notifications are only displayed in the console and don't load in backgroundFirebasePush 通知仅显示在控制台中,不会在后台加载
【发布时间】:2016-12-23 11:37:26
【问题描述】:

我准备了所有证书,将它们上传到 Firebase 控制台,更新了我的 App ID 和 Xcode 项目中的设置。但是我的应用程序无法显示任何推送通知。如果我从 Firebase 控制台发送推送通知,我只会在控制台中看到消息,而不是在后台模式下运行我的应用程序。我用的是真机/iPhone 6S。

此外,如果我从 AppDelegate 注释所有断开连接的部分,控制台会向我显示以下错误。

Unable to connect with FCM. Optional(Error Domain=com.google.fcm Code=2001 "(null)") 

我在 AppDelegate.swift 中准备了 Firebase 推送通知:

import UIKit
import UserNotifications

import Firebase
import FirebaseAuth
import FirebaseMessaging
import FirebaseDatabase
import FirebaseInstanceID
import FirebaseRemoteConfig

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?


    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
        // Override point for customization after application launch.
        application.statusBarStyle = .lightContent
        FIRApp.configure()
        FIRDatabase.database().persistenceEnabled = true

        //let _ = RemoteConfigValues.sharedInstance

        let settings: UIUserNotificationSettings =
            UIUserNotificationSettings(forTypes: [.Alert, .Badge, .Sound], categories: nil)
        application.registerUserNotificationSettings(settings)
        application.registerForRemoteNotifications()

        NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(self.tokenRefreshNotification), name: kFIRInstanceIDTokenRefreshNotification, object: nil)
        logUser()

        return true
    }

    func application(application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: NSData) {
        let tokenChars = UnsafePointer<CChar>(deviceToken.bytes)
        var tokenString = ""

        for i in 0..<deviceToken.length {
            tokenString += String(format: "%02.2hhx", arguments: [tokenChars[i]])
        }

        FIRInstanceID.instanceID().setAPNSToken(deviceToken, type: FIRInstanceIDAPNSTokenType.Unknown)

        print("tokenString: \(tokenString)")
    }

    // [START receive_message]
    func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject],
                     fetchCompletionHandler completionHandler: (UIBackgroundFetchResult) -> Void) {
        // If you are receiving a notification message while your app is in the background,
        // this callback will not be fired till the user taps on the notification launching the application.
        // TODO: Handle data of notification

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

        // Print full message.
        print("%@", userInfo)
    }
    // [END receive_message]

    // [START refresh_token]
    func tokenRefreshNotification(notification: NSNotification) {
        let refreshedToken = FIRInstanceID.instanceID().token()!
        print("InstanceID token: \(refreshedToken)")

        // Connect to FCM since connection may have failed when attempted before having a token.
        connectToFcm()
    }
    // [END refresh_token]

    func application(application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: NSError) {
        print(error.localizedDescription)
    }

    // [START connect_to_fcm]
    func connectToFcm() {
        FIRMessaging.messaging().connectWithCompletion { (error) in
            if (error != nil) {
                print("Unable to connect with FCM. \(error)")
            } else {
                print("Connected to FCM.")
            }
        }
    }
    // [END connect_to_fcm]

    func applicationDidBecomeActive(_ application: UIApplication) {
        application.applicationIconBadgeNumber = 0
        connectToFcm()
    }

    // [START disconnect_from_fcm]
    func applicationDidEnterBackground(_ application: UIApplication) {
        FIRMessaging.messaging().disconnect()
        print("Disconnected from FCM.")
    }
    // [END disconnect_from_fcm]


}

【问题讨论】:

  • 在通知中将优先级键添加到高并尝试
  • 什么意思? Firebase 中的优先级键?我这样做了,但没有奏效

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


【解决方案1】:

即使我遇到了这个错误,当我更改发送到以下的通知数据时,它对我有用

{"to":"coYbqscbjkA,S","priority" : "high" "通知":{"body":"12345","service_type":"我的服务"}}

检查一次...希望它有效...!

【讨论】:

  • 但是我应该如何更改通知?我从 Firebase 收到通知到控制台,但前提是应用程序处于活动状态。我想要在后台模式下推送通知。
  • 应用在前台时是否收到通知...?
  • 是的,但仅在控制台中。我没有看到警报或推送通知。
  • 不,遗憾的是没有。该语句已经在 return 语句之上。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-10-03
  • 2018-09-23
  • 1970-01-01
  • 2017-09-29
  • 1970-01-01
相关资源
最近更新 更多