FCM iOS推送通知无法接收任何通知

时间:2018-12-14 19:03:10

标签: swift firebase apple-push-notifications firebase-cloud-messaging

我目前正在处理通知,但未成功从Firebase FCM收到任何通知。

podfile配置为:

target 'Project' do
  # Comment the next line if you're not using Swift and don't want to use dynamic frameworks
  use_frameworks!

  # Pods for Project
  pod 'Firebase/Core'
  pod 'Firebase/Messaging'
end

我已经从Push Notifications激活了Remote NotificationsBackground fetches,并且已经在here中阅读了stackoverflow中当前类似的另一个主题

我想与您分享我的AppDelegate代码,但首先,我不得不说Google的推送通知文档似乎有点令人困惑,因为这里有很多替代方法,每个教程都有不同的方法来接收通知。

我已经导入了这些

import Firebase
import FirebaseInstanceID
import UserNotifications
import FirebaseMessaging

然后有代表团

class AppDelegate: UIResponder, UIApplicationDelegate, UNUserNotificationCenterDelegate, MessagingDelegate{
...
}

然后这是willFinishLaunchWithOptions方法

func application(_ application: UIApplication, willFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey : Any]? = nil) -> Bool {
        FirebaseApp.configure()
        Messaging.messaging().delegate = self



        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
        } else {
            let settings: UIUserNotificationSettings =
                UIUserNotificationSettings(types: [.alert, .badge, .sound], categories: nil)
            application.registerUserNotificationSettings(settings)

        }

        application.registerForRemoteNotifications()


        return true
    }

这是Messaging委托函数。

func messaging(_ messaging: Messaging, didReceive remoteMessage: MessagingRemoteMessage) {
        print("MEssage ->  \(remoteMessage.appData)")
    }

    func messaging(_ messaging: Messaging, didReceiveRegistrationToken fcmToken: String) {
        print("Firebase registration token: \(fcmToken)")
    }

该功能在Firebase文档中,用于设置apns令牌。

func application(application: UIApplication,
                     didRegisterForRemoteNotificationsWithDeviceToken deviceToken: NSData) {
        Messaging.messaging().apnsToken = deviceToken as Data
    }

我已经从FCM发送了数百个通知,并且它已成功在服务器端发送,但是当我记录接收到的消息时,收入数据没有任何内容。

如果您问为什么我要在willFinish函数中实现配置,则在文档中会有类似的注释。

        For devices running iOS 10 and above, you must assign the
     UNUserNotificationCenter's delegate property and FIRMessaging's 
delegate property. For example, in an iOS app, assign it in the 
applicationWillFinishLaunchingWithOptions: or 
applicationDidFinishLaunchingWithOptions: method of the app delegate.

对于您的任何帮助,我们将不胜感激,因为目前尚很难确定问题所在。

5 个答案:

答案 0 :(得分:2)

我正在使用FirebaseMessaging v4.1.4,并且已启用方法切换

application.registerForRemoteNotifications()->首先

Messaging.messaging()。delegate = self->第二

您必须在请求远程通知之后设置消息传递委托。 (我注意到您是在请求许可之前调用它的)

在我的情况下,因为我在iOS 13的didRegisterForRemoteNotificationsWithDeviceToken方法中未收到APNS令牌之前就调用了它,而生成了火力令牌。

也许是因为未生成APNS,并且Firebase没有与APNS令牌映射,所以您可能未收到任何通知。

答案 1 :(得分:1)

  1. 请检查您是否在项目功能中激活了推送通知

  2. 创建开发APNs证书并将其添加到Firebase控制台项目设置

  3. 在您的应用程序委托中

    import FirebaseMessaging
    import UserNotifications
    
    
    
    @UIApplicationMain
    class AppDelegate: UIResponder, UIApplicationDelegate, 
    UNUserNotificationCenterDelegate, MessagingDelegate {
    
    var window: UIWindow?
    
    
    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
         // Override point for customization after application launch.
    
    
    
        //REMOTE NOTIFICATION
    
        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 })
        } else {
            let settings: UIUserNotificationSettings =
            UIUserNotificationSettings(types: [.alert, .badge, .sound], categories: nil)
            application.registerUserNotificationSettings(settings)
        }
    
        application.registerForRemoteNotifications()
    
        Messaging.messaging().delegate = self
    
        let token = Messaging.messaging().fcmToken
        print("FCM token: \(token ?? "")")
    
    
        //Added Code to display notification when app is in Foreground
        if #available(iOS 10.0, *) {
            UNUserNotificationCenter.current().delegate = self
        } else {
            // Fallback on earlier versions
        }
    
    
        return true
    }
    
    
    func application(_ application: UIApplication,
                 didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
        Messaging.messaging().apnsToken = deviceToken as Data
    }
    
    func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any]) {
        // Print full message.
        print(userInfo)
    
    }
    
    // This method will be called when app received push notifications in foreground
    @available(iOS 10.0, *)
    func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void)
    { completionHandler([UNNotificationPresentationOptions.alert,UNNotificationPresentationOptions.sound,UNNotificationPresentationOptions.badge])
    }
    
    
    // MARK:- Messaging Delegates
    func messaging(_ messaging: Messaging, didReceiveRegistrationToken fcmToken: String) {
        InstanceID.instanceID().instanceID { (result, error) in
            if let error = error {
                print("Error fetching remote instange ID: \(error)")
            } else if let result = result {
                print("Remote instance ID token: \(result.token)")
            }
        }
    }
    
    
    func messaging(_ messaging: Messaging, didReceive remoteMessage: MessagingRemoteMessage) {
        print("received remote notification")
    }
    
    
    
    }
    

答案 2 :(得分:1)

他们说 警告:要以这种方式使用FCM直接通道,必须使用旧版HTTP API发送消息。 HTTP v1 API对所有发送到iOS设备的消息使用APN。 你需要把 Messaging.messaging()。should EstablishmentDirectChannek = true

答案 3 :(得分:1)

我在ios 13设备上有相同的问题。但是在低于ios 13的设备上运行正常。 问题是在iOS 13设备上没有调用didRegisterForRemoteNotificationsWithDeviceToken方法。 经过一番研究,我发现这是由于网络连接问题。然后,我从办公室的wifi切换到了蜂窝网络,此后它开始工作了。 didRegisterForRemoteNotificationsWithDeviceToken返回了设备令牌。

答案 4 :(得分:0)

我回答了一个类似的问题 here

关键的一步是在功能中启用“推送通知”。

祝大家编码愉快!