【问题标题】:Why is the function in the appdelegate not being called?为什么不调用 appdelegate 中的函数?
【发布时间】:2015-12-17 18:46:19
【问题描述】:

我正在尝试将当前用户添加到安装类,但由于某种原因该函数没有被调用。我究竟做错了什么?我该如何解决?下面是finishlaunchingwithoptions 和didregisterfornotifications。

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
    // Override point for customization after application launch.



    Parse.enableLocalDatastore()

    // Initialize Parse.
    Parse.setApplicationId("***********************",
        clientKey: "****************************")




    let settings = UIUserNotificationSettings(forTypes: [.Alert, .Sound, .Badge], categories: nil)
    UIApplication.sharedApplication().registerUserNotificationSettings(settings)
    UIApplication.sharedApplication().registerForRemoteNotifications()

    if let launchOptions = launchOptions as? [String : AnyObject] {
        if let notificationDictionary = launchOptions[UIApplicationLaunchOptionsRemoteNotificationKey] as? [NSObject : AnyObject] {
            self.application(application, didReceiveRemoteNotification: notificationDictionary)
        }
    }

    return true
}

func application(application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: NSData) {
    // Store the deviceToken in the current Installation and save it to Parse
    let installation = PFInstallation.currentInstallation()
    installation.setDeviceTokenFromData(deviceToken)
    installation["user"] = PFUser.currentUser()
    installation.saveInBackgroundWithBlock({ (success: Bool, error: NSError?) -> Void in
        if (error == nil){
            print("saved installation")
        }else{
            print(error)
        }
    })


}
func application(application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: NSError!) {
    print("Couldn't register: \(error)")
}

【问题讨论】:

  • 您注册远程通知了吗? :)
  • 我该怎么做?在我的应用下的设置中,通知已启用
  • 你需要打电话给UIApplication.sharedApplication().registerForRemoteNotifications(),也可以打电话给UIApplication.sharedApplication().registerUserNotificationSettings()
  • @TwoStraws 我更新了我的代码以包含didFinishLaunchingWithOptions
  • 谢谢。为免生疑问,您是否也请将didFailToRegisterForRemoteNotificationsWithError 添加到您的应用程序委托中? iOS 可能会向您发送一条您没有看到的错误消息。

标签: ios swift parse-platform


【解决方案1】:

这种情况下的问题是你在 iOS 模拟器中运行,它不支持远程通知。因此,您的 didRegisterForRemoteNotificationsWithDeviceToken 将永远不会被调用,因为您在模拟器中。在设备上试一试,我怀疑它会第一次工作。

对于发现此问题并遇到类似问题的未来读者,诊断问题的最佳方法是将其添加到您的应用委托中:

func application(application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: NSError) {
    print("Failed to register for remote notifications: \(error.localizedDescription)");
}

【讨论】:

  • 这就是说的,但是为什么推送的次数是0? puu.sh/lZfnU/ac14ed9829.png
  • 这是一个单独的问题;您需要询问在该网络服务方面有经验的人。
【解决方案2】:

我建议在 Installation 类上使用 cloud code beforeSave trigger 来完成此操作。与尝试在客户端处理它相比,它非常简单且更强大。以下是完成工作所需的全部内容:

// Make sure all installations point to the current user
Parse.Cloud.beforeSave(Parse.Installation, function(request, response) {

    Parse.Cloud.useMasterKey();
    if (request.user) {
        request.object.set('user', request.user);
    } else {
        request.object.unset('user');
    }
    response.success();
});

【讨论】:

  • 虽然它肯定不能回答问题,但这是做你想做的事情的更好方法,@stackerleet
猜你喜欢
  • 1970-01-01
  • 2020-10-16
  • 1970-01-01
  • 2016-12-03
  • 2019-09-02
  • 2017-05-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多