解析 - 在应用程序安装上调用云代码

时间:2014-11-13 18:50:34

标签: ios parse-platform push-notification cloud

我为Parse数据库创建了一些云代码,我希望在用户安装应用程序时调用它。我已经实现了Push,所以当用户注册推送通知时,我希望它被触发。我也希望能够通过" deviceToken"进入我的云功能。

这是我到目前为止的功能:

Parse.Cloud.define("newListing", function(request, response) {
                   var ListingClass = Parse.Object.extend("Listings");
                   var listing = new ListingClass();

                   listing.set("Name","--");
                   listing.set("DeviceID",request.params.param_DeviceID);

                   listing.save(null,{
                               success:function(listing) {
                               response.success(listing);
                               },
                               error:function(error) {
                               response.error(error);
                               }
                               });
});

要点:

  • 我想调用云功能" newListing"当用户注册推送通知

有什么想法吗?谢谢!

2 个答案:

答案 0 :(得分:1)

只有在AppDelegate中调用didRegisterForRemoteNotificationsWithDeviceToken方法时才会获得deviceToken,因此请将云代码调用函数放在那里。

Swift代码:

func application(application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: NSData) {
  // Do your normal token handling to set the device token in the Installation and save to Parse.com
  let currentInstallation = PFInstallation.currentInstallation()
  currentInstallation.setDeviceTokenFromData(deviceToken)
  currentInstallation.saveInBackground()
  // Call your cloud code function
  let deviceTokenAsString = .... // You need to implement this code
  PFCloud.callFunctionInBackground("newListing", withParameters: ["param_DeviceID": deviceTokenAsString]) { results, error in
    // Error handling, and any other functionality you need when your cloud function is complete
  }
}

答案 1 :(得分:0)

以下是我提出的解决方案,并不需要修改应用。在新安装发生后调用此云函数。

Parse.Cloud.afterSave(Parse.Installation, function(request, response) {
                      var newID = request.object.get("deviceToken");

                      var query = new Parse.Query("Listings");
                      query.equalTo("DeviceID", newID);
                      query.find({
                         success: function(results) {
                             if(results.length > 0){
                                 response.success("ID already exists");
                             }else{
                                 var ListingClass = Parse.Object.extend("Listings");
                                 var listing = new ListingClass();

                                 listing.set("Name","--");
                                 listing.set("DeviceID",newID);

                                 listing.save(null,{
                                              success:function(listing) {
                                              response.success(listing);
                                              },
                                              error:function(error) {
                                              response.error(error);
                                              }
                                              });
                             }
                         },
                         error: function() {
                             response.error("ID fail");
                         }
                     });
});