gpt4 book ai didi

ios - Swift Firebase iOS didReceiveRemoteNotification 从不调用

转载 作者:行者123 更新时间:2023-11-28 23:19:30 24 4
gpt4 key购买 nike

我在我的 iOS 应用程序中使用 Firebase,并在服务器上使用 Azure 通知中心来发送通知。我已经制作了所有需要的东西,如 thisthis文章。这是我的 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 通知中心注册我的应用程序以获取通知。以下是我获取设备 token 的方式:

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 模拟器上运行这段代码。为什么它不起作用?

最佳答案

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

关于ios - Swift Firebase iOS didReceiveRemoteNotification 从不调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59846611/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com