Exemplo n.º 1
0
        public void Gcm_Send_Single ()
        {
            var succeeded = 0;
            var failed = 0;
            var attempted = 0;

            var config = new GcmConfiguration (Settings.Instance.GcmSenderId, Settings.Instance.GcmAuthToken, null);
            var broker = new GcmServiceBroker (config);
            broker.OnNotificationFailed += (notification, exception) => {
                failed++;        
            };
            broker.OnNotificationSucceeded += (notification) => {
                succeeded++;
            };

            broker.Start ();

            foreach (var regId in Settings.Instance.GcmRegistrationIds) {
                attempted++;

                broker.QueueNotification (new GcmNotification {
                    RegistrationIds = new List<string> { 
                        regId
                    },
                    Data = JObject.Parse ("{ \"somekey\" : \"somevalue\" }")
                });
            }

            broker.Stop ();

            Assert.AreEqual (attempted, succeeded);
            Assert.AreEqual (0, failed);
        }
Exemplo n.º 2
0
        public GcmServiceConnection(GcmConfiguration configuration)
        {
            Configuration = configuration;
            http          = new HttpClient();

            http.DefaultRequestHeaders.UserAgent.Clear();
            http.DefaultRequestHeaders.UserAgent.Add(new ProductInfoHeaderValue("PushSharp", "3.0"));
            http.DefaultRequestHeaders.TryAddWithoutValidation("Authorization", "key=" + Configuration.SenderAuthToken);
        }
Exemplo n.º 3
0
 public GcmServiceConnection(GcmConfiguration configuration)
 {
     Configuration = configuration;
     if (Configuration.UseProxy)
     {
         http = GetClientHTTPProxy(Configuration.ProxyHost, Configuration.ProxyPort, Configuration.ProxyCredentials);
     }
     else
     {
         http = new HttpClient();
     }
     http.DefaultRequestHeaders.UserAgent.Clear();
     http.DefaultRequestHeaders.UserAgent.Add(new ProductInfoHeaderValue("PushSharp", "3.0"));
     http.DefaultRequestHeaders.TryAddWithoutValidation("Authorization", "key=" + Configuration.SenderAuthToken);
 }
Exemplo n.º 4
0
 public GcmServiceBroker(GcmConfiguration configuration) : base(new GcmServiceConnectionFactory(configuration))
 {
 }
Exemplo n.º 5
0
 public GcmServiceConnectionFactory(GcmConfiguration configuration)
 {
     Configuration = configuration;
 }
Exemplo n.º 6
0
        private void InitGcmBroker(GlobalSettings globalSettings)
        {
            if(string.IsNullOrWhiteSpace(globalSettings.Push.GcmSenderId) || string.IsNullOrWhiteSpace(globalSettings.Push.GcmApiKey)
                || string.IsNullOrWhiteSpace(globalSettings.Push.GcmAppPackageName))
            {
                return;
            }

            var gcmConfig = new GcmConfiguration(globalSettings.Push.GcmSenderId, globalSettings.Push.GcmApiKey,
                globalSettings.Push.GcmAppPackageName);

            _gcmBroker = new GcmServiceBroker(gcmConfig);
            _gcmBroker.OnNotificationFailed += GcmBroker_OnNotificationFailed;
            _gcmBroker.OnNotificationSucceeded += (notification) =>
            {
                Debug.WriteLine("GCM Notification Sent!");
            };
            _gcmBroker.Start();
        }
        public AndroidPushMessageSender(ApplicationType? appType)
        {
            if (appType == ApplicationType.Tablet)
                config = new GcmConfiguration(Options.GCMSenderIdTablet, Options.DeviceAuthTokenTablet, null);
            else
                config = new GcmConfiguration(Options.GCMSenderIdMobile, Options.DeviceAuthTokenMobile, null);

            gcmBroker = new GcmServiceBroker(config);

            gcmBroker.OnNotificationFailed += (notification, aggregateEx) =>
            {
                aggregateEx.Handle(ex =>
                {
                    if (ex is GcmNotificationException)
                    {
                        var notificationException = (GcmNotificationException)ex;
                        var gcmNotification = notificationException.Notification;
                        var description = notificationException.Description;

                        notificationService.Unsubscribe(devToken, userId);

                        log.Error(string.Format("GCM Notification Failed: ID={0}, Desc={1}", gcmNotification.MessageId, description));
                    }
                    else if (ex is GcmMulticastResultException)
                    {
                        var multicastException = (GcmMulticastResultException)ex;

                        foreach (var succeededNotification in multicastException.Succeeded)
                        {
                            log.Error(string.Format("GCM Notification Failed: ID={0}", succeededNotification.MessageId));
                        }

                        foreach (var failedKvp in multicastException.Failed)
                        {
                            var n = failedKvp.Key;
                            var e = failedKvp.Value;

                            log.Error(string.Format("GCM Notification Failed: ID={0}, Desc={1}", n.MessageId, e.InnerException));
                        }
                        notificationService.Unsubscribe(devToken, userId);
                    }
                    else if (ex is DeviceSubscriptionExpiredException)
                    {

                        var expiredException = (DeviceSubscriptionExpiredException)ex;

                        var oldId = expiredException.OldSubscriptionId;
                        var newId = expiredException.NewSubscriptionId;

                        log.Error(string.Format("Device RegistrationId Expired: {0}", oldId));
                        log.Error(string.Format("Device RegistrationId Changed To: {0}", newId));

                        if (!string.IsNullOrWhiteSpace(newId))
                        {
                            notificationService.Unsubscribe(oldId, userId);

                            log.Error(string.Format("Device RegistrationId Changed To: {0}", newId));
                        }
                        else
                            notificationService.Unsubscribe(oldId, userId);
                    }
                    else if (ex is RetryAfterException)
                    {
                        var retryException = (RetryAfterException)ex;

                        log.Error(string.Format("GCM Rate Limited, don't send more until after {0}", retryException.RetryAfterUtc));
                    }
                    else
                    {
                        log.Error("GCM Notification Failed for some unknown reason");
                    }

                    log.Error("Failed for:" + string.Join(",", notification.RegistrationIds));

                    return true;
                });
            };

            gcmBroker.OnNotificationSucceeded += (notification) =>
            {
                log.Info("Success for:" + string.Join(",", notification.RegistrationIds));
            };

            gcmBroker.Start();
        }