Esempio n. 1
0
        private void updateView()
        {
            //Get the stored latest registration id
            var registrationId = PushClient.GetRegistrationId(this);

            //If it's empty, we need to register
            if (string.IsNullOrEmpty(registrationId))
            {
                registered = false;
                this.textRegistrationStatus.Text = "Registered: No";
                this.textRegistrationId.Text     = "Id: N/A";
                this.buttonRegister.Text         = "Register...";

                Log.Info(TAG, "Not registered...");
            }
            else
            {
                registered = true;
                this.textRegistrationStatus.Text = "Registered: Yes";
                this.textRegistrationId.Text     = "Id: " + registrationId;
                this.buttonRegister.Text         = "Unregister...";

                Log.Info(TAG, "Already Registered: " + registrationId);
            }

            var preferences = GetSharedPreferences(this.PackageName, FileCreationMode.Private);

            this.textLastMsg.Text = "Last Msg: " + Preferences.GetString(Constants.PREF_LAST_MESSAGE, "N/A");

            //Enable the button as it was normally disabled
            this.buttonRegister.Enabled = true;
        }
Esempio n. 2
0
        protected override void OnCreate(Bundle bundle)
        {
            base.OnCreate(bundle);

            SetContentView(Resource.Layout.Main);

            textRegistrationStatus = FindViewById <TextView>(Resource.Id.textRegistrationStatus);
            textRegistrationId     = FindViewById <TextView>(Resource.Id.textRegistrationId);
            textLastMsg            = FindViewById <TextView>(Resource.Id.textLastMessage);
            buttonRegister         = FindViewById <Button>(Resource.Id.buttonRegister);

            PushClient.CheckDevice(this);
            PushClient.CheckManifest(this);

            CheckPhoneNumber();

            this.buttonRegister.Click += delegate {
                if (!registered)
                {
                    Log.Info(TAG, "Registering...");
                    PushClient.Register(this, PushHandlerBroadcastReceiver.SENDER_IDS);
                }
                else
                {
                    Log.Info(TAG, "Unregistering...");
                    PushClient.UnRegister(this);
                }

                RunOnUiThread(() =>
                {
                    //Disable the button so that we can't click it again
                    //until we get back to the activity from a notification
                    this.buttonRegister.Enabled = false;
                });
            };
        }
Esempio n. 3
0
        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))
                                {
                                    Log.Verbose(TAG, "Received deleted messages notification: " + nTotal);
                                    OnDeletedMessages(context, nTotal);
                                }
                                else
                                {
                                    Log.Error(TAG, "GCM returned invalid number of deleted messages: " + sTotal);
                                }
                            }
                        }
                        else
                        {
                            // application is not using the latest GCM library
                            Log.Error(TAG, "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.
                        Log.Error(TAG, "Received invalid token: " + token);
                        return;
                    }

                    // retry last call
                    if (PushClient.IsRegistered(context))
                    {
                        PushClient.internalUnRegister(context);
                    }
                    else
                    {
                        PushClient.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)
                    {
                        Log.Verbose(TAG, "Releasing Wakelock");
                        sWakeLock.Release();
                    }
                    else
                    {
                        //Should never happen during normal workflow
                        Log.Error(TAG, "Wakelock reference is null");
                    }
                }
            }
        }
Esempio n. 4
0
        private void handleRegistration(Context context, Intent intent)
        {
            var registrationId = intent.GetStringExtra(Constants.EXTRA_REGISTRATION_ID);
            var error          = intent.GetStringExtra(Constants.EXTRA_ERROR);
            var unregistered   = intent.GetStringExtra(Constants.EXTRA_UNREGISTERED);

            Log.Debug(TAG, "handleRegistration: registrationId = " + registrationId + ", error = " + error + ", unregistered = " + unregistered);

            // registration succeeded
            if (registrationId != null)
            {
                PushClient.ResetBackoff(context);
                PushClient.SetRegistrationId(context, registrationId);
                OnRegistered(context, registrationId);
                return;
            }

            // unregistration succeeded
            if (unregistered != null)
            {
                // Remember we are unregistered
                PushClient.ResetBackoff(context);
                var oldRegistrationId = PushClient.ClearRegistrationId(context);
                OnUnRegistered(context, oldRegistrationId);
                return;
            }

            // last operation (registration or unregistration) returned an error;
            Log.Debug(TAG, "Registration error: " + error);
            // Registration failed
            if (Constants.ERROR_SERVICE_NOT_AVAILABLE.Equals(error))
            {
                var retry = OnRecoverableError(context, error);

                if (retry)
                {
                    int backoffTimeMs = PushClient.GetBackoff(context);
                    int nextAttempt   = backoffTimeMs / 2 + sRandom.Next(backoffTimeMs);

                    Log.Debug(TAG, "Scheduling registration retry, backoff = " + nextAttempt + " (" + backoffTimeMs + ")");

                    var retryIntent = new Intent(Constants.INTENT_FROM_GCM_LIBRARY_RETRY);
                    retryIntent.PutExtra(EXTRA_TOKEN, TOKEN);

                    var retryPendingIntent = PendingIntent.GetBroadcast(context, 0, retryIntent, PendingIntentFlags.OneShot);

                    var am = AlarmManager.FromContext(context);
                    am.Set(AlarmType.ElapsedRealtime, SystemClock.ElapsedRealtime() + nextAttempt, retryPendingIntent);

                    // Next retry should wait longer.
                    if (backoffTimeMs < MAX_BACKOFF_MS)
                    {
                        PushClient.SetBackoff(context, backoffTimeMs * 2);
                    }
                }
                else
                {
                    Log.Debug(TAG, "Not retrying failed operation");
                }
            }
            else
            {
                // Unrecoverable error, notify app
                OnError(context, error);
            }
        }