Guest User

Untitled

a guest
Jan 20th, 2018
310
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.28 KB | None | 0 0
  1. public static void SendAppleNotification()
  2. {
  3. // Configuration (NOTE: .pfx can also be used here)
  4. byte[] arr = File.ReadAllBytes("D:\MySoftware\pa_Dev.pem");
  5.  
  6. var config = new ApnsConfiguration(ApnsConfiguration.ApnsServerEnvironment.Sandbox,
  7. arr, "1234");
  8.  
  9. // Create a new broker
  10. var apnsBroker = new ApnsServiceBroker(config);
  11.  
  12. // Wire up events
  13. apnsBroker.OnNotificationFailed += (notification, aggregateEx) => {
  14.  
  15. aggregateEx.Handle(ex => {
  16.  
  17. // See what kind of exception it was to further diagnose
  18. if (ex is ApnsNotificationException)
  19. {
  20. var notificationException = (ApnsNotificationException)ex;
  21.  
  22. // Deal with the failed notification
  23. var apnsNotification = notificationException.Notification;
  24. var statusCode = notificationException.ErrorStatusCode;
  25.  
  26. Console.WriteLine($"Apple Notification Failed: ID={apnsNotification.Identifier}, Code={statusCode}");
  27.  
  28. }
  29. else
  30. {
  31. // Inner exception might hold more useful information like an ApnsConnectionException
  32. Console.WriteLine($"Apple Notification Failed for some unknown reason : {ex.InnerException}");
  33. }
  34.  
  35. // Mark it as handled
  36. return true;
  37. });
  38. };
  39.  
  40. apnsBroker.OnNotificationSucceeded += (notification) => {
  41. Console.WriteLine("Apple Notification Sent!");
  42. };
  43.  
  44. // Start the broker
  45. apnsBroker.Start();
  46.  
  47.  
  48. // Queue a notification to send
  49. apnsBroker.QueueNotification(new ApnsNotification
  50. {
  51. DeviceToken = "660E4433785EFF2B2AA29D5076B039C969F1AADD839D79261328F40B08D26497",
  52. Payload = JObject.Parse("{"aps":{"badge":7}}")
  53. });
  54.  
  55.  
  56. // Stop the broker, wait for it to finish
  57. // This isn't done after every message, but after you're
  58. // done with the broker
  59. apnsBroker.Stop();
  60.  
  61.  
  62. }
Add Comment
Please, Sign In to add comment