public IDictionary<string, object> Encode(IAVState state)
        {
            if (state.Alert == null && state.Data == null) {
            throw new InvalidOperationException("A push must have either an Alert or Data");
              }
              if (state.Channels == null && state.Query == null) {
            throw new InvalidOperationException("A push must have either Channels or a Query");
              }

              var data = state.Data ?? new Dictionary<string, object> { { "alert", state.Alert } };
              var query = state.Query ?? AVInstallation.Query;
              if (state.Channels != null) {
            query = query.WhereContainedIn("channels", state.Channels);
              }
              var payload = new Dictionary<string, object> {
            { "data", data },
            { "where", query.BuildParameters().GetOrDefault("where", new Dictionary<string, object>()) },
              };
              if (state.Expiration.HasValue) {
            payload["expiration_time"] = state.Expiration.Value.ToString("yyyy-MM-ddTHH:mm:ssZ");
              } else if (state.ExpirationInterval.HasValue) {
            payload["expiration_interval"] = state.ExpirationInterval.Value.TotalSeconds;
              }
              if (state.PushTime.HasValue) {
            payload["push_time"] = state.PushTime.Value.ToUniversalTime().ToString("yyyy-MM-ddTHH:mm:ssZ");
              }

              return payload;
        }
Esempio n. 2
0
 /// <summary>
 /// Creates a push which will target every device. The Data field must be set before calling SendAsync.
 /// </summary>
 public AVPush()
 {
     mutex = new object();
       // Default to everyone.
       state = new MutableAVState {
     Query = AVInstallation.Query
       };
 }
        public Task SendPushNotificationAsync(IAVState state, CancellationToken cancellationToken)
        {
            return currentUserController.GetCurrentSessionTokenAsync(cancellationToken).OnSuccess(sessionTokenTask => {
            var command = new AVCommand("push",
            method: "POST",
            sessionToken: sessionTokenTask.Result,
            data: AVPushEncoder.Instance.Encode(state));

            return commandRunner.RunCommandAsync(command, cancellationToken: cancellationToken);
              }).Unwrap();
        }
Esempio n. 4
0
        private IAVPushController GetMockedPushController(IAVState expectedPushState)
        {
            Mock<IAVPushController> mockedController = new Mock<IAVPushController>(MockBehavior.Strict);

              mockedController.Setup(
            obj => obj.SendPushNotificationAsync(
              It.Is<IAVState>(s => s.Equals(expectedPushState)),
              It.IsAny<CancellationToken>()
            )
              ).Returns(Task.FromResult(false));

              return mockedController.Object;
        }
Esempio n. 5
0
 private void MutateState(Action<MutableAVState> func)
 {
     lock (mutex) {
     state = state.MutatedClone(func);
       }
 }