Parse Push Notification Not Firing On Time

2019-09-08 09:11发布

问题:

In my application the user creates an alarm which uploads an object to parse as well as schedules a push notification for the time that they select. I had it working yesterday but for some reason today, right after the user creates the alarm the notification is triggered. I can't figure it out, I don't remember changing anything.

Here is my code to create the notification:

    PFUser *user = [PFUser currentUser];
    PFObject *alarm = [PFObject objectWithClassName:@"Alarm"];
    alarm[@"Active"] = @YES;
    alarm[@"Bounty"] = IntNumber;
    alarm[@"ActionComplete"] = [NSNumber numberWithInt:0];;
    alarm[@"Time"] = _alarmTime;
    alarm[@"User"] = [PFUser currentUser];

    NSLog(@"%@",_alarmTime);
    NSString *dateString = [NSString stringWithFormat:@"%f",[_alarmTime timeIntervalSince1970] * 1000];

    NSString *clientId = [[PFUser currentUser] objectId];
    NSLog(@"%@",dateString);
    alarm[@"aString"] = dateString;

    [alarm save];
    NSString *objectID = [alarm objectId];

    [PFCloud callFunctionInBackground:@"sendSilentPush"
                       withParameters:@{
                                        @"clientId":clientId,
                                        @"alarmTime":dateString,
                                        @"alarmTimeDate":_alarmTime,
                                        }
                                block:^(id object, NSError *error) {

                                }];

And this is my cloud code:

Parse.Cloud.define("sendSilentPush", function(request,response){
 //Get user Id
 var recepeintId = request.params.clientId;
 var alarmTime = request.params.alarmTime;
 var alarmTimeDate = request.params.alarmTimeDate;
 //Get User hook using the ID using a query on user table
 var userQuery = new Parse.Query('_User');

userQuery.get(recepeintId, {
  success: function(user) {
    // object is an instance of Parse.Object.

 var pushQuery = new Parse.Query(Parse.Installation);
  pushQuery.equalTo('deviceType', 'ios');


//Send a push to the user
Parse.Push.send({
    where: pushQuery,
    "data" : { "content-available": 1, 
    "sound": "", 
    "extra": { "Time": alarmTime } 
    }
    }).then(function() {
      response.success("Push was sent successfully.")
  }, function(error) {
      response.error("Push failed to send with error: " + error.message);
  });

  },

  error: function(user, error) {
    // error is an instance of Parse.Error.
  }
});




});

回答1:

Jack I can't duplicate your error when building from ground up. There are a couple things you might be overlooking that i've outlined below. If your dead set on using Cloud Code the correct way to send a scheduled push is below. But first lets discuss since I can't reproduce your error, brainstorming might prevail in this case.

var query = new Parse.Query(Parse.Installation);
query.equalTo('deviceType', 'ios');

Parse.Push.send({
where: query,
"data" : { "content-available": 1, 
"sound": "",
"extra": {"Time": alarmTime} //confused what your trying to do here. 
}
//remaining code

extra is not a supported field for the data dictionary. Unless you created your own but I don't see that anywhere else in your code. And I don't know why would have to create a new dictionary for it anyways? Additionally if you did want to create a new field the data is only presented once the user has opened the app after tapping on the notification.

CORRECT SYNTAX WITH CLOUD CODE

var query = new Parse.Query(Parse.Installation);
query.equalTo('deviceType', 'ios');

Parse.Push.send({
where: query,
data : { 
alert: "Push from the Future!",
badge: "Increment",
sound: "",
}
push_time: new Date(2015, 01, 01) // Can't be no more than two weeks from today see notes below
}, {
success: function() {
//Success
},
error: function(error) {
//Oops
}
});

However, I would urge you to not use Cloud Code unless you absolutely have to. There are other ways of firing local push notifications, specifically Apples way. It's user friendly and easy to incorporate for tasks like this. Another reason, is because for every query you execute it counts against your API Requests as 1 request. This could easily multiply if you plan on expanding your app or growing the audience in the future. Which goes back to the first reason, send a local push notification from the device.

There is also a couple things to consider when scheduling the way you want through cloud code.

  1. The scheduled time cannot be in the past, and can be up to two weeks in the future
  2. It can be an ISO 8601 date (which is basically the internationally accepted way to represent YYYY-MM-DD) with a date, time, and timezone, as in the example above, or it can be a numeric value representing a UNIX epoch time in seconds (UTC)
  3. Ensure you have the latest version of Parse installed