1

I am planning on implementing a reward system in my android application where users of the application can gain 100 points each day for opening the app. Users can only get 100 points at one given day no matter how many times more then once they open the app. I am not sure how I can go about doing this, for example, a application called WhatsChat, give users credits for opening the app each day:

enter image description here

I am trying to implement a similar system but instead only give users 100 points for opening the app each day. I understand I will need to keep track of the day and maybe store this in local storage using SharedPreferences and keep record of the dates of each day and then increments the variable that records points by 100.

In pseudo code it would look something like this:

Set CreditsGained to 0

IF app launched for the first time on Current date THEN
CreditsGained = CreditsGained + 100
Else 
Do nothing

How can I implement a such system in my application?

SumOne
  • 817
  • 3
  • 13
  • 24

4 Answers4

2

You do it on the server. The first time they make a network request every day, add 100 points to their total. There's no way to do this securely client side.

Gabe Sechan
  • 90,003
  • 9
  • 87
  • 127
  • I am making a prototype, currently I have no servers. So basically I want to implement this system using local storage for testing / user testing purposes. – SumOne Jan 20 '17 at 18:33
  • 1
    If you aren't making it for real, you don't have to make it right. Just write a job that gives them 100 points every day at midnight. – Gabe Sechan Jan 20 '17 at 18:41
  • But I want them to launch the app at least once a day to get the points – SumOne Jan 20 '17 at 18:42
1

I recommend you not use Shared Preferences to save data like you want. Cos they can modify it.

Only 1 way you can secure it, you have to have your own server to keep it. Local storage is not safe too.

But if you want to know how to check that, for studying, you can use Shared Preferences as well.

Step1: get your current time in string:

SimpleDateFormat sdf = new SimpleDateFormat("d-m-Y");
Calendar cal = Calendar.getInstance();
String dateInStr = sdf.format(cal.getTime());

Step2: Check at startup activity. After onCreate()

SharedPreferences pre=getSharedPreferences("my_sp", MODE_PRIVATE);
SharedPreferences.Editor edit=pre.edit();

if (pre.getBoolean(dateInStr, false)){
   //they checked today
}else{
  //not today
  // use check function here
  edit.putBoolean(dateInStr, true);
  edit.commit();
}

Okay, put step 1 code above step 2 code, i only separate it for easier understanding.

Step3: After you can check if they have check point more than 1 time or not. Let's add them 100points if that is the first time of the day they check out.

Put this inside else statement, above edit.putBoolean(dateInStr, true);

//get prev_score from SP
long previous_score = pre.getLong("score", 0);
//add 100
previous_score = previous_score + 100;
//save back to SP
edit.putLong("score", previous_score);
Harry T.
  • 3,478
  • 2
  • 21
  • 41
  • I am making a prototype, currently I have no servers. So basically I want to implement this system using local storage for testing / user testing purposes. – SumOne Jan 20 '17 at 18:33
  • @SumOne check my solution – Harry T. Jan 20 '17 at 18:42
  • Hi, code from step1, should I paste that in mainactivity? – SumOne Jan 20 '17 at 19:24
  • Also, will this give points to user only once per day, not matter if they open the app more then once? – SumOne Jan 20 '17 at 19:27
  • Please can you help me with what should be inside what roughly the if statement will do if false e.g. how can I add 100 credits to existing? – SumOne Jan 20 '17 at 19:34
  • Can I also check what this code is doing please (pre.getBoolean(dateInStr, false)) – SumOne Jan 20 '17 at 20:45
  • @SumOne put that code above SharedPreferces code in startup activity too. Yes, this code help you only give them 1 check point per day – Harry T. Jan 21 '17 at 04:27
  • What should I paste in the if statement? also the app closes but doesn't give any error. Any idea – SumOne Jan 21 '17 at 23:51
  • @SumOne if you want to do something if they open app more than 1 per day, put these methods inside if. If you get any error, first paste your code here first. – Harry T. Jan 23 '17 at 01:49
1

I may be late answering to your question..but for others seeking for the reward system and do not want the servers to handle this stuff -->

You can check whether the Automatic Date and time option is enabled or not in the user's device

**. Then according to the response, you can give reward to the user.

eg.

calendar= Calendar.getInstance();
year=calendar.get(Calendar.YEAR);
month=calendar.get(Calendar.MONTH);
day=calendar.get(Calendar.DAY_OF_MONTH);



  todaystring= year+ "" + month + "" + day + "";
   timepref=context.getSharedPreferences("REWARD",0);
   currentday=timepref.getBoolean(todaystring,false);


 //Daily reward
        if (!currentday && isZoneAutomatic(context) && isTimeAutomatic(context)) { //currentday =false
            btnrwrd.setEnabled(true);
            btnrwrd.setText("Collect your daily reward!");
            btnrwrd.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Toast.makeText(context, "Daily reward granted!", Toast.LENGTH_SHORT).show();
                   // Do your stuff here
                    // saving the date
                    SharedPreferences sharedPreferences = context.getSharedPreferences("SAVING", Context.MODE_PRIVATE);
                    SharedPreferences.Editor edt = sharedPreferences.edit();
                    edt.putInt("mypoints", Integer.valueOf(points.getText().toString()));
                    edt.apply();
                    Toast.makeText(context, String.valueOf(daily), Toast.LENGTH_SHORT).show();
                    SharedPreferences.Editor timedaily = timepref.edit();
                    timedaily.putBoolean(todaystring, true);
                    timedaily.apply();
                    btnrwrd.setText("Wait for 24 hrs");
                    btnrwrd.setEnabled(false);
                }
            });
}
else {
            Toast.makeText(context, "Your daily reward is over!", Toast.LENGTH_SHORT).show();
        }


    public static boolean isTimeAutomatic(Context c) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
            return Settings.Global.getInt(c.getContentResolver(), Settings.Global.AUTO_TIME, 0) == 1;
        } else {
            return android.provider.Settings.System.getInt(c.getContentResolver(), android.provider.Settings.System.AUTO_TIME, 0) == 1;
        }
    }

    public static boolean isZoneAutomatic(Context c) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
            return Settings.Global.getInt(c.getContentResolver(), Settings.Global.AUTO_TIME_ZONE, 0) == 1;
        } else {
            return android.provider.Settings.System.getInt(c.getContentResolver(), android.provider.Settings.System.AUTO_TIME, 0) == 1;
        }
    }

I hope it helps you! Upvote the answer and Good luck :)

1

Whenever the user opens the app, check if any last credit date is available in the local storage and if it does, verify the current date is one more than the stored date, then you credit the user for next day.

If the date is not available in storage, then you can assume it's the first day. And yes you can do it in MainActivity for this use case because you are not making any time consuming api calls.

I just shared the idea, figure out the code. It's easy.