Esempio n. 1
0
        public static GcmNotification ForSingleResult(GcmResponse response, int resultIndex)
        {
            var result = new GcmNotification();

            result.Tag       = response.OriginalNotification.Tag;
            result.MessageId = response.OriginalNotification.MessageId;
            result.RegistrationIds.Add(response.OriginalNotification.RegistrationIds [resultIndex]);
            result.CollapseKey      = response.OriginalNotification.CollapseKey;
            result.Data             = response.OriginalNotification.Data;
            result.DelayWhileIdle   = response.OriginalNotification.DelayWhileIdle;
            result.ContentAvailable = response.OriginalNotification.ContentAvailable;
            result.DryRun           = response.OriginalNotification.DryRun;
            result.Priority         = response.OriginalNotification.Priority;
            result.To = response.OriginalNotification.To;
            result.NotificationKey = response.OriginalNotification.NotificationKey;

            return(result);
        }
Esempio n. 2
0
        public static GcmNotification ForSingleResult (GcmResponse response, int resultIndex)
        {
            var result = new GcmNotification ();
            result.Tag = response.OriginalNotification.Tag;
            result.MessageId = response.OriginalNotification.MessageId;

            if (response.OriginalNotification.RegistrationIds != null && response.OriginalNotification.RegistrationIds.Count >= (resultIndex + 1))
                result.RegistrationIds.Add (response.OriginalNotification.RegistrationIds [resultIndex]);

            result.CollapseKey = response.OriginalNotification.CollapseKey;
            result.Data = response.OriginalNotification.Data;
            result.DelayWhileIdle = response.OriginalNotification.DelayWhileIdle;
            result.ContentAvailable = response.OriginalNotification.ContentAvailable;
            result.DryRun = response.OriginalNotification.DryRun;
            result.Priority = response.OriginalNotification.Priority;
            result.To = response.OriginalNotification.To;
            result.NotificationKey = response.OriginalNotification.NotificationKey;

            return result;
        }
Esempio n. 3
0
        async Task ProcessResponseOk(HttpResponseMessage httpResponse, GcmNotification notification)
        {
            var multicastResult = new GcmMulticastResultException();

            var result = new GcmResponse()
            {
                ResponseCode         = GcmResponseCode.Ok,
                OriginalNotification = notification
            };

            var str = await httpResponse.Content.ReadAsStringAsync();

            var json = JObject.Parse(str);

            result.NumberOfCanonicalIds = json.Value <long> ("canonical_ids");
            result.NumberOfFailures     = json.Value <long> ("failure");
            result.NumberOfSuccesses    = json.Value <long> ("success");

            var jsonResults = json ["results"] as JArray ?? new JArray();

            foreach (var r in jsonResults)
            {
                var msgResult = new GcmMessageResult();

                msgResult.MessageId = r.Value <string> ("message_id");
                msgResult.CanonicalRegistrationId = r.Value <string> ("registration_id");
                msgResult.ResponseStatus          = GcmResponseStatus.Ok;

                if (!string.IsNullOrEmpty(msgResult.CanonicalRegistrationId))
                {
                    msgResult.ResponseStatus = GcmResponseStatus.CanonicalRegistrationId;
                }
                else if (r ["error"] != null)
                {
                    var err = r.Value <string> ("error") ?? "";

                    msgResult.ResponseStatus = GetGcmResponseStatus(err);
                }

                result.Results.Add(msgResult);
            }

            int index = 0;

            //Loop through every result in the response
            // We will raise events for each individual result so that the consumer of the library
            // can deal with individual registrationid's for the notification
            foreach (var r in result.Results)
            {
                var singleResultNotification = GcmNotification.ForSingleResult(result, index);

                singleResultNotification.MessageId = r.MessageId;

                if (r.ResponseStatus == GcmResponseStatus.Ok)   // Success
                {
                    multicastResult.Succeeded.Add(singleResultNotification);
                }
                else if (r.ResponseStatus == GcmResponseStatus.CanonicalRegistrationId)     //Need to swap reg id's
                //Swap Registrations Id's
                {
                    var newRegistrationId = r.CanonicalRegistrationId;
                    var oldRegistrationId = string.Empty;

                    if (singleResultNotification.RegistrationIds != null && singleResultNotification.RegistrationIds.Count > 0)
                    {
                        oldRegistrationId = singleResultNotification.RegistrationIds[0];
                    }
                    else if (!string.IsNullOrEmpty(singleResultNotification.To))
                    {
                        oldRegistrationId = singleResultNotification.To;
                    }

                    multicastResult.Failed.Add(singleResultNotification,
                                               new DeviceSubscriptionExpiredException(singleResultNotification)
                    {
                        OldSubscriptionId = oldRegistrationId,
                        NewSubscriptionId = newRegistrationId
                    });
                }
                else if (r.ResponseStatus == GcmResponseStatus.Unavailable)     // Unavailable
                {
                    multicastResult.Failed.Add(singleResultNotification, new GcmNotificationException(singleResultNotification, "Unavailable Response Status"));
                }
                else if (r.ResponseStatus == GcmResponseStatus.NotRegistered)     //Bad registration Id
                {
                    var oldRegistrationId = string.Empty;

                    if (singleResultNotification.RegistrationIds != null && singleResultNotification.RegistrationIds.Count > 0)
                    {
                        oldRegistrationId = singleResultNotification.RegistrationIds[0];
                    }
                    else if (!string.IsNullOrEmpty(singleResultNotification.To))
                    {
                        oldRegistrationId = singleResultNotification.To;
                    }

                    multicastResult.Failed.Add(singleResultNotification,
                                               new DeviceSubscriptionExpiredException(singleResultNotification)
                    {
                        OldSubscriptionId = oldRegistrationId
                    });
                }
                else
                {
                    multicastResult.Failed.Add(singleResultNotification, new GcmNotificationException(singleResultNotification, "Unknown Failure: " + r.ResponseStatus));
                }

                index++;
            }

            // If we only have 1 total result, it is not *multicast*,
            if (multicastResult.Succeeded.Count + multicastResult.Failed.Count == 1)
            {
                // If not multicast, and succeeded, don't throw any errors!
                if (multicastResult.Succeeded.Count == 1)
                {
                    return;
                }
                // Otherwise, throw the one single failure we must have
                throw multicastResult.Failed.First().Value;
            }

            // If we get here, we must have had a multicast message
            // throw if we had any failures at all (otherwise all must be successful, so throw no error
            if (multicastResult.Failed.Count > 0)
            {
                throw multicastResult;
            }
        }