在将我的应用程序更新为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和Clean>运行应用程序时调用此函数
函数注册通知()
{
uiapplication.sharedapplication()
让application=uiapplication.sharedapplication()
if application.respondstoselector(选择器(uiapplication.registerusernotificationsettings(:))
{
let settings = UIUserNotificationSettings(forTypes: [.Alert, .Badge, .Sound], categories: nil)
application.registerUserNotificationSettings(settings)
application.registerForRemoteNotifications()
}
}
func应用程序(应用程序:uiapplication,didregisterusernotificationsettings notificationsettings:uiusernotificationsettings){
if notificationSettings.types != .None {
application.registerForRemoteNotifications()
}
}
关于swift - 升级到Swift 3.0后没有收到来自Azure的APNS消息,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/40213253/