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

问题描述:

我使用this tutorialthis project作为我的参考。这是我在AppDelegate中的代码swift 3,ios 10 - 推送通知firebase未收到

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, .badge, .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证书。我已将功能上的推送通知转为开启。我在我的设备上试过了。我在我的项目中添加了GoogleService-Info.plist。但我没有收到任何来自前景或背景的通知。

我在代码上犯了什么错误吗?或者我需要更新我的证书?顺便提一下,我对此很新。

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

谢谢!

这是因为我没有上传生产APNs证书,伙计们。我以为我不需要它,因为我看到了this tutorial,它只使用了开发APNs证书。

您必须先注册你的deviceTokenStringdeviceTokenId在数据库中,你必须有正确的分布或根据您的需要开发证书,这东西基本都是需要接收推送通知。

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

谢谢

+0

我已经添加了代码添加deviceTokenString到数据库,但仍然没有通知出现。我也有正确的开发证书。我不知道这一点。谢谢你的回应:) – cathrinnatalia