コード例 #1
0
        void transport_MessageResponseReceived(GcmMessageTransportResponse response)
        {
            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 response.Results)
            {
                var singleResultNotification = GcmNotification.ForSingleResult(response, index);

                if (r.ResponseStatus == GcmMessageTransportResponseStatus.Ok)
                {
                    //It worked! Raise success
                    this.Events.RaiseNotificationSent(singleResultNotification);
                }
                else if (r.ResponseStatus == GcmMessageTransportResponseStatus.CanonicalRegistrationId)
                {
                    //Swap Registrations Id's
                    var newRegistrationId = r.CanonicalRegistrationId;

                    this.Events.RaiseDeviceSubscriptionIdChanged(PlatformType.AndroidGcm, singleResultNotification.RegistrationIds[0], newRegistrationId, singleResultNotification);
                }
                else if (r.ResponseStatus == GcmMessageTransportResponseStatus.Unavailable)
                {
                    this.QueueNotification(singleResultNotification);
                }
                else if (r.ResponseStatus == GcmMessageTransportResponseStatus.NotRegistered)
                {
                    //Raise failure and device expired
                    this.Events.RaiseDeviceSubscriptionExpired(PlatformType.AndroidGcm, singleResultNotification.RegistrationIds[0], singleResultNotification);
                }
                else
                {
                    //Raise failure, for unknown reason
                    this.Events.RaiseNotificationSendFailure(singleResultNotification, new GcmMessageTransportException(r.ResponseStatus.ToString(), response));
                }

                index++;

                //decrement pending results.
                Interlocked.Decrement(ref PendingNotificationsResult);
            }

            Interlocked.Decrement(ref waitCounter);
        }
コード例 #2
0
ファイル: GcmPushChannel.cs プロジェクト: yself/PushSharp
        void processResponseOk(GcmAsyncParameters asyncParam)
        {
            var result = new GcmMessageTransportResponse()
            {
                ResponseCode = GcmMessageTransportResponseCode.Ok,
                Message      = asyncParam.Message
            };

            //Get the response body
            var json = new JObject();

            var str = string.Empty;

            try { str = (new StreamReader(asyncParam.WebResponse.GetResponseStream())).ReadToEnd(); }
            catch { }

            try { json = JObject.Parse(str); }
            catch { }

            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;

            if (jsonResults == null)
            {
                jsonResults = 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          = GcmMessageTransportResponseStatus.Ok;

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

                    switch (err.ToLower().Trim())
                    {
                    case "ok":
                        msgResult.ResponseStatus = GcmMessageTransportResponseStatus.Ok;
                        break;

                    case "missingregistration":
                        msgResult.ResponseStatus = GcmMessageTransportResponseStatus.MissingRegistrationId;
                        break;

                    case "unavailable":
                        msgResult.ResponseStatus = GcmMessageTransportResponseStatus.Unavailable;
                        break;

                    case "notregistered":
                        msgResult.ResponseStatus = GcmMessageTransportResponseStatus.NotRegistered;
                        break;

                    case "invalidregistration":
                        msgResult.ResponseStatus = GcmMessageTransportResponseStatus.InvalidRegistration;
                        break;

                    case "mismatchsenderid":
                        msgResult.ResponseStatus = GcmMessageTransportResponseStatus.MismatchSenderId;
                        break;

                    case "messagetoobig":
                        msgResult.ResponseStatus = GcmMessageTransportResponseStatus.MessageTooBig;
                        break;

                    case "invaliddatakey":
                        msgResult.ResponseStatus = GcmMessageTransportResponseStatus.InvalidDataKey;
                        break;

                    case "invalidttl":
                        msgResult.ResponseStatus = GcmMessageTransportResponseStatus.InvalidTtl;
                        break;

                    case "internalservererror":
                        msgResult.ResponseStatus = GcmMessageTransportResponseStatus.InternalServerError;
                        break;

                    default:
                        msgResult.ResponseStatus = GcmMessageTransportResponseStatus.Error;
                        break;
                    }
                }

                result.Results.Add(msgResult);
            }

            asyncParam.WebResponse.Close();

            int index = 0;

            var response = result;

            //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 response.Results)
            {
                var singleResultNotification = GcmNotification.ForSingleResult(response, index);

                if (r.ResponseStatus == GcmMessageTransportResponseStatus.Ok)
                {
                    //It worked! Raise success
                    asyncParam.Callback(this, new SendNotificationResult(singleResultNotification));
                }
                else if (r.ResponseStatus == GcmMessageTransportResponseStatus.CanonicalRegistrationId)
                {
                    //Swap Registrations Id's
                    var newRegistrationId = r.CanonicalRegistrationId;
                    var oldRegistrationId = string.Empty;

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

                    asyncParam.Callback(this, new SendNotificationResult(singleResultNotification, false, new DeviceSubscriptonExpiredException())
                    {
                        OldSubscriptionId = oldRegistrationId, NewSubscriptionId = newRegistrationId, IsSubscriptionExpired = true
                    });
                }
                else if (r.ResponseStatus == GcmMessageTransportResponseStatus.Unavailable)
                {
                    asyncParam.Callback(this, new SendNotificationResult(singleResultNotification, true, new Exception("Unavailable Response Status")));
                }
                else if (r.ResponseStatus == GcmMessageTransportResponseStatus.NotRegistered)
                {
                    var oldRegistrationId = string.Empty;

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

                    //Raise failure and device expired
                    asyncParam.Callback(this, new SendNotificationResult(singleResultNotification, false, new DeviceSubscriptonExpiredException())
                    {
                        OldSubscriptionId = oldRegistrationId, IsSubscriptionExpired = true, SubscriptionExpiryUtc = DateTime.UtcNow
                    });
                }
                else
                {
                    //Raise failure, for unknown reason
                    asyncParam.Callback(this, new SendNotificationResult(singleResultNotification, false, new GcmMessageTransportException(r.ResponseStatus.ToString(), response)));
                }

                index++;
            }

            Interlocked.Decrement(ref waitCounter);
        }