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

9

我是一位有用的助手,可以为您进行翻译。以下是需要翻译的内容:

我目前正在处理通知问题,但是我无法从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

我已经启用了来自“后台获取”中的“推送通知”和“远程通知”,并且已经阅读了类似的另一个主题,位于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.

我希望你能帮我,因为目前很难看出问题所在。这涉及到IT技术方面的内容。


感谢您的关注,由于没有收到日志,因此未收到任何通知。 - eemrah
通知和消息是有区别的。消息基于数据,而通知则会出现在屏幕上。只有当应用程序处于后台或关闭状态时,通知才会出现。如果应用程序在前台运行,通知将不会出现。 - Jen Person
是的,但我尝试了两种方式来查看当前发生了什么,即使在前台测试中,也没有任何书面消息。 - eemrah
我已经使用了NWPusher,它运行良好。我可以通过手动推送获取可哈希通知数据,但是我不知道为什么在FCM上无法工作。 - eemrah
1
@AakashDave 谢谢您的回复,现在它运行得很好。我会再次阅读文档。 - eemrah
显示剩余2条评论
5个回答

12
  1. Please check if you have activated Push Notifications in your Project Capabilities

  2. Create Development APNs certificate and add it to Firebase Console Project Settings

  3. In your App Delegate

    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")
    }
    
    
    
    }
    

7
我正在使用FirebaseMessaging v4.1.4,已启用方法交换。
application.registerForRemoteNotifications() --> 第一步
Messaging.messaging().delegate = self --> 第二步
在请求远程通知后,必须设置messaging delegate。(我注意到您在请求权限之前调用了它)
在我的情况下,因为我在请求之前调用它,所以我没有在didRegisterForRemoteNotificationsWithDeviceToken方法中收到iOS 13的APNS Token,而是生成了firebase token。
也许因为APNS未生成,而且firebase未与APNS Token映射,您可能没有收到任何通知。

我有同样的问题,在iOS13中didRegisterForRemoteNotificationsWithDeviceToken没有被调用。但在iOS12中它正常工作。如果您解决了这个问题,请分享代码。 - Faris Muhammed
上述提到的修复是我为解决iOS 13问题所做的。在didRegisterForRemoteNotificationsWithDeviceToken方法中,您能获取APNS令牌以及Firebase令牌吗? - kadar
我的问题已经解决。这是一个网络连接问题。 - Faris Muhammed

2

我在iOS 13设备上遇到了同样的问题。但在iOS 13以下的设备上,它能正常工作。 问题出在didRegisterForRemoteNotificationsWithDeviceToken方法在iOS 13设备上没有被调用。 经过一些研究,我发现这是由于网络连接问题造成的。然后我从办公室的WiFi切换到蜂窝网络,之后它就可以工作了。didRegisterForRemoteNotificationsWithDeviceToken返回了设备令牌。


我关闭了蜂窝数据,再重新打开,然后它就正常工作了!谢谢! - Hwangho Kim

2
他们说:要以这种方式使用FCM直接通道,您必须使用传统的HTTP API发送消息。对于发送到iOS设备的所有消息,HTTP v1 API都使用APNs。而且您需要添加Messaging.messaging().shouldEstablishDirectChannel = true。

0

我在这里回答了一个类似的问题。

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

祝大家编程愉快!


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接