Пример #1
0
        protected override void OnHandleIntent(Intent intent)
        {
            try
            {
                var context = ApplicationContext;
                var action  = intent.Action;

                if (action.Equals(Constants.IntentFromGcmRegistrationCallback))
                {
                    HandleRegistration(context, intent);
                }
                else if (action.Equals(Constants.IntentFromGcmMessage))
                {
                    // checks for special messages
                    var messageType = intent.GetStringExtra(Constants.ExtraSpecialMessage);
                    if (messageType != null)
                    {
                        if (messageType.Equals(Constants.ValueDeletedMessages))
                        {
                            var sTotal = intent.GetStringExtra(Constants.ExtraTotalDeleted);
                            if (!string.IsNullOrEmpty(sTotal))
                            {
                                var nTotal = 0;
                                if (int.TryParse(sTotal, out nTotal))
                                {
                                    //Logger.Debug("Received deleted messages notification: " + nTotal);
                                    OnDeletedMessages(context, nTotal);
                                }
                            }
                        }
                    }
                    else
                    {
                        OnMessage(context, intent);
                    }
                }
                else if (action.Equals(Constants.IntentFromGcmLibraryRetry))
                {
                    var token = intent.GetStringExtra(ExtraToken);

                    if (!string.IsNullOrEmpty(token) && !this.token.Equals(token))
                    {
                        // make sure intent was generated by this class, not by a
                        // malicious app.
                        //Logger.Debug("Received invalid token: " + token);
                        return;
                    }

                    // retry last call
                    if (GcmClient.IsRegistered(context))
                    {
                        GcmClient.InternalUnRegister(context);
                    }
                    else
                    {
                        GcmClient.InternalRegister(context, SenderIds);
                    }
                }
            }
            finally
            {
                // Release the power lock, so phone can get back to sleep.
                // The lock is reference-counted by default, so multiple
                // messages are ok.

                // If OnMessage() needs to spawn a thread or do something else,
                // it should use its own lock.
                lock (Lock)
                {
                    //Sanity check for null as this is a public method
                    if (sWakeLock != null)
                    {
                        //Logger.Debug("Releasing Wakelock");
                        sWakeLock.Release();
                    }
                }
            }
        }
        protected override void OnHandleIntent(Intent intent)
        {
            try
            {
                var context = this.ApplicationContext;
                var action  = intent.Action;

                if (action.Equals(Constants.INTENT_FROM_GCM_REGISTRATION_CALLBACK))
                {
                    handleRegistration(context, intent);
                }
                else if (action.Equals(Constants.INTENT_FROM_GCM_MESSAGE))
                {
                    // checks for special messages
                    var messageType = intent.GetStringExtra(Constants.EXTRA_SPECIAL_MESSAGE);
                    if (messageType != null)
                    {
                        if (messageType.Equals(Constants.VALUE_DELETED_MESSAGES))
                        {
                            var sTotal = intent.GetStringExtra(Constants.EXTRA_TOTAL_DELETED);
                            if (!string.IsNullOrEmpty(sTotal))
                            {
                                int nTotal = 0;
                                if (int.TryParse(sTotal, out nTotal))
                                {
                                    Logger.Debug("Received deleted messages notification: " + nTotal);
                                    OnDeletedMessages(context, nTotal);
                                }
                                else
                                {
                                    Logger.Debug("GCM returned invalid number of deleted messages: " + sTotal);
                                }
                            }
                        }
                        else
                        {
                            // application is not using the latest GCM library
                            Logger.Debug("Received unknown special message: " + messageType);
                        }
                    }
                    else
                    {
                        OnMessage(context, intent);
                    }
                }
                else if (action.Equals(Constants.INTENT_FROM_GCM_LIBRARY_RETRY))
                {
                    var token = intent.GetStringExtra(EXTRA_TOKEN);

                    if (!string.IsNullOrEmpty(token) && !TOKEN.Equals(token))
                    {
                        // make sure intent was generated by this class, not by a
                        // malicious app.
                        Logger.Debug("Received invalid token: " + token);
                        return;
                    }

                    // retry last call
                    if (GcmClient.IsRegistered(context))
                    {
                        GcmClient.internalUnRegister(context);
                    }
                    else
                    {
                        GcmClient.internalRegister(context, SenderIds);
                    }
                }
            }
            finally
            {
                // Release the power lock, so phone can get back to sleep.
                // The lock is reference-counted by default, so multiple
                // messages are ok.

                // If OnMessage() needs to spawn a thread or do something else,
                // it should use its own lock.
                lock (LOCK)
                {
                    //Sanity check for null as this is a public method
                    if (sWakeLock != null)
                    {
                        Logger.Debug("Releasing Wakelock");
                        sWakeLock.Release();
                    }
                    else
                    {
                        //Should never happen during normal workflow
                        Logger.Debug("Wakelock reference is null");
                    }
                }
            }
        }