public static FirebaseNotificationData ForSingleResult(FirebaseResponse response, int resultIndex)
        {
            var result = new FirebaseNotificationData
            {
                Tag       = response.OriginalNotification.Tag,
                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;

            return(result);
        }
Exemple #2
0
        private async Task ProcessResponseOk(HttpResponseMessage httpResponse, FirebaseNotificationData notification)
        {
            var result = new FirebaseResponse
            {
                ResponseCode         = FirebaseResponseCode.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["result"] as JArray ?? new JArray();

            foreach (var jsonResult in jsonResults)
            {
                var msgResult = new FirebaseMessageResult
                {
                    MessageId = jsonResult.Value <string>("message_id"),
                    CanonicalRegistrationId = jsonResult.Value <string>("registration_id"),
                    ResponseStatus          = FirebaseResponseStatus.Ok
                };


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

                    msgResult.ResponseStatus = GetFirebaseReponseStatus(err);
                }

                result.Results.Add(msgResult);
            }

            var index           = 0;
            var multicastResult = new FirebaseMulticastResultException();

            //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 = FirebaseNotificationData.ForSingleResult(result, index);

                singleResultNotification.MessageId = r.MessageId;

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

                _logger.LogWarning("Fail to send notification through Fireabse", multicastResult.Failed.First().Value);
                // 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)
            {
                _logger.LogWarning(multicastResult, "Fail to send notification through Firebase");
                throw multicastResult;
            }
        }