【问题标题】:Push Notification NotWorking in IOS 11推送通知在 IOS 11 中不起作用
【发布时间】:2017-11-07 02:52:41
【问题描述】:

您好,我正在尝试实现 google firebase 推送通知。

我正在使用 IOS 11Xcode 9.0

Pod 文件

target 'VQ Smart Home' do
  # Comment the next line if you're not using Swift and don't want to use dynamic frameworks
  use_frameworks!

  # Pods for VQ Smart Home
        pod 'SwiftyJSON'
        pod 'NVActivityIndicatorView'
        pod 'FirebaseInstanceID', '2.0.0'
        pod 'Firebase/Core'
        pod 'Firebase/Messaging'
end

应用代理

 func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {

        UINavigationBar.appearance().isTranslucent = true

    /************************ FireBase Notification ************************************************************************/
        FirebaseApp.configure()
        if #available(iOS 10.0, *) {
            UNUserNotificationCenter.current().delegate = self as? UNUserNotificationCenterDelegate
            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()

        let token = Messaging.messaging().fcmToken
        print("*********************************************************************************************************************")
        print("FCM token: \(token ?? "")")
        print("*********************************************************************************************************************")
    /************************ FireBase Notification ************************************************************************/

        return true
    }

 // Push Notification Methods
    func messaging(_ messaging: Messaging, didRefreshRegistrationToken fcmToken: String) {
        print("Firebase registration token: \(fcmToken)")
    }

    func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable: Any]) {
        print(userInfo)
    }

    func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable: Any],
                     fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
        print(userInfo)
        completionHandler(UIBackgroundFetchResult.newData)
    }

错误

*********************************************************************************************************************
FCM token: dEukPynDndQ:APA91bG41IAmwarrpdqoUmYxQQ_a6Ro-13YycSXTTyrqRHDGnroJ0pI2cSS5XYIQJwL50s07zUJcaujuj7SY-F5jO_bVEkVpFFyz4jylmwAlgief65Cpl6_TSgNNXkd-eDse7p1uk_rE
*********************************************************************************************************************
- [BoringSSL] Function boringssl_context_get_peer_sct_list: line 1754 received sct extension length is less than sct data length
- [MC] Lazy loading NSBundle MobileCoreServices.framework
- [MC] Loaded MobileCoreServices.framework
- [MC] System group container for systemgroup.com.apple.configurationprofiles path is /private/var/containers/Shared/SystemGroup/systemgroup.com.apple.configurationprofiles
- refreshPreferences: HangTracerEnabled: 0
- refreshPreferences: HangTracerDuration: 500
- refreshPreferences: ActivationLoggingEnabled: 0 ActivationLoggingTaskedOffByDA:0
- [Firebase/Analytics][I-ACS023007] Firebase Analytics v.40004000 started
- [Firebase/Analytics][I-ACS023008] To enable debug logging set the following application argument: -FIRAnalyticsDebugEnabled (URL)
- TIC Read Status [1:0x0]: 1:57
- TIC Read Status [1:0x0]: 1:57

我已向 Firebase 添加了正确的 APNs 密钥。仍然没有收到推送通知。有人可以帮我解决这个问题。 Tnx。

【问题讨论】:

    标签: ios swift firebase push-notification


    【解决方案1】:

    找到解决办法

    更新 podfile

    pod 'Firebase/Core', '4.0.4'
    pod 'Firebase/Database', '4.0.4'
    pod 'Firebase/Messaging', '4.0.4'
    pod 'FirebaseInstanceID', '2.0.0'
    

    添加此方法didRegisterForRemoteNotificationsWithDeviceToken

    应用代理

    func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
            Messaging.messaging().apnsToken = deviceToken
        }
    

    现在尝试通过firebase Ui发送一个推送通知,你会在这个方法中收到它(didReceiveRemoteNotification

     func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable: Any],
                         fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
            print(userInfo)
            let aps = userInfo["aps"] as! NSDictionary
            print(aps["alert"]  as? String ?? "")
            completionHandler(UIBackgroundFetchResult.newData)
        }
    

    【讨论】:

      【解决方案2】:

      试试这个:

      XCode 10、Swift 4、配置推送通知

      import UserNotifications
      
      @UIApplicationMain
      class AppDelegate: UIResponder, UIApplicationDelegate, UNUserNotificationCenterDelegate {
      
          func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
              //This line is very import:
              UNUserNotificationCenter.current().delegate = self
      
              if #available(iOS 10.0, *) {
                  let center = UNUserNotificationCenter.current()
                  center.requestAuthorization(options: [.badge, .alert, .sound], completionHandler: {(granted, error) in
                      if error == nil {
                          DispatchQueue.main.async(execute: {
                              UIApplication.shared.registerForRemoteNotifications()
                          })
                      }
                  })
              } else {
               UIApplication.shared.registerUserNotificationSettings(UIUserNotificationSettings(types: [.sound, .badge, .alert], categories: nil))
                  UIApplication.shared.registerForRemoteNotifications()
              }
              return true
          }
      
          @available(iOS 10.0, *)
          func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
              print("willPresent, userInfo: ", notification.request.content.userInfo)
              completionHandler([.alert, .badge, .sound])
          }
      }
      

      向用户界面展示:

      private func SendNotification(identifier: String, title: String, body: String){
              let content = UNMutableNotificationContent()
              content.title = title
              content.body = body
              content.sound = UNNotificationSound.default
              let trigger = UNTimeIntervalNotificationTrigger(timeInterval: 5, repeats: false)
              let request = UNNotificationRequest(identifier: identifier, content: content, trigger: trigger)
              UNUserNotificationCenter.current().add(request, withCompletionHandler: nil)
          }
      

      【讨论】:

        猜你喜欢
        • 2018-04-24
        • 1970-01-01
        • 2018-03-05
        • 1970-01-01
        • 2015-06-28
        • 2019-09-22
        • 2015-12-19
        • 1970-01-01
        相关资源
        最近更新 更多