예제 #1
0
		public async Task<string> RegisterWithHub(DeviceRegistration deviceUpdate)
		{
			string newRegistrationId = null;
			try
			{
				// make sure there are no existing registrations for this push handle (used for iOS and Android)
				if(deviceUpdate.Handle != null)
				{
					//Azure likes to uppercase the iOS device handles for some reason - no worries tho, I only spent 2 hours tracking this down
					if(deviceUpdate.Platform == "iOS")
						deviceUpdate.Handle = deviceUpdate.Handle.ToUpper();

					var registrations = await _hub.GetRegistrationsByChannelAsync(deviceUpdate.Handle, 100);

					foreach(var reg in registrations)
					{
						if(newRegistrationId == null)
						{
							newRegistrationId = reg.RegistrationId;
						}
						else
						{
							await _hub.DeleteRegistrationAsync(reg);
						}
					}
				}

				if(newRegistrationId == null)
					newRegistrationId = await _hub.CreateRegistrationIdAsync();

				RegistrationDescription registration = null;

				switch(deviceUpdate.Platform)
				{
					case "iOS":
						var alertTemplate = "{\"aps\":{\"alert\":\"$(message)\",\"badge\":\"#(badge)\",\"payload\":\"$(payload)\"}}";
						registration = new AppleTemplateRegistrationDescription(deviceUpdate.Handle, alertTemplate);
						break;
					case "Android":
						var messageTemplate = "{\"data\":{\"title\":\"Sport\",\"message\":\"$(message)\",\"payload\":\"$(payload)\"}}";
						registration = new GcmTemplateRegistrationDescription(deviceUpdate.Handle, messageTemplate);
						break;
					default:
						throw new HttpResponseException(HttpStatusCode.BadRequest);
				}

				registration.RegistrationId = newRegistrationId;
				registration.Tags = new HashSet<string>(deviceUpdate.Tags);
				await _hub.CreateOrUpdateRegistrationAsync(registration);
			}
			catch(Exception ex)
			{
				throw ex.GetBaseException().Message.ToException(Request);
			}
			
			return newRegistrationId;
		}
예제 #2
0
        /// <summary>
        /// This app uses Azure as the backend which utilizes Notifications hubs
        /// </summary>
        /// <returns>The athlete notification hub registration.</returns>
        public Task UpdateAthleteNotificationHubRegistration(Athlete athlete, bool forceSave = false, bool sendTestPush = false)
        {
            return(new Task(() =>
            {
                if (athlete == null)
                {
                    throw new ArgumentNullException("athlete");
                }

                if (athlete.Id == null || athlete.DeviceToken == null)
                {
                    return;
                }

                var tags = new List <string> {
                    App.CurrentAthlete.Id,
                    "All",
                };

                App.CurrentAthlete.LocalRefresh();
                App.CurrentAthlete.Memberships.Select(m => m.LeagueId).ToList().ForEach(tags.Add);
                athlete.DevicePlatform = Xamarin.Forms.Device.OS.ToString();

                var reg = new DeviceRegistration {
                    Handle = athlete.DeviceToken,
                    Platform = athlete.DevicePlatform,
                    Tags = tags.ToArray()
                };

                var registrationId = Client.InvokeApiAsync <DeviceRegistration, string>("registerWithHub", reg, HttpMethod.Put, null).Result;
                athlete.NotificationRegistrationId = registrationId;

                //Used to verify the device is successfully registered with the backend
                if (sendTestPush)
                {
                    var qs = new Dictionary <string, string>();
                    qs.Add("athleteId", athlete.Id);
                    Client.InvokeApiAsync("sendTestPushNotification", null, HttpMethod.Get, qs).Wait();
                }

                if (athlete.IsDirty || forceSave)
                {
                    var task = SaveAthlete(athlete);
                    task.Start();
                    task.Wait();
                }
            }));
        }
예제 #3
0
		/// <summary>
		/// This app uses Azure as the backend which utilizes Notifications hubs
		/// </summary>
		/// <returns>The athlete notification hub registration.</returns>
		public Task UpdateAthleteNotificationHubRegistration(Athlete athlete, bool forceSave = false, bool sendTestPush = false)
		{
			return new Task(() =>
			{
				if(athlete == null)
					throw new ArgumentNullException("athlete");

				if(athlete.Id == null || athlete.DeviceToken == null)
					return;

				var tags = new List<string> {
					App.CurrentAthlete.Id,
					"All",
				};

				App.CurrentAthlete.LocalRefresh();
				App.CurrentAthlete.Memberships.Select(m => m.LeagueId).ToList().ForEach(tags.Add);
				athlete.DevicePlatform = Xamarin.Forms.Device.OS.ToString();

				var reg = new DeviceRegistration {
					Handle = athlete.DeviceToken,
					Platform = athlete.DevicePlatform,
					Tags = tags.ToArray()
				};

				var registrationId = Client.InvokeApiAsync<DeviceRegistration, string>("registerWithHub", reg, HttpMethod.Put, null).Result;
				athlete.NotificationRegistrationId = registrationId;

				//Used to verify the device is successfully registered with the backend 
				if(sendTestPush)
				{
					var qs = new Dictionary<string, string>();
					qs.Add("athleteId", athlete.Id);
					Client.InvokeApiAsync("sendTestPushNotification", null, HttpMethod.Get, qs).Wait();
				}

				if(athlete.IsDirty || forceSave)
				{
					var task = SaveAthlete(athlete);
					task.Start();
					task.Wait();
				}
			});
		}