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

查看:103
本文介绍了推送通知不起作用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证书和一份Apple发行证书:

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:

首次登录到您的Apple开发者帐户->标识符->单击您的应用程序标识符并启用推送通知. 启用通知后,它将询问您CertificateSigningRequest.certSigningRequest,您需要打开钥匙串访问并打开单击证书助手->从证书颁发机构请求证书

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.

现在进入代码,添加Pod并安装

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天全站免登陆