推送通知在使用FCM时在iOS上无法正常工作

3

我已经实现了 FCM。没有显示错误,Token 成功生成。但是设备上没有收到通知。

我遵循了 Google 的文档。

这是我的 AppDelegate 类。

import Foundation
import UIKit
import Firebase

//https://github.com/firebase/quickstart-ios/blob/5694c29ac5a8dcc672ec13f55f0ab74a6b9af11e/messaging/MessagingExampleSwift/AppDelegate.swift#L40-L55
class AppDelegate: UIResponder, UIApplicationDelegate, UNUserNotificationCenterDelegate, MessagingDelegate {


func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey : Any]? = nil) -> Bool {

    FirebaseApp.configure()
    Messaging.messaging().delegate = self

              // 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 })
    
    return true
}


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

// [START receive_message]
func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable: Any],
                 fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
  // Print full message.
  print(userInfo)
  completionHandler(UIBackgroundFetchResult.newData)
}


func application(_ application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: Error) {
  print("Unable to register for remote notifications: \(error.localizedDescription)")
}
    
func messaging(_ messaging: Messaging, didReceiveRegistrationToken fcmToken: String) {
  print("Firebase registration token: \(String(describing: fcmToken))")
  
  let dataDict:[String: String] = ["token": fcmToken ?? ""]
  NotificationCenter.default.post(name: Notification.Name("FCMToken"), object: nil, userInfo: dataDict)

}


}

didReceiveRegistrationToken代理方法调用两次

以下是我的功能enter image description here

我尝试从Firebase云消息传递选项发送通知,但未推送到设备中的任何Push。 我还尝试了postman。这是我的负载和输出。

enter image description here

我已在Firebabse中上传了APNS证书。 enter image description here

我尝试过主项目,但它不起作用。然后我尝试了一个虚拟项目,但也不起作用。

这是我的源代码。 你可以检查这个。 https://www.dropbox.com/s/22wojqv5u3vwjzt/Swift%20UI%20Push%20Notification.zip?dl=0


检查有效载荷,但在文档中,FCM用于APNS的有效载荷是不同的。 - Adarsh KC
在iOS上使用FCM总是很困难,而在Android上则更好用。曾经与Firebase团队长时间讨论过他们不稳定的iOS FCM问题。请在未来使用APNs,它始终很稳定。 - rana5ohaib
这里也有同样的问题。你找到解决方案了吗? - Dmitrij Rogov
2个回答

1
我认为APNS的有效负载与FCM不同。从我找到的文档中,我发现以下是在APNS中使用的FCM有效负载格式。
 {
   "message":  {
                "token" : "bk3RNwTe3H0:CI2k_HHwgIpoDKCIZvvDMExUdFQ3P1...",
                "notification" : {
                                  "body" : "This is an FCM notification that displays an image.!",
                                  "title" : "FCM Notification",
                                  },
                "apns": {
                       "payload": {
                                  "aps": {
                                         "mutable-content": 1
                                         }
                                  },
                "fcm_options": {
                                "image": "url-to-image"
                                }
                        }
               }
 }

1
这个在Postman中不起作用。然而,我的有效载荷在另一个应用程序中是有效的。 - Shourob Datta

-1

您可以添加所有的UNUserNotificationCenterDelegate方法,以及completionHandler([.alert, .badge, .sound])。


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