【问题标题】:swift 3, ios 10 - push notification firebase is not receivedswift 3,ios 10 - 未收到推送通知 firebase
【发布时间】:2017-10-20 05:10:50
【问题描述】:

我使用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, .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。但是我没有收到来自前台或后台的任何通知。

我的代码有什么错误吗?还是我需要更新我的证书?顺便说一句,我对此很陌生。

谁能帮我解决?对此,我真的非常感激。

谢谢!

【问题讨论】:

    标签: ios swift3 push-notification firebase-cloud-messaging firebase-notifications


    【解决方案1】:

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

    根据您的代码,您没有注册deviceTokenString/Id

    谢谢

    【讨论】:

    • 我已经添加了将 deviceTokenString 添加到数据库的代码,但仍然没有出现通知。我也有正确的开发证书。我对此一无所知。顺便感谢您的回复:)
    【解决方案2】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-01-23
      • 2017-02-25
      • 2017-10-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-01-26
      • 2016-12-20
      相关资源
      最近更新 更多