Advertisement
Guest User

Pushsharp 4 Send FCM notification

a guest
Jun 21st, 2016
342
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.83 KB | None | 0 0
  1.   var config = new GcmConfiguration("GOOGLE-API-KEY");
  2.         config.GcmUrl = "https://fcm.googleapis.com/fcm/send";
  3.         // Create a new broker
  4.         var gcmBroker = new GcmServiceBroker(config);
  5.  
  6.         // Wire up events
  7.         gcmBroker.OnNotificationFailed += (notification, aggregateEx) =>
  8.         {
  9.  
  10.             aggregateEx.Handle(ex =>
  11.             {
  12.  
  13.                 // See what kind of exception it was to further diagnose
  14.                 if (ex is GcmNotificationException)
  15.                 {
  16.                     var notificationException = (GcmNotificationException)ex;
  17.  
  18.                     // Deal with the failed notification
  19.                     var gcmNotification = notificationException.Notification;
  20.                     var description = notificationException.Description;
  21.  
  22.                     // Console.WriteLine ($"GCM Notification Failed: ID={gcmNotification.MessageId}, Desc={description}");
  23.                 }
  24.                 else if (ex is GcmMulticastResultException)
  25.                 {
  26.                     var multicastException = (GcmMulticastResultException)ex;
  27.  
  28.                     foreach (var succeededNotification in multicastException.Succeeded)
  29.                     {
  30.                         Console.WriteLine("GCM Notification Failed: ID={succeededNotification.MessageId}");
  31.                     }
  32.  
  33.                     foreach (var failedKvp in multicastException.Failed)
  34.                     {
  35.                         var n = failedKvp.Key;
  36.                         var e = failedKvp.Value;
  37.  
  38.                         Console.WriteLine("GCM Notification Failed: ID={n.MessageId}, Desc={e.Description}");
  39.                     }
  40.  
  41.                 }
  42.                 else if (ex is DeviceSubscriptionExpiredException)
  43.                 {
  44.                     var expiredException = (DeviceSubscriptionExpiredException)ex;
  45.  
  46.                     var oldId = expiredException.OldSubscriptionId;
  47.                     var newId = expiredException.NewSubscriptionId;
  48.  
  49.                     Console.WriteLine("Device RegistrationId Expired: {oldId}");
  50.  
  51.                     //if (!string.IsNullOrWhitespace (newId)) {
  52.                     //    // If this value isn't null, our subscription changed and we should update our database
  53.                     //    Console.WriteLine ("Device RegistrationId Changed To: {newId}");
  54.                     //}
  55.                 }
  56.                 else if (ex is RetryAfterException)
  57.                 {
  58.                     var retryException = (RetryAfterException)ex;
  59.                     // If you get rate limited, you should stop sending messages until after the RetryAfterUtc date
  60.                     Console.WriteLine("GCM Rate Limited, don't send more until after {retryException.RetryAfterUtc}");
  61.                 }
  62.                 else
  63.                 {
  64.                     Console.WriteLine("GCM Notification Failed for some unknown reason");
  65.                 }
  66.  
  67.                 // Mark it as handled
  68.                 return true;
  69.             });
  70.         };
  71.  
  72.         gcmBroker.OnNotificationSucceeded += (notification) =>
  73.         {
  74.            
  75.             Console.WriteLine("GCM Notification Sent!");
  76.            
  77.         };
  78.  
  79.         // Start the broker
  80.         gcmBroker.Start();
  81.  
  82.         foreach (var regId in listofDevices)
  83.         {
  84.             // Queue a notification to send
  85.             gcmBroker.QueueNotification(new GcmNotification
  86.             {
  87.                 RegistrationIds = new List<string> {
  88.             regId
  89.         },
  90.                 Data = JObject.Parse("{ \"message\" : \"my message\",\"title\" : \"my title\" }"),
  91.                 TimeToLive = 500
  92.             });
  93.         }
  94.  
  95.         // Stop the broker, wait for it to finish  
  96.         // This isn't done after every message, but after you're
  97.         // done with the broker
  98.         gcmBroker.Stop();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement