Yeremia Danang
Yeremia Danang

Reputation: 139

How to trigger App Center Push from my asp.net Web API?

I'm making an android Xamarin.Android. I finished the android application and now I want to add remote push notifications based on my item condition in my database that can accessed from ASP.Net Web Api.

I succeeded sent notifications from App Center Push to my application. I already authorized the App Center Client and now can access the app center api. I'm planning to merge the app center api to my asp.net web api if possible. But I don't know where to start it.

Should I put the app center action to my controller (I don't know if its working or not) or there's another way? here's my controller: public class InventoriesController : ApiController { private InventoryRepository _inventoryRepository;

    public InventoriesController()
    {
        _inventoryRepository = new InventoryRepository();
    }

    // GET: api/Categories
    public IHttpActionResult GetInventories()
    {
        IEnumerable<InventoryViewModel> inv = _inventoryRepository.GetAll().ToList().Select(e=> new InventoryViewModel(e)).ToList();
        return Ok(inv);
    }

    // GET: api/Categories/5
    [ResponseType(typeof(InventoryViewModel))]
    public IHttpActionResult GetInventory(Guid id)
    {
        InventoryViewModel inventory = new InventoryViewModel (_inventoryRepository.GetSingle(e => e.Id == id));
        if (inventory == null)
        {
            return NotFound();
        }

        return Ok(inventory);
    }

    // PUT: api/Categories/5
    [ResponseType(typeof(void))]
    public IHttpActionResult PutInventory(Guid id, InventoryViewModel inventory)
    {
        if (!ModelState.IsValid)
        {
            return BadRequest(ModelState);
        }

        if (id != inventory.Id)
        {
            return BadRequest();
        }

        try
        {
            _inventoryRepository.Edit(inventory.ToModel());

        }
        catch (DbUpdateConcurrencyException)
        {
            if (!InventoryExist(id))
            {
                return NotFound();
            }
            else
            {
                throw;
            }
        }

        return StatusCode(HttpStatusCode.NoContent);
    }

    // POST: api/Categories
    [ResponseType(typeof(InventoryViewModel))]
    public IHttpActionResult PostInventory(InventoryViewModel inventory)
    {
        if (!ModelState.IsValid)
        {
            return BadRequest(ModelState);
        }

        try
        {
            _inventoryRepository.Add(inventory.ToModel());
        }
        catch (DbUpdateException)
        {
            if (InventoryExist(inventory.Id))
            {
                return Conflict();
            }
            else
            {
                throw;
            }
        }

        return CreatedAtRoute("DefaultApi", new { id = inventory.Id }, inventory);
    }

    // DELETE: api/Categories/5
    [ResponseType(typeof(Inventory))]
    public async Task<IHttpActionResult> DeleteInventory(Guid id)
    {
        Inventory inventory = _inventoryRepository.GetSingle(e => e.Id == id);
        if (inventory == null)
        {
            return NotFound();
        }

        await _inventoryRepository.DeleteAsync(inventory);


        return Ok(inventory);
    }
   private bool InventoryExist(Guid id)
    {
        IQueryable<Inventory> inv = _inventoryRepository.GetAll();
        return inv.Count(e => e.Id == id) > 0;
    }

And this is my model:

    public class InventoryViewModel
    {
        public Guid Id { get; set; }
        public int Quantity { get; set; }
        public DateTime ExpirationDate { get; set; }
        public bool IsDeleted { get; set; }
        public bool IsConsumed { get; set; }
        public decimal Price { get; set; }
        public string ItemName { get; set; }
        public Guid ProductId { get; set; }
        public Guid StorageId { get; set; }
        public string AddedUserId { get; set; }

        public Inventory ToModel()
        {
            return new Inventory
            {
                Id = (Id == Guid.Empty) ? Guid.NewGuid() : Id,
                ExpirationDate = ExpirationDate,
                Price = Price,
                ProductId=ProductId,
                StorageId=StorageId,
                ItemName=ItemName,
                IsDeleted=IsDeleted,
                IsConsumed=IsConsumed,
                AddedUserId = AddedUserId,
            };
        }
        public InventoryViewModel()
        {

        }

        public InventoryViewModel(Inventory i)
        {
            this.Id = i.Id;
            this.ExpirationDate = i.ExpirationDate;
            this.Price = i.Price;
            this.ProductId = i.ProductId;
            this.StorageId = i.StorageId;
            this.ItemName = i.ItemName;
            this.AddedUserId = i.AddedUserId;
        }
    }

I want to make the app center send notification based on Expired Date on my Inventories model and AddedUserId. So its my web self made web api who send the notification to my apps. I read this documentation: [https://learn.microsoft.com/en-us/appcenter/push/pushapi][1] but still don't know where I have to write in my Web Api.

Hope someone here can help me. Thanks in advance :)

Upvotes: 0

Views: 963

Answers (1)

Jeevan
Jeevan

Reputation: 538

You can find detail here in appcenter REST API documentation https://learn.microsoft.com/en-us/appcenter/push/rest-api

Appcenter REST API swagger: https://openapi.appcenter.ms/

Upvotes: 1

Related Questions