static JsonObject JsonObj(PushNotification notification) { var jsonObj = new JsonObject(); jsonObj["aps"] = JsonObject(notification.Payload); if (notification.DeviceTokens != null) { jsonObj["device_tokens"] = notification.DeviceTokens.ToJsonArray(); } if (notification.Aliases != null) { jsonObj["aliases"] = notification.Aliases.ToJsonArray(); } if (notification.Tags != null) { jsonObj["tags"] = notification.Tags.ToJsonArray(); } if (notification.ExcludeTokens != null) { jsonObj["exclude_tokens"] = notification.ExcludeTokens.ToJsonArray(); } if (notification.CustomData != null) { foreach (var pair in notification.CustomData) { ValidateKey(pair.Key); jsonObj[pair.Key] = pair.Value; } } return jsonObj; }
private void PushToCustomer(long CustomerID, string Text, long ChallengeID=0) { IPushServiceTokenRepository tokenRepo = RepoFactory.GetPushServiceTokenRepo(); AddRegistrationService reg = new AddRegistrationService { RequestBuilder = GetIOSUAProductionCredentials() }; PushService service = new PushService { RequestBuilder = GetIOSUAProductionCredentials() }; List<string> pushTokens = new List<string>(); foreach (PushServiceToken t in tokenRepo.TokensForCustomer(CustomerID)) { reg.Execute(t.Token, new Registration()); pushTokens.Add(t.Token); Trace.WriteLine("PUSH: Registering device token "+t.Token+" for customer "+CustomerID); } if (pushTokens.Count == 0) return; PushPayload payload = new PushPayload(); payload.Alert = Text; payload.Badge = "0"; Trace.WriteLine("PUSH: Pushing \"" + Text + "\" to " + pushTokens.Count.ToString()+" clients - "+pushTokens.ToString()); Dictionary<string, string> customData=null; if (ChallengeID > 0) { customData = new Dictionary<string, string>(); customData.Add("dareid", ChallengeID.ToString()); } try { PushNotification notification = new PushNotification { DeviceTokens = pushTokens, Payload = payload, CustomData=customData }; service.Execute(notification); } catch (Exception e) { Trace.WriteLine("PUSH: Exception encountered, " + e.ToString()); } }