PageRenderTime 58ms CodeModel.GetById 35ms RepoModel.GetById 1ms app.codeStats 0ms

/PushSharp.Sample/Program.cs

https://github.com/mustafagenc/PushSharp
C# | 137 lines | 88 code | 22 blank | 27 comment | 0 complexity | e3706253a340ae40de833703046c1f04 MD5 | raw file
Possible License(s): Apache-2.0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Collections.Specialized;
  4. using System.IO;
  5. using System.Linq;
  6. using System.Text;
  7. using PushSharp;
  8. using PushSharp.Apple;
  9. using PushSharp.Android;
  10. using PushSharp.WindowsPhone;
  11. using PushSharp.Windows;
  12. namespace PushSharp.Sample
  13. {
  14. class Program
  15. {
  16. static void Main(string[] args)
  17. {
  18. //Create our service
  19. PushService push = new PushService();
  20. //Wire up the events
  21. push.Events.OnDeviceSubscriptionExpired += new Common.ChannelEvents.DeviceSubscriptionExpired(Events_OnDeviceSubscriptionExpired);
  22. push.Events.OnDeviceSubscriptionIdChanged += new Common.ChannelEvents.DeviceSubscriptionIdChanged(Events_OnDeviceSubscriptionIdChanged);
  23. push.Events.OnChannelException += new Common.ChannelEvents.ChannelExceptionDelegate(Events_OnChannelException);
  24. push.Events.OnNotificationSendFailure += new Common.ChannelEvents.NotificationSendFailureDelegate(Events_OnNotificationSendFailure);
  25. push.Events.OnNotificationSent += new Common.ChannelEvents.NotificationSentDelegate(Events_OnNotificationSent);
  26. push.Events.OnChannelCreated += new Common.ChannelEvents.ChannelCreatedDelegate(Events_OnChannelCreated);
  27. push.Events.OnChannelDestroyed += new Common.ChannelEvents.ChannelDestroyedDelegate(Events_OnChannelDestroyed);
  28. //Configure and start Apple APNS
  29. // IMPORTANT: Make sure you use the right Push certificate. Apple allows you to generate one for connecting to Sandbox,
  30. // and one for connecting to Production. You must use the right one, to match the provisioning profile you build your
  31. // app with!
  32. var appleCert = File.ReadAllBytes(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "../../../Resources/PushSharp.Apns.Sandbox.p12"));
  33. //IMPORTANT: If you are using a Development provisioning Profile, you must use the Sandbox push notification server
  34. // (so you would leave the first arg in the ctor of ApplePushChannelSettings as 'false')
  35. // If you are using an AdHoc or AppStore provisioning profile, you must use the Production push notification server
  36. // (so you would change the first arg in the ctor of ApplePushChannelSettings to 'true')
  37. push.StartApplePushService(new ApplePushChannelSettings(appleCert, "pushsharp"));
  38. //Configure and start Android GCM
  39. //IMPORTANT: The SENDER_ID is your Google API Console App Project ID.
  40. // Be sure to get the right Project ID from your Google APIs Console. It's not the named project ID that appears in the Overview,
  41. // but instead the numeric project id in the url: eg: https://code.google.com/apis/console/?pli=1#project:785671162406:overview
  42. // where 785671162406 is the project id, which is the SENDER_ID to use!
  43. push.StartGoogleCloudMessagingPushService(new GcmPushChannelSettings("785671162406", "AIzaSyC2PZNXQDVaUpZGmtsF_Vp8tHtIABVjazI", "com.pushsharp.test"));
  44. //Configure and start Windows Phone Notifications
  45. push.StartWindowsPhonePushService(new WindowsPhonePushChannelSettings());
  46. //Configure and start Windows Notifications
  47. push.StartWindowsPushService(new WindowsPushChannelSettings("677AltusApps.PushSharpTest",
  48. "ms-app://s-1-15-2-397915024-884168245-3562497613-3307968140-4074292843-797285123-433377759", "ei5Lott1HEbbZBv2wGDTUsrCjU++Pj8Z"));
  49. //Fluent construction of a Windows Toast Notification
  50. push.QueueNotification(NotificationFactory.Windows().Toast().AsToastText01("This is a test").ForChannelUri("YOUR_CHANNEL_URI_HERE"));
  51. //Fluent construction of a Windows Phone Toast notification
  52. //IMPORTANT: For Windows Phone you MUST use your own Endpoint Uri here that gets generated within your Windows Phone app itself!
  53. push.QueueNotification(NotificationFactory.WindowsPhone().Toast()
  54. .ForEndpointUri(new Uri("http://sn1.notify.live.net/throttledthirdparty/01.00/AAFCoNoCXidwRpn5NOxvwSxPAgAAAAADAgAAAAQUZm52OkJCMjg1QTg1QkZDMkUxREQ"))
  55. .ForOSVersion(WindowsPhone.WindowsPhoneDeviceOSVersion.MangoSevenPointFive)
  56. .WithBatchingInterval(WindowsPhone.BatchingInterval.Immediate)
  57. .WithNavigatePath("/MainPage.xaml")
  58. .WithText1("PushSharp")
  59. .WithText2("This is a Toast"));
  60. //Fluent construction of an iOS notification
  61. //IMPORTANT: For iOS you MUST MUST MUST use your own DeviceToken here that gets generated within your iOS app itself when the Application Delegate
  62. // for registered for remote notifications is called, and the device token is passed back to you
  63. push.QueueNotification(NotificationFactory.Apple()
  64. .ForDeviceToken("1071737321559691b28fffa1aa4c8259d970fe0fc496794ad0486552fc9ec3db")
  65. .WithAlert("1 Alert Text!")
  66. .WithSound("default")
  67. .WithBadge(7));
  68. //Fluent construction of an Android GCM Notification
  69. //IMPORTANT: For Android you MUST use your own RegistrationId here that gets generated within your Android app itself!
  70. push.QueueNotification(NotificationFactory.AndroidGcm()
  71. .ForDeviceRegistrationId("APA91bG7J-cZjkURrqi58cEd5ain6hzi4i06T0zg9eM2kQAprV-fslFiq60hnBUVlnJPlPV-4K7X39aHIe55of8fJugEuYMyAZSUbmDyima5ZTC7hn4euQ0Yflj2wMeTxnyMOZPuwTLuYNiJ6EREeI9qJuJZH9Zu9g")
  72. .WithCollapseKey("NONE")
  73. .WithJson("{\"alert\":\"Alert Text!\",\"badge\":\"7\"}"));
  74. push.QueueNotification(NotificationFactory.Windows()
  75. .Toast()
  76. .ForChannelUri("https://bn1.notify.windows.com/?token=AgUAAACC2u7flXAmaevcggrLenaSdExjVfIHvr6KSZrg0KeuGrcz877rPJprPL9bEuQH%2bacmmm%2beUyXNXEM8oRNit%2bzPoigksDOq6bIFyV3XGmhUmXadysLokl5rlmTscvHGAbs%3d")
  77. .WithRequestForStatus(true)
  78. .AsToastText01("This is a test!"));
  79. Console.WriteLine("Waiting for Queue to Finish...");
  80. //Stop and wait for the queues to drains
  81. push.StopAllServices(true);
  82. Console.WriteLine("Queue Finished, press return to exit...");
  83. Console.ReadLine();
  84. }
  85. static void Events_OnDeviceSubscriptionIdChanged(Common.PlatformType platform, string oldDeviceInfo, string newDeviceInfo, Common.Notification notification)
  86. {
  87. //Currently this event will only ever happen for Android GCM
  88. Console.WriteLine("Device Registration Changed: Old-> " + oldDeviceInfo + " New-> " + newDeviceInfo);
  89. }
  90. static void Events_OnNotificationSent(Common.Notification notification)
  91. {
  92. Console.WriteLine("Sent: " + notification.Platform.ToString() + " -> " + notification.ToString());
  93. }
  94. static void Events_OnNotificationSendFailure(Common.Notification notification, Exception notificationFailureException)
  95. {
  96. Console.WriteLine("Failure: " + notification.Platform.ToString() + " -> " + notificationFailureException.Message + " -> " + notification.ToString());
  97. }
  98. static void Events_OnChannelException(Exception exception, Common.PlatformType platformType, Common.Notification notification)
  99. {
  100. Console.WriteLine("Channel Exception: " + platformType.ToString() + " -> " + exception.ToString());
  101. }
  102. static void Events_OnDeviceSubscriptionExpired(Common.PlatformType platform, string deviceInfo, Common.Notification notification)
  103. {
  104. Console.WriteLine("Device Subscription Expired: " + platform.ToString() + " -> " + deviceInfo);
  105. }
  106. static void Events_OnChannelDestroyed(Common.PlatformType platformType, int newChannelCount)
  107. {
  108. Console.WriteLine("Channel Destroyed for: " + platformType.ToString() + " Channel Count: " + newChannelCount);
  109. }
  110. static void Events_OnChannelCreated(Common.PlatformType platformType, int newChannelCount)
  111. {
  112. Console.WriteLine("Channel Created for: " + platformType.ToString() + " Channel Count: " + newChannelCount);
  113. }
  114. }
  115. }