You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

64 lines
2.2 KiB

1 year ago
  1. using FirebaseAdmin;
  2. using FirebaseAdmin.Messaging;
  3. using Google.Apis.Auth.OAuth2;
  4. using Microsoft.AspNetCore.Mvc;
  5. namespace firebase.net7API.Controllers
  6. {
  7. [ApiController]
  8. [Route("[controller]")]
  9. public class WeatherForecastController : ControllerBase
  10. {
  11. private static readonly string[] Summaries = new[]
  12. {
  13. "Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching"
  14. };
  15. private readonly ILogger<WeatherForecastController> _logger;
  16. public WeatherForecastController(ILogger<WeatherForecastController> logger)
  17. {
  18. _logger = logger;
  19. }
  20. [HttpGet(Name = "GetWeatherForecast")]
  21. public IEnumerable<WeatherForecast> Get()
  22. {
  23. return Enumerable.Range(1, 5).Select(index => new WeatherForecast
  24. {
  25. Date = DateOnly.FromDateTime(DateTime.Now.AddDays(index)),
  26. TemperatureC = Random.Shared.Next(-20, 55),
  27. Summary = Summaries[Random.Shared.Next(Summaries.Length)]
  28. })
  29. .ToArray();
  30. }
  31. [HttpPost(Name = "Post Notification")]
  32. public async Task PushNotifications()
  33. {
  34. try
  35. {
  36. //var testApp = new AppOptions
  37. //{
  38. // Credential = GoogleCredential.FromFile(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "push-notification-api-c8613-firebase-adminsdk-pv5a8-1e337ca32a.json"))
  39. //};
  40. //FirebaseApp.Create(testApp);
  41. var message = new Message()
  42. {
  43. Notification = new Notification
  44. {
  45. Title = "Hello",
  46. Body = "03/11/23",
  47. },
  48. Token = "c9jYkaOeR0ypxJXqIuTfBr:APA91bEWUoncjxEyqMN2p5onHBRzF6N5XXrE48cu2TeGvhUFrNLdy6L6Yr72di4fXcc1HUINOk9HOs8wclnXLklNDQRpoPJ6l7GIdxJ87ym4BECXRyMfWhNw2VgxzCVImis4MEti7Enw",
  49. };
  50. var response = await FirebaseMessaging.DefaultInstance.SendAsync(message);
  51. }
  52. catch (Exception ex)
  53. {
  54. throw;
  55. }
  56. }
  57. }
  58. }