[英]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.
}
}
I suggest resetting your devises permission for your App.我建议重置您的应用程序的设计权限。 Follow these steps:
按着这些次序:
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.