【问题标题】:Not receiving remote push notifications on iOS在 iOS 上未收到远程推送通知
【发布时间】: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


【解决方案1】:

你说你没有收到,所以让我们首先确保你之前提到的那些没有被调用的方法来自这里extension AppDelegate: UNUserNotificationCenterDelegate(顺便说一下,当你实现 didReceive方法确保你最后调用completionHandler()

当你这样做时:

application.registerForRemoteNotifications()

确保从主线程运行它,因为如果没有从后台指定它可以被调用并且可能会失败。你可以这样做

DispatchQueue.main.async { 
  UIApplication.shared.registerForRemoteNotifications()
}

我假设您以这种方式或类似方式获取设备令牌

func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
        // 1. Convert device token to string
        let tokenParts = deviceToken.map { data -> String in
            return String(format: "%02.2hhx", data)
        }
        let token = tokenParts.joined()
        // 2. Print device token to use for PNs payloads
        print("Device Token: \(token)")
}

最后实现这个方法,看看注册设备有没有报错

func application(_ application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: Error) {
        // 1. Print out error if PNs registration not successful
        print("Failed to register for remote notifications with error: \(error)")
 }

哦,顺便说一下(以防万一您错过了),请确保您在项目的目标中启用了推送通知。

【讨论】:

  • 是的,我已经完成了你所描述的一切。还是没有结果
  • 也许this step by step tutorial 可以帮助你发现你缺少的东西
【解决方案2】:

确保您已在项目设置功能中启用推送通知。 如何启用: 转到目标:-> 选择项目-> 转到功能-> 转到推送通知。在那里启用它。

另一件事是证书。在开发过程中,您不应使用生产证书。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-01-23
    • 1970-01-01
    • 2013-10-08
    • 2016-03-08
    • 1970-01-01
    • 2021-02-11
    相关资源
    最近更新 更多