推送通知不起作用 Xcode 11.3.1 [英] Push notifications don't work Xcode 11.3.1

查看:27
本文介绍了推送通知不起作用 Xcode 11.3.1的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Here is my situation: I generated a production push notification certificate. On Apple Developer Portal the certificate appears as:

我有 Apple Dev Certificate 和 Apple Distribution Certificate:

I have an Apple Dev Certificate and an Apple Distribution Certificate:

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

I create an archive and distribute my app with Ad Hoc using Xcode 11.3.1. Here is the Ad Hoc summary:

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

I have enabled push notifications but they don't work. I can't receive notifications when app is in the background. When I used an iPhone Distribution certificate in Xcode 10.3 notifications worked with the same code. What am I missing? Thx!!

推荐答案

这里是集成推送通知的过程,一步一步来:

Here is the process of integrating push notification, follow it step by step:

首先登录您的苹果开发者帐户-> 标识符-> 单击您的应用标识符并启用推送通知.启用通知后,它会要求您提供 CertificateSigningRequest.certSigningRequest,您需要打开钥匙串访问并打开 点击 Certificate Assistant -> Request Certificate From Certificate Authority

First login to your apple developer account -> Identifiers -> click your app identifier and enable push notification. After enabling notification it will ask you for CertificateSigningRequest.certSigningRequest, You need to open Keychain access and open click Certificate Assistant -> Request Certificate From Certificate Authority

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

After creating that certificate you need to add that certificates in apple account. And download development.cer and aps.cer certificate from there.

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

After download these certificate just click on both certificate and this will open your keychain Access. Now left click on those certificate and export .p12 certificate that will ask you to generate password for that certificate.

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

Now open your firebase account and go to settings -> cloud messaging -> add your production.p12 and development.p12 certificate there.

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

Now back to xcode Go to app target -> Signin and capabilities -> add push notifications and add background modes, check background fetch and Remote notification.

现在来代码添加pods并安装

Come to the code now add pods and install

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

打开你的 AppDelegate 导入这些

Open your AppDelegate import these

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

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

After adding add these methods in app delegate and you are ready to receive push notifications:

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



       }

这篇关于推送通知不起作用 Xcode 11.3.1的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆