【发布时间】:2018-09-11 20:02:54
【问题描述】:
我目前正在将我的应用程序从 Objective-C 重写为 Swift 并处理通知。出于某种原因,Swift 版本的应用没有收到任何远程推送通知,而 Objective-C 版本却收到了。
这是我用来在 AppDelegate 中注册通知的代码:
UNUserNotificationCenter.current().delegate = self
let options: UNAuthorizationOptions = [.badge, .alert, .sound]
UNUserNotificationCenter.current().requestAuthorization(options: options, completionHandler: { granted, error in
print("access granted: \(granted)")
})
application.registerForRemoteNotifications()
我假设应用程序成功注册,因为 didRegisterForRemoteNotificationsWithDeviceToken 方法被调用。但是当我尝试使用从该方法获得的令牌发送测试通知时,我没有在设备上收到实际通知。
这些方法也没有被调用:
func pushRegistry(_ registry: PKPushRegistry, didReceiveIncomingPushWith payload: PKPushPayload, for type: PKPushType) {
}
func userNotificationCenter(_ center: UNUserNotificationCenter, openSettingsFor notification: UNNotification?) {
}
func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (_ options: UNNotificationPresentationOptions) -> Void) {
}
func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
}
func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
}
func application(_ application: UIApplication, handleActionWithIdentifier identifier: String?, forRemoteNotification userInfo: [AnyHashable : Any], completionHandler: @escaping () -> Void) {
}
我做错了什么?
【问题讨论】:
-
你是否使用正确的证书(开发aps证书)来推送?
标签: ios swift apple-push-notifications