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

            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);
        }
        async Task processResponseOk(HttpResponseMessage httpResponse, FcmNotification notification)
        {
            var multicastResult = new FcmMulticastResultException();

            var result = new FcmResponse()
            {
                ResponseCode         = FcmResponseCode.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 FcmMessageResult();

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

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

                    msgResult.ResponseStatus = GetFcmResponseStatus(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 = FcmNotification.ForSingleResult(result, index);

                singleResultNotification.MessageId = r.MessageId;

                if (r.ResponseStatus == FcmResponseStatus.Ok)   // Success
                {
                    multicastResult.Succeeded.Add(singleResultNotification);
                }
                else if (r.ResponseStatus == FcmResponseStatus.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 == FcmResponseStatus.Unavailable)     // Unavailable
                {
                    multicastResult.Failed.Add(singleResultNotification, new FcmNotificationException(singleResultNotification, "Unavailable Response Status"));
                }
                else if (r.ResponseStatus == FcmResponseStatus.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 FcmNotificationException(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;
            }
        }