我是 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.
}
}
最佳答案
我建议重置您的应用程序的设备权限。请按照下列步骤操作:
- 从您的设备中删除该应用
- 转至“设置”应用 -> 常规 -> 日期和时间 -> 将时间设置为提前一天
- 重新启动您的设备
- 重新安装您的应用
您对该应用程序的权限现在应该全部重置,理论上,当打开应用程序时,您应该收到有关您需要请求的权限的警报,并且在授予这些权限后,您的代码应该通过注册远程通知的逻辑运行。
关于使用 Parse 平台的 iOS 推送通知 - didRegisterForRemoteNotificationsWithDeviceToken - 从未调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59780453/