解析推送通知未按时触发 [英] Parse Push Notification Not Firing On Time

查看:99
本文介绍了解析推送通知未按时触发的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用程序中,用户创建一个警报,该警报上载要解析的对象,并为他们选择的时间安排推送通知.我昨天让它工作了,但是由于某种原因,今天,在用户创建警报后立即触发了通知.我无法弄清楚,我不记得更改任何内容.

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) {

                                }];

这是我的云代码:

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.
  }
});




});

推荐答案

杰克(Jack)从头开始构建时,我无法复制您的错误.我在下面概述了您可能忽略的几件事.如果您无法使用Cloud Code,则发送预定推送的正确方法如下.但是首先让我们讨论一下,因为我无法重现您的错误,在这种情况下,可能需要集思广益.

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 字段.除非您创建了自己的代码,但是我在代码的其他地方看不到该代码.而且我不知道为什么无论如何都要为它创建一个新的字典?此外,如果您确实想创建一个新字段,则仅在用户点击通知后打开应用程序后,才会显示数据.

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.

带有云代码的正确语法

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
}
});

但是,我强烈建议您除非绝对必要,否则不要使用Cloud Code.还有其他触发本地推送通知的方法,特别是Apple的方法.它对用户友好,易于集成到此类任务中.另一个原因是,对于您执行的每个查询,它都将您的API请求视为1个请求.如果您打算将来扩展应用程序或扩大受众群体,则数量很容易成倍增加.回到第一个原因,从设备发送本地推送通知.

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. 排定的时间不能是过去的时间,最多可以是两周后的时间
  2. 可以是带有日期,时间和时区的ISO 8601日期(基本上是国际上公认的表示YYYY-MM-DD的方式),如上例所示,也可以是表示UNIX纪元时间,以秒为单位(UTC)
  3. 确保您已安装最新版本的Parse

这篇关于解析推送通知未按时触发的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆