【问题标题】:Swift Firebase iOS didReceiveRemoteNotification never callingSwift Firebase iOS didReceiveRemoteNotification 从不调用
【发布时间】:2020-05-07 20:08:44
【问题描述】:

我在我的 iOS 应用程序中使用 Firebase 并在服务器上使用 Azure Notification Hub 来发送通知。 我已经在this 和this 文章中制作了所有需要的东西。 这是我的 AppDelegate.swift 代码:

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
    if #available(iOS 10.0, *) {
      // For iOS 10 display notification (sent via APNS)
      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()
    Messaging.messaging().delegate = self

    return true
}

在此之后,我将调用服务器端在 Azure 通知中心注册我的应用程序以获取通知。以下是我获取设备令牌的方式:

InstanceID.instanceID().instanceID { (result, error) in
      if let error = error {
        print("Error fetching remote instance ID: \(error)")
      } else if let result = result {
        let appGuid = KeychainService.loadAppGuid()
        let type = "apns"
        var str = "native.initClient('" + appGuid!
        str += "',  '" + AppName
        str += "', '\(type)', "
        str += "'\(result.token )'"
        str += ", '" + HUBNAME + "')"
        print(str)
        self.webViewController.callJavaScript(command: str)
      }
    }

这是我尝试在 AppDelegate.swift 中接收通知的方式:

func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable: Any]) {
  let gcmMessageIDKey = "gcm.message_id"
  if let messageID = userInfo[gcmMessageIDKey] {
    print("Message ID: \(messageID)")
  }
  print(userInfo)
}

func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable: Any],
                 fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
  let gcmMessageIDKey = "gcm.message_id"
  if let messageID = userInfo[gcmMessageIDKey] {
    print("Message ID: \(messageID)")
  }
  print(userInfo)
  completionHandler(UIBackgroundFetchResult.newData)
}

但是这些方法都没有被调用。推送通知证书、标识符和配置文件正确且有效。我在 Xcode Simulator 上运行此代码。为什么它不起作用?

【问题讨论】:

    标签: ios swift firebase push-notification azure-notificationhub


    【解决方案1】:

    您需要一个真实的设备来测试推送通知。

    【讨论】:

    • @siakdeta 你确定吗?有没有办法用 Xcode 模拟器测试一下?
    猜你喜欢
    • 1970-01-01
    • 2023-03-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-12-12
    • 2023-04-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多