ios - 在前台 ios 应用程序中没有收到 FCM 消息

标签 ios swift firebase firebase-cloud-messaging

我在我的 didFinishLaunchingWithOptions 方法中像这样在 ios 中注册和启动 firebase。

    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 })
        // For iOS 10 data message (sent via FCM
        FIRMessaging.messaging().remoteMessageDelegate = self

    } else {
        let settings: UIUserNotificationSettings =
            UIUserNotificationSettings(types: [.alert, .badge, .sound], categories: nil)
        application.registerUserNotificationSettings(settings)
    }

    application.registerForRemoteNotifications()


    FIRApp.configure()

然后在AppDelegate中这样设置回调

 func applicationReceivedRemoteMessage(_ remoteMessage: FIRMessagingRemoteMessage) {
    print("fir msg front : \(remoteMessage.appData)")
    print(remoteMessage.appData)
}

func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable: Any]) {

    print("fir msg : \(userInfo)")
}

当我从我的服务器发送消息时,当应用程序不在前台时,它会显示一个通知,但是当应用程序在前台时,我看不到任何通知,也没有任何回调被调用。我错过了什么吗?

最佳答案

请实现 UNUserNotificationCenterDelegate 方法。从 iOS 10 开始,您将在 UNUserNotificationCenterDelegate 方法中获得回调。

// The method will be called on the delegate only if the application is in the foreground. If the method is not implemented or the handler is not called in a timely manner then the notification will not be presented. The application can choose to have the notification presented as a sound, badge, alert and/or in the notification list. This decision should be based on whether the information in the notification is otherwise visible to the user.

func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions){

}

// The method will be called on the delegate when the user responded to the notification by opening the application, dismissing the notification or choosing a UNNotificationAction.

func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping (){

}

如果不实现这些,您会在以下行中收到错误-

UNUserNotificationCenter.current().delegate = self

关于ios - 在前台 ios 应用程序中没有收到 FCM 消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48112389/

相关文章:

ios - 什么时候在 iOS 开发中使用多线程?

iphone - 将 QuartzCore 框架导入 Xcode 项目时出现问题

ios - 在标签栏后面弹出带有动画的通知

swift - 如何 NSKeyedUnarchiver.unarchiveObject

ios - Firestore : How can I correctly order by string with accent characters?

android - 获取嵌套数据库 firebase android 中的最后一项

ios - firebase 数据库中新添加的数据在使用 Swift3 拉刷新之前 self 复制

ios - NSString + URLEncoding中的内存泄漏

ios - OSX是运行xCodeBuild所必需的,还是可以在Unix/Linux机器上运行

swift - 如何在 TableViewCell 中嵌入 AVPlayerViewController