private void gcmPush_OnNotificationSucceeded(GcmNotification notification) { string token = ""; try { token = notification.RegistrationIds.First(); TokensSended.Add(token); } catch { } ContadorEnviados++; }
void apnsPush_OnNotificationSucceeded(ApnsNotification notification) { string token = ""; try { token = notification.DeviceToken; TokensSended.Add(token); } catch (Exception e) { Console.WriteLine(e.Message); } ContadorEnviados++; }
public void DoWork() { while (!_shouldStop) { ContadorIntentos++; try { byte[] appleCert = null; if (!File.Exists(CertAppleURL)) { EscribirEnLog("hubo Error : No existe el Certificado iOS"); } else { appleCert = File.ReadAllBytes(CertAppleURL); EscribirEnLog("certificado encontrado:" + "iOS:" + CertAppleURL + ", Pass:"******", GCMKeyCliente: " + GCMKeyCliente + ", GCMKeyEmpresa: " + GCMKeyEmpresa + ", GCMKeyConductor: " + GCMKeyConductor + ", Alerta " + alerta); } foreach (var token in Tokens) { if (!TokensSended.Contains(token.TokenID)) { try { string FBSenderAuthToken = ""; switch (token.TipoAplicacion) { case Aplicacion.Usuario: FBSenderAuthToken = GCMKeyCliente; break; case Aplicacion.Conductor: FBSenderAuthToken = GCMKeyConductor; break; case Aplicacion.Administrador: FBSenderAuthToken = GCMKeyCliente; break; case Aplicacion.Encargado: FBSenderAuthToken = GCMKeyEmpresa; break; default: FBSenderAuthToken = GCMKeyCliente; break; } switch (token.TipoDispositivo) { case Dispositivo.Apple: if (appleCert != null) { ApnsConfiguration.ApnsServerEnvironment ambiente = ApplePushProduccion ? ApnsConfiguration.ApnsServerEnvironment.Production : ApnsConfiguration.ApnsServerEnvironment.Sandbox; var configApns = new ApnsConfiguration(ambiente, appleCert, CertApplePass, false); apnsPush = new ApnsServiceBroker(configApns); apnsPush.OnNotificationFailed += apnsPush_OnNotificationFailed; apnsPush.OnNotificationSucceeded += apnsPush_OnNotificationSucceeded; apnsPush.Start(); apnsPush.QueueNotification((ApnsNotification)alerta.Notificacion(token)); apnsPush.Stop(); } break; case Dispositivo.Android: var configGCM = new GcmConfiguration(FBSenderAuthToken); configGCM.OverrideUrl("https://fcm.googleapis.com/fcm/send"); configGCM.GcmUrl = "https://fcm.googleapis.com/fcm/send"; gcmPush = new GcmServiceBroker(configGCM); gcmPush.OnNotificationSucceeded += gcmPush_OnNotificationSucceeded; gcmPush.OnNotificationFailed += gcmPush_OnNotificationFailed; gcmPush.Start(); gcmPush.QueueNotification((GcmNotification)alerta.Notificacion(token)); gcmPush.Stop(); break; } } catch (Exception ex) { EscribirEnLog("hubo Error : " + ex.Message); Console.WriteLine(ex.Message); } } } if (todoCorrecto() || (ContadorIntentos >= CantidadIntentos)) { this._shouldStop = true; } else { this.ContadorEnviados = 0; this.ContadorNoEnviados = 0; Thread.Sleep(SegundosReintento * 1000); } } catch (Exception ex) { EscribirEnLog("hubo Error : " + ex.Message); Console.WriteLine(ex.Message); } } }