簡體   English   中英

迅速3,iOS 10-未收到推送通知Firebase

[英]swift 3, ios 10 - push notification firebase is not received

我正在使用本教程該項目作為參考。 這是我在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, .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。 但是我沒有從前台或后台收到任何通知。

我在代碼上犯了任何錯誤嗎? 還是我需要更新證書? 順便說一下,我對此很陌生。

有人可以幫我解決嗎? 對此,我真的非常感激。

謝謝!

您必須在數據庫中注冊deviceTokenStringdeviceTokenId ,並且必須根據需要獲得正確的分發或開發證書,這是接收推送通知的基本需求。

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

謝謝

伙計們,這是因為我還沒有上傳生產APNs證書。 我以為我不需要它,因為我在本教程中看到了,它僅使用Development APNs證書。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM