使用 Parse 平台的 iOS 推送通知 - didRegisterForRemoteNotificationsWithDeviceToken - 从未调用

标签 ios swift apple-push-notifications back4app

我是 iOS 开发新手。我无法让推送通知正常工作。我正在遵循本指南:https://www.back4app.com/docs/ios/push-notifications/ios-send-push-notification-from-server

行为是,在模拟器中回调 didFailToRegisterForRemoteNotificationsWithError 确实发生(如预期)。

当我在手机上运行时,没有调用 didRegisterForRemoteNotificationsWithDeviceToken 或 didFailToRegisterForRemoteNotificationsWithError 。我尝试过安装和卸载、移动代码等等。

这是我在 AppDelegate.swift 中的代码

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate, UNUserNotificationCenterDelegate {

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

    let userNotificationCenter = UNUserNotificationCenter.current()
    userNotificationCenter.delegate = self

    UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .sound, .badge]) { granted, error in
        if let error = error {
            print("D'oh: \(error.localizedDescription)")
        } else {
            let configuration = ParseClientConfiguration {
                               $0.applicationId = "actual"
                               $0.clientKey = "actual"
                               $0.server = "https://parseapi.back4app.com"
                               $0.isLocalDatastoreEnabled = true
                           }
            Parse.initialize(with: configuration)

            DispatchQueue.main.async {
                application.registerForRemoteNotifications()
            }

        }
    }

    IQKeyboardManager.shared.enable = true

    if let user = UserDefaults.standard.value(forKey: "user"),let pwd = UserDefaults.standard.value(forKey: "pwd"){
        PFUser.logInWithUsername(inBackground: user as! String, password: pwd as! String) { (user, error) in
//                print(user)
        }
    }


    return true
}

func application(_ application: UIApplication,
                 didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
    print("Registered.")
    createInstallationOnParse(deviceTokenData: deviceToken)
}

func application(_ application: UIApplication,
                 didFailToRegisterForRemoteNotificationsWithError error: Error) {
    print("Failed to register: \(error)")
}

func createInstallationOnParse(deviceTokenData:Data){
    if let installation = PFInstallation.current(){
        installation.setDeviceTokenFrom(deviceTokenData)
        installation.saveInBackground {
            (success: Bool, error: Error?) in
            if (success) {
                print("You have successfully saved your push installation to Back4App!")
            } else {
                if let myError = error{
                    print("Error saving parse installation \(myError.localizedDescription)")
                }else{
                    print("Uknown error")
                }
            }
        }
    }
}

// MARK: UISceneSession Lifecycle

func application(_ application: UIApplication, configurationForConnecting connectingSceneSession: UISceneSession, options: UIScene.ConnectionOptions) -> UISceneConfiguration {
    // Called when a new scene session is being created.
    // Use this method to select a configuration to create the new scene with.
    return UISceneConfiguration(name: "Default Configuration", sessionRole: connectingSceneSession.role)
}

func application(_ application: UIApplication, didDiscardSceneSessions sceneSessions: Set<UISceneSession>) {
    // Called when the user discards a scene session.
    // If any sessions were discarded while the application was not running, this will be called shortly after application:didFinishLaunchingWithOptions.
    // Use this method to release any resources that were specific to the discarded scenes, as they will not return.
}


}

Signing & Capabilities Screenshot

Apple Developer Certs Screenshot

最佳答案

我建议重置您的应用程序的设备权限。请按照下列步骤操作:

  1. 从您的设备中删除该应用
  2. 转至“设置”应用 -> 常规 -> 日期和时间 -> 将时间设置为提前一天
  3. 重新启动您的设备
  4. 重新安装您的应用

您对该应用程序的权限现在应该全部重置,理论上,当打开应用程序时,您应该收到有关您需要请求的权限的警报,并且在授予这些权限后,您的代码应该通过注册远程通知的逻辑运行。

关于使用 Parse 平台的 iOS 推送通知 - didRegisterForRemoteNotificationsWithDeviceToken - 从未调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59780453/

相关文章:

iphone - iOS 核心动画 : CALayer bringSublayerToFront?

ios - 控制台数据问题

swift - 全局控制按钮外观

ios - Apple 推送通知收到错误身份验证失败,因为远程方已关闭传输流

ios - 使用 QuickTime 从 iPhone 捕获屏幕会禁用设备上的声音

ios - NSMutableArray 线程并发与 GCD

objective-c - 如何检测CGAffineTransform是否翻转?

ios - 带有新 ios-charts 框架新版本的条形图

ios - 带有 Node JS 的 APNS(Apple 推送通知服务)

ios - 推送通知不适用于 testflight 和 xcode 8 beta