推送通知不起作用 Xcode 11.3.1

Push notifications don't work Xcode 11.3.1

这是我的情况:我生成了生产推送通知证书。在 Apple Developer Portal 上,证书显示为:

我有 Apple Dev 证书和 Apple Distribution 证书:

我使用 Xcode 11.3.1 创建一个存档并通过 Ad Hoc 分发我的应用程序。以下是临时摘要:

我启用了推送通知,但它们不起作用。当应用程序处于后台时,我无法收到通知。当我在 Xcode 中使用 iPhone 分发证书时,10.3 通知使用相同的代码。我错过了什么?谢谢!!

下面是集成推送通知的过程,一步一步来:

首先登录您的苹果开发者账户->标识符->点击您的应用标识符并启用推送通知。 启用通知后,它会要求您 CertificateSigningRequest.certSigningRequest,您需要打开钥匙串访问并打开 单击证书助手 -> 从证书颁发机构请求证书

创建该证书后,您需要在苹果帐户中添加该证书。并从那里下载 development.cer 和 aps.cer 证书。

下载这些证书后,只需单击两个证书,这将打开您的钥匙串访问。现在左键单击那些证书并导出 .p12 证书,它会要求您为该证书生成密码。

现在打开您的 firebase 帐户并转到设置 -> 云消息传递 -> 在那里添加您的 production.p12 和 development.p12 证书。

现在回到 xcode 转到应用程序目标 -> 登录和功能 -> 添加推送通知和添加后台模式,检查后台获取和远程通知。

来到代码现在添加pods并安装

pod 'Firebase/Analytics' 
pod 'Firebase/Messaging'

打开你的 AppDelegate 导入这些

import FirebaseAnalytics
import Firebase
import FirebaseMessaging

添加委托 MessagingDelegate

发起let gcmMessageIDKey = "gcm.message_id"

didFinishLaunchingWithOptions方法中添加这些

       FirebaseApp.configure()

        Messaging.messaging().delegate = self
        //Added push 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().isAutoInitEnabled = true

在应用委托中添加这些方法后,您就可以接收推送通知了:

//Push Notifications

   func application(application: UIApplication,
                       didRegisterForRemoteNotificationsWithDeviceToken deviceToken: NSData) {
          Messaging.messaging().apnsToken = deviceToken as Data
      }
      func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable: Any]) {

          InstanceID.instanceID().instanceID { (result, error) in
              if let error = error {
                  print("Error fetching remote instance ID: \(error)")
              } else if let result = result {
                  print("Remote instance ID token: \(result.token)")
                  //  self.instanceIDTokenMessage.text  = "Remote InstanceID token: \(result.token)"
              }
          }

          print(userInfo)
      }

      func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable: Any],
                       fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {

          if let messageID = userInfo[gcmMessageIDKey] {
              print("Message ID: \(messageID)")
          }

          print(userInfo)

          completionHandler(UIBackgroundFetchResult.newData)
      }

    func userNotificationCenter(_ center: UNUserNotificationCenter,
                                   willPresent notification: UNNotification,
                                   withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
           let userInfo = notification.request.content.userInfo

           if let messageID = userInfo[gcmMessageIDKey] {
               print("Message ID: \(messageID)")
           }

           completionHandler([])
       }

       func userNotificationCenter(_ center: UNUserNotificationCenter,
                                   didReceive response: UNNotificationResponse,
                                   withCompletionHandler completionHandler: @escaping () -> Void) {
           let userInfo = response.notification.request.content.userInfo

           if let messageID = userInfo[gcmMessageIDKey] {
               print("Message ID: \(messageID)")
           }

           print(userInfo)

           completionHandler()
       }


       func messaging(_ messaging: Messaging, didReceiveRegistrationToken fcmToken: String) {
           print("Firebase registration token: \(fcmToken)")
           let dataDict:[String: String] = ["token": fcmToken]


           NotificationCenter.default.post(name: Notification.Name("FCMToken"), object: nil, userInfo: dataDict)

           UserDefaults.standard.set(fcmToken, forKey: "FCMToken")
           UserDefaults.standard.synchronize()

       }

       func messaging(_ messaging: Messaging, did remoteMessage: MessagingRemoteMessage) {
           print("Received data message: \(remoteMessage.appData)")
       }

       func messaging(_ messaging: Messaging, didReceive remoteMessage: MessagingRemoteMessage) {
           print("Received data message: \(remoteMessage.appData)")
       }

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

       }

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

           let dataDict:[String: String] = ["token": fcmToken]



       }