【问题标题】:Firebase Notification being handled by userNotificationCenter not Firebase functions由 userNotificationCenter 而非 Firebase 函数处理的 Firebase 通知
【发布时间】:2019-04-04 22:08:06
【问题描述】:

我的应用程序正在实现 Firebase Cloud 消息传递以发送通知。每当我使用 Firebase 控制台测试 Firebase 通知时,都会出现由 userNotificationCenter 函数处理的通知,并且会显示 didReceiveRemoteNotification 而不是 Firebase applicationReceivedRemoteMessage 函数,我是否遗漏了什么?此外,当我尝试打印刚刚来自 Firebase 的通知时,userNotification 函数没有任何数据。这是我的设置:

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

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
        UNUserNotificationCenter.current().delegate = self
        FIRMessaging.messaging().remoteMessageDelegate = self
        FIRApp.configure()
        registerForFireBaseNotifications()
    //Other set up variables
    connectToFcm()

        return true
    }

func registerForFireBaseNotifications(){
        let authOptions: UNAuthorizationOptions = [.alert, .sound, .badge]
        UNUserNotificationCenter.current().requestAuthorization(options: authOptions, completionHandler: {_, _ in })
        application.registerForRemoteNotifications()
    }

    func applicationReceivedRemoteMessage(_ remoteMessage: FIRMessagingRemoteMessage) {
        print("Recieved remote firebase notification: %@", remoteMessage.appData)
    }

    func tokenRefreshNotification(notification: NSNotification) {
        let refreshedToken = FIRInstanceID.instanceID().token()
        print("FCM: Connected to FCM. Token : \(String(describing: refreshedToken))")
        connectToFcm()
    }

    func connectToFcm() {
        // Won't connect since there is no token
        guard FIRInstanceID.instanceID().token() != nil else {
            print("FCM: Token does not exist.")
            return
        }

        // Disconnect previous FCM connection if it exists.
        FIRMessaging.messaging().disconnect()

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

//Non firebase notifications
func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (_ options: UNNotificationPresentationOptions) -> Void) {
    //do something with notifications that came while on foreground
}

func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
    //do something with notifications that came from background
}

func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
    FIRInstanceID.instanceID().setAPNSToken(deviceToken, type: FIRInstanceIDAPNSTokenType.sandbox)
}

【问题讨论】:

    标签: ios firebase apple-push-notifications firebase-notifications


    【解决方案1】:

    我能够继续挖掘并找到我正在寻找的答案。对于任何为此苦苦挣扎并了解 Firebase 通知如何与 IOS 配合使用的人。上面的设置代码是正确的。我遇到的主要问题是通知没有通过 Firebase 函数 applicationReceivedRemoteMessage 得到解决。根据我的理解,这是不正确的! Firebase 控制台仅允许您将消息作为通知发送!这意味着您的应用将通过 apns 获得通知!如果要触发 applicationReceivedRemoteMessage 函数,则需要将消息作为数据 json 对象发送。您可以通过邮递员在这里查看更多信息:Unable to send data message using firebase console。希望这会有所帮助!

    【讨论】:

      猜你喜欢
      • 2018-02-21
      • 1970-01-01
      • 2014-09-24
      • 1970-01-01
      • 2019-02-13
      • 2018-04-14
      • 2018-03-15
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多