public async Task RegisterDeviceTags(UserDevice deviceUpdate, string[] tags) { try { RegistrationDescription registration = null; switch (deviceUpdate.Platform) { case "mpns": registration = new MpnsRegistrationDescription(deviceUpdate.PushToken); break; case "wns": registration = new WindowsRegistrationDescription(deviceUpdate.PushToken); break; case "apns": registration = new AppleRegistrationDescription(deviceUpdate.PushToken); break; case "gcm": registration = new GcmRegistrationDescription(deviceUpdate.PushToken); break; default: throw new Exception("Unrecognized platform."); } registration.RegistrationId = deviceUpdate.RegistrationId; registration.Tags = new HashSet<string>(tags); // Add an extra tag for the platform, in case we want to send // to all users on that platform. registration.Tags.Add("Reop:" + deviceUpdate.Platform); try { await Hub.CreateOrUpdateRegistrationAsync(registration); } catch (MessagingException e) { ReturnGoneIfHubResponseIsGone(e); throw e; } return; } catch (Exception e) { throw e; } }
public async Task<HttpResponseMessage> Post(UserDevice device) { try { User user = this.User.User; User fullUser = _repository.Single<User>( a => a.UserId == user.UserId && a.Deleted == false , "Credential.Devices"); if (fullUser == null) // Though this will never happen as authentication found the user, but for sake of completion. return Request.CreateResponse(HttpStatusCode.InternalServerError, GetErrorResponse("Unexpectedly, could not find the user.")); Credential credential = fullUser.Credential; if (credential == null) return Request.CreateResponse(HttpStatusCode.NotAcceptable, GetErrorResponse("User credentials not found.")); if (credential.Devices == null) credential.Devices = new List<UserDevice>(); foreach (UserDevice savedDevice in credential.Devices) { // lets see if this device already exists. if (savedDevice.Udid.Equals(device.Udid)) { // Unregister the device from hub to register again with new token. _pushNotificationService.UnRegisterDevice(savedDevice.RegistrationId); savedDevice.OptIn = device.OptIn; //savedDevice.Platform = device.Platform; savedDevice.Nickname = device.Nickname; savedDevice.TimeZoneOffset = device.TimeZoneOffset; savedDevice.PushToken = device.PushToken; string newRegistrationId = await _pushNotificationService.RegisterDevicePushToken(device.PushToken); savedDevice.RegistrationId = newRegistrationId; device.DeviceId = savedDevice.DeviceId; await _pushNotificationService.RegisterDeviceTags(savedDevice, new string[] { "id:" + device.DeviceId }); _repository.Save(); // Device already exists. Just return 200 OK return Request.CreateResponse(HttpStatusCode.OK); } } // Check if this device is associated with another user. /* * We allow the same device to be registered against multiple users now. UserDevice alreadyExistingDevice = _repository.Single<UserDevice>(a => a.Udid.Equals(device.Udid)); if (alreadyExistingDevice != null) return Request.CreateResponse(HttpStatusCode.NotAcceptable, GetErrorResponse("Device already associated with another user.")); */ string registrationId = await _pushNotificationService.RegisterDevicePushToken(device.PushToken); // associate this device with the user credentials. device.RegistrationId = registrationId; credential.Devices.Add(device); // we need to associate some tag with this device. // A user might have multiple devices so tag cannot be // the user. _repository.Save(); await _pushNotificationService.RegisterDeviceTags(device, new string[] { "id:" + device.DeviceId }); return Request.CreateResponse(HttpStatusCode.OK); }catch (ParamMissingException e) { return Request.CreateResponse(HttpStatusCode.NotAcceptable, GetErrorResponse(e.Message)); } catch (AlreadyExistsException e) { return Request.CreateResponse(HttpStatusCode.Conflict, GetErrorResponse(e.Message)); } catch (InvalidValueException e) { return Request.CreateResponse(HttpStatusCode.NotAcceptable, GetErrorResponse(e.Message)); } catch (UserNotFoundException e) { return Request.CreateResponse(HttpStatusCode.NotFound, GetErrorResponse(e.Message)); } catch (Exception e) { return Request.CreateResponse(HttpStatusCode.InternalServerError, GetErrorResponse("Oops, server encountered an issue... " + e.Message)); } }