swift 3,ios 10 – 未收到推送通知firebase

我使用 this tutorialthis project作为参考.这是我在AppDelegate中的代码

import UIKit 
import UserNotifications 
import Firebase 

@UIApplicationMain 
class AppDelegate: UIResponder,UIApplicationDelegate { 

     var window: UIWindow? 
     let gcmMessageIDKey = "gcm.message_id" 
     let preferences = UserDefaults.standard 

     func application(_ application: UIApplication,didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool { 

         Messaging.messaging().delegate = self 

         if #available(iOS 10.0,*) { 
             UNUserNotificationCenter.current().delegate = self 

             let authOptions: UNAuthorizationoptions = [.alert,.badge,.sound] 
             UNUserNotificationCenter.current().requestAuthorization( 
                 options: authOptions,completionHandler: {_,_ in }) 
         } else { 
             let settings: UIUserNotificationSettings = 
                 UIUserNotificationSettings(types: [.alert,.sound],categories: nil) 
             application.registerUserNotificationSettings(settings) 
         } 

         application.registerForRemoteNotifications() 
         FirebaseApp.configure() 
         return true 
     } 

     func application(_ application: UIApplication,didReceiveRemoteNotification userInfo: [AnyHashable: Any]) { 
         if let messageID = userInfo[gcmMessageIDKey] { 
             print("Message ID: \(messageID)") 
         } 

         print("message1:",userInfo) 

     } 

     func application(_ application: UIApplication,didReceiveRemoteNotification userInfo: [AnyHashable: Any],fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) { 
         if let messageID = userInfo[gcmMessageIDKey] { 
             print("Message ID: \(messageID)") 
         } 

         print("message2:",userInfo) 

         completionHandler(UIBackgroundFetchResult.newData) 

     } 

     func application(_ application: UIApplication,didFailToRegisterForRemoteNotificationsWithError error: Error) {

         print("Unable to register for remote notifications: \(error.localizedDescription)") 
     } 

     func application(_ application: UIApplication,didRegisterForRemoteNotificationsWithDevicetoken devicetoken: Data) { 

         print("APNs token retrieved: \(devicetoken)") 
         let apn = devicetoken.map { String(format: "%02.2hhx",$0) }.joined() 

     } 

     func application(application: UIApplication,didReceiveRemoteNotification userInfo: [NSObject : AnyObject],fetchCompletionHandler completionHandler: (UIBackgroundFetchResult) -> Void) { 

         Messaging.messaging().appDidReceiveMessage(userInfo) 

     } 
} 

@available(iOS 10,*) 
extension AppDelegate : UNUserNotificationCenterDelegate {

     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)") 
         } 

         print("userinfo",userInfo) 

         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",userInfo) 

         completionHandler() 
     } 
 } 


extension AppDelegate : MessagingDelegate { 

     func messaging(_ messaging: Messaging,didRefreshRegistrationToken fcmToken: String) { 

         print("Firebase registration token: \(fcmToken)") 

     } 

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

     } 
 }

我得到了fcmtoken和APNs令牌.我上传了我的开发APNs证书.我已将功能上的推送通知变为ON.我在我的设备上试过了.我在我的项目中添加了GoogleService-Info.plist.但我没有从前台后台收到任何通知.

我的代码是否有任何错误?或者我应该更新我的证书吗?顺便说一下,我对此很新.

任何人都可以帮我解决吗?对此,我真的非常感激.

谢谢!

解决方法

您必须在数据库注册devicetokenString或devicetokenId,并且您必须根据需要获得正确的分发或开发证书,这是接收推送通知的基本需求.

从您的代码中,您没有注册devicetokenString / Id.

谢谢

相关文章

软件简介:蓝湖辅助工具,减少移动端开发中控件属性的复制和粘...
现实生活中,我们听到的声音都是时间连续的,我们称为这种信...
前言最近在B站上看到一个漂亮的仙女姐姐跳舞视频,循环看了亿...
【Android App】实战项目之仿抖音的短视频分享App(附源码和...
前言这一篇博客应该是我花时间最多的一次了,从2022年1月底至...
因为我既对接过session、cookie,也对接过JWT,今年因为工作...