4

我使用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。但我沒有收到任何來自前景或背景的通知。

我在代碼上犯了什麼錯誤嗎?或者我需要更新我的證書?順便提一下,我對此很新。

任何人都可以幫我解決它嗎?對此,我真的非常感激。

謝謝!

回答

0

這是因爲我沒有上傳生產APNs證書,夥計們。我以爲我不需要它,因爲我看到了this tutorial,它只使用了開發APNs證書。

1

您必須先註冊你的deviceTokenStringdeviceTokenId在數據庫中,你必須有正確的分佈或根據您的需要開發證書,這東西基本都是需要接收推送通知。

從您的代碼中,您沒有註冊deviceTokenString/Id

謝謝

+0

我已經添加了代碼添加deviceTokenString到數據庫,但仍然沒有通知出現。我也有正確的開發證書。我不知道這一點。謝謝你的迴應:) – cathrinnatalia