Exemplo n.º 1
0
        /// <summary>
        /// Notify when the application identifier is known. </summary>
        /// <param name="appId"> application identifier. </param>
        public virtual void onAppIdGot(string appId)
        {
            /* Keep identifier */
            mAppId = appId;

            /* Send pending token if any */
            string value = mStorage.getString(NEW_TOKEN_VALUE, null);

            EngagementNativePushToken.Type type = typeFromInt(mStorage.getInt(NEW_TOKEN_TYPE, -1));
            if (value != null && type != null)
            {
                registerNativePush(new EngagementNativePushToken(value, type));
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// Calls <seealso cref="EngagementAgent#registerNativePush(EngagementNativePushToken)"/> only if the token
        /// is a new one or some time has elapsed since the last time we sent it. </summary>
        /// <param name="token"> token to register. </param>
        public virtual void registerNativePush(EngagementNativePushToken token)
        {
            /* If application identifier is unknown */
            if (mAppId == null)
            {
                /*
                 * Keep state until onAppIdGot is called. Possibly in the next application launch so it must
                 * be persisted.
                 */
                SharedPreferences.Editor edit = mStorage.edit();
                edit.putString(NEW_TOKEN_VALUE, token.Token);
                edit.putInt(NEW_TOKEN_TYPE, token.getType().ordinal());
                edit.commit();
            }
            else
            {
                /* Get state */
                string oldAppId      = mStorage.getString(APP_ID, null);
                string oldTokenValue = mStorage.getString(TOKEN_VALUE, null);
                EngagementNativePushToken.Type oldType = EngagementNativePushToken.typeFromInt(mStorage.getInt(TOKEN_TYPE, -1));
                long sent             = mStorage.getLong(SENT, 0);
                long elapsedSinceSent = DateTimeHelperClass.CurrentUnixTimeMillis() - sent;

                /* If registrationId changed or enough time elapsed since we sent it */
                if (oldAppId == null || !oldAppId.Equals(mAppId) || oldTokenValue == null || !oldTokenValue.Equals(token.Token) || oldType == null || !oldType.Equals(token.getType()) || elapsedSinceSent >= SENT_EXPIRY)
                {
                    /* Send registration identifier */
                    mEngagementAgent.registerNativePush(token);

                    /* Update state */
                    SharedPreferences.Editor edit = mStorage.edit();
                    edit.clear();
                    edit.putString(APP_ID, mAppId);
                    edit.putString(TOKEN_VALUE, token.Token);
                    edit.putInt(TOKEN_TYPE, token.getType().ordinal());
                    edit.putLong(SENT, DateTimeHelperClass.CurrentUnixTimeMillis());
                    edit.commit();
                }
            }
        }