Forum Xamarin.Forms

How to write audiences in JSON formatted for App Center push notification?

ocir21ocir21 Member ✭✭

{
"notification_content": {
"name": "Cyclists and Dog Lovers",
"title": "Crossover Event This Weekend",
"body": "Do your dogs like to ride bikes? Come to our crossover event this weekend.",
"custom_data": {"eventDate": "06/01/2020", "eventTime": "08:00 AM"}
},
"notification_target": {
"type": "audiences_target",
"audiences": ["dog-lovers", "cyclists"]
}
}

Where/how can I define "dog-lovers" or "cyclists"?

source: https://docs.microsoft.com/en-us/appcenter/push/rest-api#send-a-notification-to-an-audience

Answers

  • YelinzhYelinzh Member, Xamarin Team Xamurai
    edited July 2019

    Do you mean parse the json data to an object class? You can use this tool.

    namespace QuickType
    {
        using System;
        using System.Collections.Generic;
    
        using System.Globalization;
        using Newtonsoft.Json;
        using Newtonsoft.Json.Converters;
    
        public partial class Welcome
        {
            [JsonProperty("notification_content")]
            public NotificationContent NotificationContent { get; set; }
    
            [JsonProperty("notification_target")]
            public NotificationTarget NotificationTarget { get; set; }
        }
    
        public partial class NotificationContent
        {
            [JsonProperty("name")]
            public string Name { get; set; }
    
            [JsonProperty("title")]
            public string Title { get; set; }
    
            [JsonProperty("body")]
            public string Body { get; set; }
    
            [JsonProperty("custom_data")]
            public CustomData CustomData { get; set; }
        }
    
        public partial class CustomData
        {
            [JsonProperty("eventDate")]
            public string EventDate { get; set; }
    
            [JsonProperty("eventTime")]
            public string EventTime { get; set; }
        }
    
        public partial class NotificationTarget
        {
            [JsonProperty("type")]
            public string Type { get; set; }
    
            [JsonProperty("audiences")]
            public string[] Audiences { get; set; }
        }
    
        public partial class Welcome
        {
            public static Welcome FromJson(string json) => JsonConvert.DeserializeObject<Welcome>(json, QuickType.Converter.Settings);
        }
    
        public static class Serialize
        {
            public static string ToJson(this Welcome self) => JsonConvert.SerializeObject(self, QuickType.Converter.Settings);
        }
    
        internal static class Converter
        {
            public static readonly JsonSerializerSettings Settings = new JsonSerializerSettings
            {
                MetadataPropertyHandling = MetadataPropertyHandling.Ignore,
                DateParseHandling = DateParseHandling.None,
                Converters =
                {
                    new IsoDateTimeConverter { DateTimeStyles = DateTimeStyles.AssumeUniversal }
                },
            };
        }
    }
    
Sign In or Register to comment.