Firebase通知未在iOS应用程序中显示警报或横幅和徽章应用程序图标 我设计了一个iOS应用程序,其中包含推送通知功能。 通知通过FCM抛出(仅用于检查),它将仅在前台模式下在控制台中打印。但当应用程序处于后台模式时,它不会显示任何通知 我在苹果开发者计划账户中注册 在这个帐户中,我创建了一个应用程序id,其中包含推送通知启用和证书(用于生产) 创建了.p12和.pem文件 还创建了资源调配配置文件(临时) 实现了Firebase中为iOS 10+的xcode 8中的通知提供的代码 正确生成的设备令牌。 仍然无法获得通知。请帮忙

Firebase通知未在iOS应用程序中显示警报或横幅和徽章应用程序图标 我设计了一个iOS应用程序,其中包含推送通知功能。 通知通过FCM抛出(仅用于检查),它将仅在前台模式下在控制台中打印。但当应用程序处于后台模式时,它不会显示任何通知 我在苹果开发者计划账户中注册 在这个帐户中,我创建了一个应用程序id,其中包含推送通知启用和证书(用于生产) 创建了.p12和.pem文件 还创建了资源调配配置文件(临时) 实现了Firebase中为iOS 10+的xcode 8中的通知提供的代码 正确生成的设备令牌。 仍然无法获得通知。请帮忙,ios,swift,Ios,Swift,Appdelegate代码: import UIKit import UserNotifications import Firebase import FirebaseInstanceID import FirebaseMessaging @UIApplicationMain class AppDelegate: UIResponder, UIApplicationDelegate { var window: UIWindow? let gcmMessageIDKey

Appdelegate代码:

import UIKit 
import UserNotifications
import Firebase
import FirebaseInstanceID
import FirebaseMessaging

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?


    let gcmMessageIDKey = "gcm.message_id"

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

        // Register for remote notifications. This shows a permission dialog on first run, to
        // show the dialog at a more appropriate time move this registration accordingly.
        // [START register_for_notifications]
        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()

        // [END register_for_notifications]
        FIRApp.configure()

        // [START add_token_refresh_observer]
        // Add observer for InstanceID token refresh callback.
        NotificationCenter.default.addObserver(self,
                                               selector: #selector(self.tokenRefreshNotification),
                                               name: .firInstanceIDTokenRefresh,
                                               object: nil)
        // [END add_token_refresh_observer]
        return true
    }

    /* [START receive_message]*/
    func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable: Any]) {
        // 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.
        if let messageID = userInfo[gcmMessageIDKey] {
            print("Message ID: \(messageID)")
        }

        // Print full message.
        print(userInfo)
    }

    /*[END receive_message]
     [START refresh_token] */
    func tokenRefreshNotification(_ notification: Notification) {
        if 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]
     / [START connect_to_fcm]*/
    func connectToFcm() {
        // Won't connect since there is no token
        guard FIRInstanceID.instanceID().token() != nil else {
            return;
        }

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

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

    /* This function is added here only for debugging purposes, and can be removed if swizzling is enabled.
     If swizzling is disabled then this function must be implemented so that the APNs token can be paired to
     the InstanceID token.*/
    func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
        print("APNs token retrieved: \(deviceToken)")

        // With swizzling disabled you must set the APNs token here.
        // FIRInstanceID.instanceID().setAPNSToken(deviceToken, type: FIRInstanceIDAPNSTokenType.sandbox)

        if let refreshedToken = FIRInstanceID.instanceID().token() {
            print("InstanceID token 1: \(refreshedToken)")
        }
    }


    func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable: Any],
                     fetchCompletionHandler completionHandler: @escaping (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.
        if let messageID = userInfo[gcmMessageIDKey] {
            print("Message ID in didReceiveRemoteNotification: \(messageID)")
        }

        // Print full message.
        print("didReceiveRemoteNotification: \(userInfo)")

        completionHandler(UIBackgroundFetchResult.newData)
    }

    // [END connect_to_fcm]
    func application(_ application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: Error) {
        print("Unable to register for remote notifications: \(error.localizedDescription)")
    }



    /* [START connect_on_active]*/
    func applicationDidBecomeActive(_ application: UIApplication) {
        connectToFcm()
    }


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

        connectToFcm()
          print("connected to FCM in Background.")
    }
    // [END disconnect_from_fcm]
}

extension AppDelegate : UNUserNotificationCenterDelegate {

    // Receive displayed notifications for iOS 10 devices.
    func userNotificationCenter(_ center: UNUserNotificationCenter,
                                willPresent notification: UNNotification,
                                withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
        let userInfo = notification.request.content.userInfo
        // Print message ID.
        if let messageID = userInfo[gcmMessageIDKey] {
            print("Message ID in UNUserNotificationCenterDelegate: \(messageID)")
        }

        // Print full message.
        print(userInfo)

        // Change this to your preferred presentation option
        completionHandler([])
    }

    func userNotificationCenter(_ center: UNUserNotificationCenter,
                                didReceive response: UNNotificationResponse,
                                withCompletionHandler completionHandler: @escaping () -> Void) {
        let userInfo = response.notification.request.content.userInfo
        // Print message ID.
        if let messageID = userInfo[gcmMessageIDKey] {
            print("Message ID in userNotificationCenter: \(messageID)")
        }

        // Print full message.
        print(userInfo)

        completionHandler()
    }
}

extension AppDelegate : FIRMessagingDelegate {

    // Receive data message on iOS 10 devices while app is in the foreground.
    func applicationReceivedRemoteMessage(_ remoteMessage: FIRMessagingRemoteMessage) {
        print("applicationReceivedRemoteMessage: \(remoteMessage.appData)")
    }

}

要在后台模式下接收和显示通知,请确保在“功能”部分启用了“推送通知”

要在前台模式下显示通知,请在willPresent方法的完成块中添加警报、徽章和声音参数

completionHandler([.alert、.badge、.sound])


你能把控制台里的信息放在前台模式吗?我面临这个问题。在我的例子中,服务器端出现了问题。在服务器端和ios代码中指向生产模式时,请与进行检查。请确保您生成了正确的生产推送证书。@AvijitNagare。。就连我也面临同样的问题。服务器的有效负载是否有任何限制。检查他们是否向APN发送了正确的有效负载。例如{“aps”:{“alert”:“你收到了你的电子邮件。”,“badge”:9}@AvijitNagare-现在我正试图从Firebase“通知”发送通知,只是为了测试。谢谢。我试试看。
 // Receive displayed notifications for iOS 10 devices.
func userNotificationCenter(_ center: UNUserNotificationCenter,
                            willPresent notification: UNNotification,
                            withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
    let userInfo = notification.request.content.userInfo
    // Print message ID.
    if let messageID = userInfo[gcmMessageIDKey] {
        print("Message ID in UNUserNotificationCenterDelegate: \(messageID)")
    }

    // Print full message.
    print(userInfo)

    // Change this to your preferred presentation option
    completionHandler([.alert, .badge, .sound])
}