Parse Push Notification Not Firing On Time

安稳与你 提交于 2019-12-25 04:52: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


来源:https://stackoverflow.com/questions/27608459/parse-push-notification-not-firing-on-time

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!