ios - Firebase 推送通知

标签 ios swift firebase apple-push-notifications firebase-cloud-messaging

我的 FCM 可以正常工作,但我无法让传统的横幅通知正常工作。

这是我的 AppDelegate.swift 文件中的内容:

import UIKit
import Firebase
import UserNotifications

@UIApplicationMain
final class AppDelegate: UIResponder {
  var window: UIWindow?

  override init() {
    super.init()    
    FIRApp.configure()
  }
}

// MARK: - UIApplicationDelegate
extension AppDelegate: UIApplicationDelegate {
  func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey : Any]? = nil) -> Bool {
    let authOptions: UNAuthorizationOptions = [.alert, .badge, .sound]
    UNUserNotificationCenter.current().requestAuthorization(options: authOptions, completionHandler: { _, _ in })
    UNUserNotificationCenter.current().delegate = self

    FIRMessaging.messaging().remoteMessageDelegate = self

    application.registerForRemoteNotifications()

    NotificationCenter.default.addObserver(self, selector: #selector(tokenRefreshNotification), name: .firInstanceIDTokenRefresh, object: nil)
    return true
  }

  func tokenRefreshNotification(notification: Notification) {
    if let refreshedToken = FIRInstanceID.instanceID().token() {
      print("InstanceID token: \(refreshedToken)")
    }

    connectToFcm()
  }

  func connectToFcm() {
    FIRMessaging.messaging().connect { error in
      if error != nil {
        print("Unable to connect with FCM. \(error)")
      } else {
        print("Connected to FCM.")
      }
    }
  }

  func applicationDidBecomeActive(_ application: UIApplication) {
    connectToFcm()
  }
}

// MARK: - UNUserNotificationCenterDelegate
extension AppDelegate: UNUserNotificationCenterDelegate {
  func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
    let userInfo = notification.request.content.userInfo

    print("Message ID: \(userInfo["gcm.message_id"]!)")

    print(userInfo)

    completionHandler([.alert, .sound])
  }
}

// MARK:
extension AppDelegate: FIRMessagingDelegate {
  func applicationReceivedRemoteMessage(_ remoteMessage: FIRMessagingRemoteMessage) {
    print("FIRMessagingRemoteMessage Received: \(remoteMessage.appData)")
  }
}

一切都是根据 documentation 配置的。当我通过 Firebase 上的通知控制台向我的应用发送消息时,我会收到控制台输出。

但是,发送通知时不会出现横幅。当应用程序处于后台时,通知似乎也不会发送 - 仅当应用程序位于前台时。

寻求帮助。提前致谢!

最佳答案

尝试禁用方法调配。

将 FirebaseAppDelegateProxyEnabled 添加到 info.plist 并将其设置为 NO

然后将此代码添加到应用程序委托(delegate)

func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
  FIRInstanceID.instanceID().setAPNSToken(deviceToken, type: FIRInstanceIDAPNSTokenTypeSandbox)
}

从设备中删除您的应用并重新安装。 请测试是否调用了 application:didRegisterForRemoteNotificationsWithDeviceToken 。如果调用了,现在尝试从 firebase 控制台发送通知。

关于ios - Firebase 推送通知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40200527/

相关文章:

ios - 文档文件夹内容 - 文件无法打开,因为没有这样的文件

ios - 如何释放为数组分配的内存?

ios - 导航栏 Swift 中的文本颜色

ios - 在iOS中,是否可以在自定义模式下直接打开通知?

angular - 从 firestore 返回嵌套集合作为 angularfire2 和 firebase 的对象

ios - Swift:在应用程序之间共享身份验证状态

android - Firebase Storage UI 和 Glide 不显示图像

IOS UIView layer setCornerRadius in ellipse 形状

ios - Swift:使用评估Javascript

objective-c - SOAP - 随机数 key 生成