在Moon APNS中,GetFeedBack方法中获取有效令牌的逻辑是什么?

时间:2012-05-08 08:30:24

标签: apple-push-notifications apns-sharp

我一直试图使用MoonAPNS库为我的Apple应用程序找出有效令牌列表。 使用GetFeedBack()时,收到的令牌数量在几分钟后就会大幅变化。 在第一次尝试它是大约8000。 然后它是0 第三次尝试是1 &安培;再次是0。

我使用的是有效的生产证书。 &安培;已成功将使用相同证书的通知推送到采样设备。

我不明白代码的逻辑和&在什么基础上提供收到的令牌。 从Moon APNS下载的代码如下。

public List<Feedback> GetFeedBack()
{
  try
  {
    var feedbacks = new List<Feedback>();
    Logger.Info("Connecting to feedback service.");

    if (!_conected)
        Connect(_feedbackHost, FeedbackPort, _certificates);

    if (_conected)
    {
        //Set up
        byte[] buffer = new byte[38];
        int recd = 0;
        DateTime minTimestamp = DateTime.Now.AddYears(-1);

        //Get the first feedback
        recd = _apnsStream.Read(buffer, 0, buffer.Length);
        Logger.Info("Feedback response received.");

        if (recd == 0)
            Logger.Info("Feedback response is empty.");

        //Continue while we have results and are not disposing
        while (recd > 0)
        {
            Logger.Info("processing feedback response");
            var fb = new Feedback();

            //Get our seconds since 1970 ?
            byte[] bSeconds = new byte[4];
            byte[] bDeviceToken = new byte[32];

            Array.Copy(buffer, 0, bSeconds, 0, 4);

            //Check endianness
            if (BitConverter.IsLittleEndian)
                Array.Reverse(bSeconds);

            int tSeconds = BitConverter.ToInt32(bSeconds, 0);

            //Add seconds since 1970 to that date, in UTC and then get it locally
            fb.Timestamp = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc).AddSeconds(tSeconds).ToLocalTime();


            //Now copy out the device token
            Array.Copy(buffer, 6, bDeviceToken, 0, 32);

            fb.DeviceToken = BitConverter.ToString(bDeviceToken).Replace("-", "").ToLower().Trim();

            //Make sure we have a good feedback tuple
            if (fb.DeviceToken.Length == 64 && fb.Timestamp > minTimestamp)
            {
                //Raise event
                //this.Feedback(this, fb);
                feedbacks.Add(fb);
            }

            //Clear our array to reuse it
            Array.Clear(buffer, 0, buffer.Length);

            //Read the next feedback
            recd = _apnsStream.Read(buffer, 0, buffer.Length);
        }
        //clode the connection here !
        Disconnect();
        if (feedbacks.Count > 0)
            Logger.Info("Total {0} feedbacks received.", feedbacks.Count);
        return feedbacks;
    }
  }
  catch (Exception ex)
  {
    Logger.Error("Error occurred on receiving feed back. - " + ex.Message);
    return null;
  }
  return null;
}

}

1 个答案:

答案 0 :(得分:1)

就我个人而言,我没有使用Moon APNS,但APNS反馈服务的工作方式是每次调用它时,它会在您上次调用反馈服务后返回非活动令牌。这解释了你在这里看到的模式。