简体   繁体   English

带有解析平台的 iOS 推送通知 - didRegisterForRemoteNotificationsWithDeviceToken - 从未调用

[英]iOS Push Notifications with Parse Platform - didRegisterForRemoteNotificationsWithDeviceToken - never called

I'm new to iOS development.我是 iOS 开发的新手。 I'm having trouble getting push notifications to work.我无法让推送通知正常工作。 I'm following this guide: https://www.back4app.com/docs/ios/push-notifications/ios-send-push-notification-from-server我正在遵循本指南: https : //www.back4app.com/docs/ios/push-notifications/ios-send-push-notification-from-server

The behavior is that, in the simulator the callback didFailToRegisterForRemoteNotificationsWithError does happen (as expected).行为是,在模拟器中,回调 didFailToRegisterForRemoteNotificationsWithError 确实发生了(如预期的那样)。

When I run on the phone, neither didRegisterForRemoteNotificationsWithDeviceToken or didFailToRegisterForRemoteNotificationsWithError are ever called.当我在电话上运行时, didRegisterForRemoteNotificationsWithDeviceToken 或 didFailToRegisterForRemoteNotificationsWithError 都没有被调用。 I've tried installing and uninstalling, moving code around etc. etc. etc.我试过安装和卸载,移动代码等等等等。

Here's my code in AppDelegate.swift这是我在 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 Apple 开发者证书截图

I suggest resetting your devises permission for your App.我建议重置您的应用程序的设计权限。 Follow these steps:按着这些次序:

  1. Delete the App from your device从您的设备中删除应用程序
  2. Go to the Settings App -> General -> Date and Time -> Set the time to be one day ahead转到设置应用程序 -> 常规 -> 日期和时间 -> 将时间设置为提前一天
  3. Restart your device重新启动您的设备
  4. Reinstall your App重新安装您的应用程序

Your permissions for that App should all be reset now, in theory when opening the App you should get alerts for the permissions you need to request and upon granting those your code should run through the logic of registering for remote notifications.您对该应用程序的权限现在应该全部重置,理论上在打开应用程序时,您应该收到有关您需要请求的权限的警报,并且在授予这些权限后,您的代码应该通过注册远程通知的逻辑运行。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 didRegisterForRemoteNotificationsWithDeviceToken仅在用户允许推送通知时调用 - didRegisterForRemoteNotificationsWithDeviceToken only called if user allows push notifications iOS 13.x didRegisterForRemoteNotificationsWithDeviceToken 从未被调用 - iOS 13.x didRegisterForRemoteNotificationsWithDeviceToken is never called didRegisterForRemoteNotificationsWithDeviceToken - 推送通知 - didRegisterForRemoteNotificationsWithDeviceToken - Push Notifications 从未调用过“didRegisterForRemoteNotificationsWithDeviceToken” - 'didRegisterForRemoteNotificationsWithDeviceToken' never called iOS推送通知-didReceiveRemoteNotification:fetchCompletionHandler:从不在前台调用 - iOS push notifications - didReceiveRemoteNotification:fetchCompletionHandler: never called in foreground iOS推送通知-application:didRegisterForRemoteNotificationsWithDeviceToken:是否在主线程中执行? - iOS push notifications - does application:didRegisterForRemoteNotificationsWithDeviceToken: execute in main thread? 在ios 9中没有调用didRegisterForRemoteNotificationsWithDeviceToken - didRegisterForRemoteNotificationsWithDeviceToken is not called up in ios 9 iOS 11 didRegisterForRemoteNotificationsWithDeviceToken未调用 - IOS 11 didRegisterForRemoteNotificationsWithDeviceToken not called 在iOS7上没有调用didRegisterForRemoteNotificationsWithDeviceToken - didRegisterForRemoteNotificationsWithDeviceToken not called on iOS7 使用Parse在iOS中推送通知 - Push notifications in iOS with Parse
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM