gpt4 book ai didi

swift - 升级到 Swift 3.0 后未收到来自 Azure 的 APNS 消息

转载 作者:搜寻专家 更新时间:2023-11-01 05:43:54 25 4
gpt4 key购买 nike

将我的应用程序更新到 Swift 3 后(这真是一次体验!) - 我知道由于 iOS10 中的更改,我必须重新编写通知代码。

使用这个有用的资源 - 我能够重新编码并且获得有效的连接

https://github.com/ashishkakkad8/Notifications10Swift/blob/master/Notifications10Swift/AppDelegate.swift

我还修改了该项目的功能,添加了 1) 通知和 2) 后台模式/远程通知。

但是,当我通过 Azure“测试发送”功能发送测试消息时,我没有收到它。

这是我的 AppDelegate 中的相关代码:

var window: UIWindow?

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

// Register for remote notifications
if #available(iOS 10.0, *) {
let center = UNUserNotificationCenter.current()
center.delegate = self
center.requestAuthorization(options: [.sound, .alert, .badge]) { (granted, error) in
if error == nil{
UIApplication.shared.registerForRemoteNotifications()
}
}
}
else {
UIApplication.shared.registerUserNotificationSettings(UIUserNotificationSettings(types: [.sound, .alert, .badge], categories: nil))
UIApplication.shared.registerForRemoteNotifications()
}
}

func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data)
{
let hub: SBNotificationHub = SBNotificationHub.init(connectionString: APISettings.Instance.notificationHubEndpoint, notificationHubPath: APISettings.Instance.notificationHubName)

hub.registerNative(withDeviceToken: deviceToken, tags: nil) { (error) -> Void in

if (error != nil){
print("Error registering for notifications: %@", error)
} else {
print("Registration ok")
}
}
}

func application(_ application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: Error)
{
print("Error = ",error.localizedDescription)
}

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

// MARK: UNUserNotificationCenter Delegate // >= iOS 10

@available(iOS 10.0, *)
func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
print("User Info = ",notification.request.content.userInfo)
completionHandler([.alert, .badge, .sound])
}

@available(iOS 10.0, *)
func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
print("User Info = ",response.notification.request.content.userInfo)
completionHandler()
}

任何帮助将不胜感激。

最佳答案

尝试在 didFinishLaunchingWithOptions 上调用此函数并清理>运行您的应用

函数registerNotification()

{

UIApplication.sharedApplication() 让应用程序 = UIApplication.sharedApplication()

if application.respondsToSelector(#selector(UIApplication.registerUserNotificationSettings(_:)))

{

        let settings = UIUserNotificationSettings(forTypes: [.Alert, .Badge, .Sound], categories: nil)


application.registerUserNotificationSettings(settings)
application.registerForRemoteNotifications()

}

}

func application(application: UIApplication, didRegisterUserNotificationSettings notificationSettings: UIUserNotificationSettings) {

    if notificationSettings.types != .None {
application.registerForRemoteNotifications()
}
}

关于swift - 升级到 Swift 3.0 后未收到来自 Azure 的 APNS 消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40213253/

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