Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/108.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 推送通知不起作用Xcode 11.3.1_Ios_Apple Push Notifications - Fatal编程技术网

Ios 推送通知不起作用Xcode 11.3.1

Ios 推送通知不起作用Xcode 11.3.1,ios,apple-push-notifications,Ios,Apple Push Notifications,以下是我的情况:我生成了一个生产推送通知证书。在Apple Developer Portal上,证书显示为: 我有苹果开发证书和苹果发行证书: 我使用Xcode 11.3.1创建了一个存档,并使用Ad Hoc分发了我的应用程序。以下是特别摘要: 我已启用推送通知,但它们不起作用。应用程序处于后台时,我无法接收通知。当我在Xcode 10.3中使用iPhone分发证书时,通知使用了相同的代码。我错过了什么?谢谢 以下是整合推送通知的过程,请一步一步地进行: 首先登录到您的apple开发者帐户-

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

我有苹果开发证书和苹果发行证书:

我使用Xcode 11.3.1创建了一个存档,并使用Ad Hoc分发了我的应用程序。以下是特别摘要:


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

以下是整合推送通知的过程,请一步一步地进行:

首先登录到您的apple开发者帐户->标识符->单击您的应用标识符并启用推送通知。 启用通知后,它将要求您提供CertificateSigningRequest.certSigningRequest,您需要打开Keychain access并单击“证书助手”->“从证书颁发机构申请证书”

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

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

现在打开你的firebase帐户,进入设置->云消息->添加你的production.p12和development.p12证书

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

现在来看看代码,添加pod并安装

pod 'Firebase/Analytics' 
pod 'Firebase/Messaging'
打开AppDelegate导入这些

import FirebaseAnalytics
import Firebase
import FirebaseMessaging
添加代理MessagingDelegate

启动让gcmMessageIDKey=gcm.message_id

将这些添加到didFinishLaunchingWithOptions方法中

添加后,在app delegate中添加这些方法,即可接收推送通知:

//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]



       }

以下是整合推送通知的过程,请逐步进行:

首先登录到您的apple开发者帐户->标识符->单击您的应用标识符并启用推送通知。 启用通知后,它将要求您提供CertificateSigningRequest.certSigningRequest,您需要打开Keychain access并单击“证书助手”->“从证书颁发机构申请证书”

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

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

现在打开你的firebase帐户,进入设置->云消息->添加你的production.p12和development.p12证书

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

现在来看看代码,添加pod并安装

pod 'Firebase/Analytics' 
pod 'Firebase/Messaging'
打开AppDelegate导入这些

import FirebaseAnalytics
import Firebase
import FirebaseMessaging
添加代理MessagingDelegate

启动让gcmMessageIDKey=gcm.message_id

将这些添加到didFinishLaunchingWithOptions方法中

添加后,在app delegate中添加这些方法,即可接收推送通知:

//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]



       }

请使用推送通知内容(即您尝试接收的APNS JSON)更新您的问题。请使用推送通知内容(即您尝试接收的APNS JSON)更新您的问题。在我启用的后台模式中:IP语音、后台提取、远程通知、音频、播放和画中画。我真的需要firebase吗?当我从Xcode 10.3临时部署应用程序时,我的通知在没有firebase的情况下工作。这是与Xcode本身有关的问题吗?对于推送通知,您需要配置firebase帐户或套接字编程。没有其他选项可以将推送通知与这两种功能集成在一起。对于通知,只需在后台模式下启用->检查后台获取和远程通知。为什么这一行被注释掉:://self.instanceIDTokenMessage.text=Remote InstanceID-token:result.token。我正在从旧版本升级,而InstancedTokenMessage似乎不再存在。在后台模式中,我已经启用了:IP语音、后台提取、远程通知、音频、播放和画中画。我真的需要firebase吗?当我从Xcode 10.3临时部署应用程序时,我的通知在没有firebase的情况下工作。这是与Xcode本身有关的问题吗?对于推送通知,您需要配置firebase帐户或套接字编程。对于integr,没有其他选项
ate推送通知与这两件事有关。对于通知,只需在后台模式下启用->检查后台获取和远程通知。为什么这一行被注释掉:://self.instanceIDTokenMessage.text=Remote InstanceID-token:result.token。我正在从旧版本升级,而InstancedTokenMessage似乎不再存在。不需要吗?