繁体   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