• <tfoot id='PzO2X'></tfoot>

      1. <small id='PzO2X'></small><noframes id='PzO2X'>

        • <bdo id='PzO2X'></bdo><ul id='PzO2X'></ul>
        <i id='PzO2X'><tr id='PzO2X'><dt id='PzO2X'><q id='PzO2X'><span id='PzO2X'><b id='PzO2X'><form id='PzO2X'><ins id='PzO2X'></ins><ul id='PzO2X'></ul><sub id='PzO2X'></sub></form><legend id='PzO2X'></legend><bdo id='PzO2X'><pre id='PzO2X'><center id='PzO2X'></center></pre></bdo></b><th id='PzO2X'></th></span></q></dt></tr></i><div id='PzO2X'><tfoot id='PzO2X'></tfoot><dl id='PzO2X'><fieldset id='PzO2X'></fieldset></dl></div>

      2. <legend id='PzO2X'><style id='PzO2X'><dir id='PzO2X'><q id='PzO2X'></q></dir></style></legend>
      3. ios10、Swift 3 和 Firebase 推送通知 (FCM)

        时间:2023-07-29
          <bdo id='UsNOM'></bdo><ul id='UsNOM'></ul>

          <small id='UsNOM'></small><noframes id='UsNOM'>

          • <legend id='UsNOM'><style id='UsNOM'><dir id='UsNOM'><q id='UsNOM'></q></dir></style></legend>
            <i id='UsNOM'><tr id='UsNOM'><dt id='UsNOM'><q id='UsNOM'><span id='UsNOM'><b id='UsNOM'><form id='UsNOM'><ins id='UsNOM'></ins><ul id='UsNOM'></ul><sub id='UsNOM'></sub></form><legend id='UsNOM'></legend><bdo id='UsNOM'><pre id='UsNOM'><center id='UsNOM'></center></pre></bdo></b><th id='UsNOM'></th></span></q></dt></tr></i><div id='UsNOM'><tfoot id='UsNOM'></tfoot><dl id='UsNOM'><fieldset id='UsNOM'></fieldset></dl></div>
                <tbody id='UsNOM'></tbody>
              <tfoot id='UsNOM'></tfoot>

                • 本文介绍了ios10、Swift 3 和 Firebase 推送通知 (FCM)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我很难显示我从 FCM 通知控制台发送到我的设备的推送通知.我可以看到设备正在接收通知,因为我可以看到我发送的消息test8"

                  I am struggling to display my push notifications that I am sending to my device from the FCM notification console. I can see the device is receiving the notification because I can see the message I send "test8"

                  Connected to FCM.
                  %@ [AnyHashable("notification"): {
                      body = test8;
                      e = 1;
                  },
                  

                  但不管我的应用是在前台还是后台,我都不会显示通知.

                  But it does not matter if my app is in the foreground or background I don't get the notification displayed.

                  我在 info.plist 中添加了必需的后台模式 - 应用下载内容以响应推送通知".我的证书是正确的,生成令牌没有问题.我的应用正在接收通知,但只是不显示它们.

                  I have added "Required background modes - App downloads content in response to push notifications" to the info.plist. My certificates are correct and I have no issue generating a token. My app is receiving the notifications but just not displaying them.

                  import UIKit
                  import UserNotifications
                  import Firebase
                  import FirebaseInstanceID
                  import FirebaseMessaging
                  
                  @UIApplicationMain
                  class AppDelegate: UIResponder, UIApplicationDelegate {
                  
                      var window: UIWindow?
                  
                      func application(application: UIApplication,
                                       didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
                  
                          // [START register_for_notifications]
                          if #available(iOS 10.0, *) {
                              let authOptions : UNAuthorizationOptions = [.Alert, .Badge, .Sound]
                              UNUserNotificationCenter.currentNotificationCenter().requestAuthorizationWithOptions(
                                  authOptions,
                                  completionHandler: {_,_ in })
                  
                              // For iOS 10 display notification (sent via APNS)
                              UNUserNotificationCenter.currentNotificationCenter().delegate = self
                              // For iOS 10 data message (sent via FCM)
                              FIRMessaging.messaging().remoteMessageDelegate = self
                  
                          } else {
                              let settings: UIUserNotificationSettings =
                                  UIUserNotificationSettings(forTypes: [.Alert, .Badge, .Sound], categories: nil)
                              application.registerUserNotificationSettings(settings)
                              application.registerForRemoteNotifications()
                          }
                  
                          application.registerForRemoteNotifications()
                  
                          // [END register_for_notifications]
                  
                          FIRApp.configure()
                  
                          // Add observer for InstanceID token refresh callback.
                          NSNotificationCenter.defaultCenter().addObserver(self,
                                                                           selector: #selector(self.tokenRefreshNotification),
                                                                           name: kFIRInstanceIDTokenRefreshNotification,
                                                                           object: nil)
                  
                          return true
                      }
                  
                      // [START receive_message]
                      func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject],
                                       fetchCompletionHandler completionHandler: (UIBackgroundFetchResult) -> Void) {
                          // If you are receiving a notification message while your app is in the background,
                          // this callback will not be fired till the user taps on the notification launching the application.
                          // TODO: Handle data of notification
                  
                          // Print message ID.
                          print("Message ID: (userInfo["gcm.message_id"]!)")
                  
                          // Print full message.
                          print("%@", userInfo)
                      }
                      // [END receive_message]
                  
                      // [START refresh_token]
                      func tokenRefreshNotification(notification: NSNotification) {
                          if let refreshedToken = FIRInstanceID.instanceID().token() {
                              print("InstanceID token: (refreshedToken)")
                          }
                  
                          // Connect to FCM since connection may have failed when attempted before having a token.
                          connectToFcm()
                      }
                      // [END refresh_token]
                  
                      // [START connect_to_fcm]
                      func connectToFcm() {
                          FIRMessaging.messaging().connectWithCompletion { (error) in
                              if (error != nil) {
                                  print("Unable to connect with FCM. (error)")
                              } else {
                                  print("Connected to FCM.")
                              }
                          }
                      }
                      // [END connect_to_fcm]
                  
                      func applicationDidBecomeActive(application: UIApplication) {
                          connectToFcm()
                      }
                  
                      // [START disconnect_from_fcm]
                      func applicationDidEnterBackground(application: UIApplication) {
                          FIRMessaging.messaging().disconnect()
                          print("Disconnected from FCM.")
                      }
                      // [END disconnect_from_fcm]
                  }
                  
                  // [START ios_10_message_handling]
                  @available(iOS 10, *)
                  extension AppDelegate : UNUserNotificationCenterDelegate {
                  
                      // Receive displayed notifications for iOS 10 devices.
                      func userNotificationCenter(center: UNUserNotificationCenter,
                                                  willPresentNotification notification: UNNotification,
                                                  withCompletionHandler completionHandler: (UNNotificationPresentationOptions) -> Void) {
                          let userInfo = notification.request.content.userInfo
                          // Print message ID.
                          print("Message ID: (userInfo["gcm.message_id"]!)")
                          // Print full message.
                          print("%@", userInfo)
                      }
                  }
                  
                  extension AppDelegate : FIRMessagingDelegate {
                      // Receive data message on iOS 10 devices.
                      func applicationReceivedRemoteMessage(remoteMessage: FIRMessagingRemoteMessage) {
                          print("%@", remoteMessage.appData)
                      }
                  }
                  // [END ios_10_message_handling]
                  

                  我一直在尝试自己研究和解决这个问题,但我遇到了问题.任何帮助或建议将不胜感激.

                  I having been trying to research and solve this issue on my own but I am having issues. Any help or suggestions would be greatly appreciated.

                  推荐答案

                  didRegisterForRemoteNotificationsWithDeviceToken方法内部,添加如下代码:

                  Inside method didRegisterForRemoteNotificationsWithDeviceToken, add the following code:

                  func application(application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: NSData) {
                      let tokenChars = UnsafePointer<CChar>(deviceToken.bytes)
                      var tokenString = ""
                  
                      for i in 0..<deviceToken.length {
                          tokenString += String(format: "%02.2hhx", arguments: [tokenChars[i]])
                      }
                  
                      FIRInstanceID.instanceID().setAPNSToken(deviceToken, type: FIRInstanceIDAPNSTokenType.Unknown)
                  
                      print("tokenString: (tokenString)")
                  }
                  

                  别忘了在Capabilities中启用Push Notifications.

                  这篇关于ios10、Swift 3 和 Firebase 推送通知 (FCM)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:应用程序在 iOS 10 中处于后台时未收到推送通知 下一篇:Android FCM - 如何只显示一个通知

                  相关文章

                    <tfoot id='eAhVO'></tfoot>

                    1. <i id='eAhVO'><tr id='eAhVO'><dt id='eAhVO'><q id='eAhVO'><span id='eAhVO'><b id='eAhVO'><form id='eAhVO'><ins id='eAhVO'></ins><ul id='eAhVO'></ul><sub id='eAhVO'></sub></form><legend id='eAhVO'></legend><bdo id='eAhVO'><pre id='eAhVO'><center id='eAhVO'></center></pre></bdo></b><th id='eAhVO'></th></span></q></dt></tr></i><div id='eAhVO'><tfoot id='eAhVO'></tfoot><dl id='eAhVO'><fieldset id='eAhVO'></fieldset></dl></div>
                        <bdo id='eAhVO'></bdo><ul id='eAhVO'></ul>
                      <legend id='eAhVO'><style id='eAhVO'><dir id='eAhVO'><q id='eAhVO'></q></dir></style></legend>

                      <small id='eAhVO'></small><noframes id='eAhVO'>