21

I am using AlarmManager() to fire notification and repeat it at every 24 hours.

My code is on onCreate() in Splash Activity which fires first when anyone opens App. User can install App at anytime. So I want that when User installs App, It checks for the timing and then fires Notification at 8 AM and repeat it daily. I don't want notification when anyone opens App.

My code is as below :

public class Splash extends Activity {

final String WebsiteURL = "http://www.mytestbuddy.com";

String cookie;

@SuppressLint("SimpleDateFormat")
@Override
protected void onCreate(Bundle showSplash) {
    // TODO Auto-generated method stub
    super.onCreate(showSplash);
    setContentView(R.layout.splash);

    getWindow().getDecorView().setBackgroundColor(Color.WHITE);

    Intent myIntent = new Intent(Splash.this, AlarmReceiver.class);

    final PendingIntent pendingIntent = PendingIntent.getBroadcast(
            Splash.this, 0, myIntent, 0);

    final AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);

    CookieSyncManager.createInstance(this);
    CookieManager cm = CookieManager.getInstance();
    cm.setAcceptCookie(true);
    CookieSyncManager.getInstance().sync();
    if (cm.getCookie("" + WebsiteURL + "") != null) {
        cookie = cm.getCookie("" + WebsiteURL + "").toString();
    } else {
        cookie = null;
    }

    Thread timer = new Thread() {
        public void run() {
            try {
                sleep(5000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            } finally {
                if (cookie == null) {
                    Intent openStartingPoint = new Intent(
                            "com.MobileWeb.mytestbuddy.Login");
                    startActivity(openStartingPoint);
                } else if (cookie.contains("Premium")) {

                    Calendar firingCal = Calendar.getInstance();
                    Calendar currentCal = Calendar.getInstance();

                    firingCal.set(Calendar.HOUR, 10);
                    firingCal.set(Calendar.MINUTE, 30);
                    firingCal.set(Calendar.SECOND, 0);

                    long intendedTime = firingCal.getTimeInMillis();
                    long currentTime = currentCal.getTimeInMillis();

                    if (intendedTime >= currentTime) {
                        alarmManager.setRepeating(AlarmManager.RTC,
                                intendedTime, AlarmManager.INTERVAL_DAY,
                                pendingIntent);

                    } else {
                        firingCal.add(Calendar.DAY_OF_MONTH, 1);
                        intendedTime = firingCal.getTimeInMillis();

                        alarmManager.setRepeating(AlarmManager.RTC,
                                intendedTime, AlarmManager.INTERVAL_DAY,
                                pendingIntent);
                    }

                    Intent openStartingPoint = new Intent(
                            "com.MobileWeb.mytestbuddy.PremiumMain");
                    startActivity(openStartingPoint);
                } else {

                    Calendar firingCal = Calendar.getInstance();
                    Calendar currentCal = Calendar.getInstance();

                    firingCal.set(Calendar.HOUR, 10);
                    firingCal.set(Calendar.MINUTE, 30);
                    firingCal.set(Calendar.SECOND, 0);

                    long intendedTime = firingCal.getTimeInMillis();
                    long currentTime = currentCal.getTimeInMillis();

                    if (intendedTime >= currentTime) {
                        alarmManager.setRepeating(AlarmManager.RTC,
                                intendedTime, AlarmManager.INTERVAL_DAY,
                                pendingIntent);

                    } else {
                        firingCal.add(Calendar.DAY_OF_MONTH, 1);
                        intendedTime = firingCal.getTimeInMillis();

                        alarmManager.setRepeating(AlarmManager.RTC,
                                intendedTime, AlarmManager.INTERVAL_DAY,
                                pendingIntent);
                    }

                    Intent openStartingPoint = new Intent(
                            "com.MobileWeb.mytestbuddy.Main");
                    startActivity(openStartingPoint);
                }
            }
        }
    };

    timer.start();
}

}

Jeeten Parmar
  • 403
  • 2
  • 6
  • 16
  • The title should be like "Repeat Notification every day on specific time in Android with" so If any one google it and find it. – Pratik Butani Oct 18 '18 at 05:12

4 Answers4

32

Do as chintan suggested. To get a clear picture, the exact solution might look something similar to the below:

Intent myIntent = new Intent(Splash.this, AlarmReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(Splash.this, 0, myIntent, 0);
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);

Calendar firingCal= Calendar.getInstance();
Calendar currentCal = Calendar.getInstance();

firingCal.set(Calendar.HOUR, 8); // At the hour you wanna fire
firingCal.set(Calendar.MINUTE, 0); // Particular minute
firingCal.set(Calendar.SECOND, 0); // particular second

long intendedTime = firingCal.getTimeInMillis();
long currentTime = currentCal.getTimeInMillis();

if(intendedTime >= currentTime){ 
   // you can add buffer time too here to ignore some small differences in milliseconds
   // set from today
   alarmManager.setRepeating(AlarmManager.RTC, intendedTime, AlarmManager.INTERVAL_DAY, pendingIntent);
} else{
   // set from next day
   // you might consider using calendar.add() for adding one day to the current day
   firingCal.add(Calendar.DAY_OF_MONTH, 1);
   intendedTime = firingCal.getTimeInMillis();

   alarmManager.setRepeating(AlarmManager.RTC, intendedTime, AlarmManager.INTERVAL_DAY, pendingIntent);
}
zeeali
  • 1,524
  • 20
  • 31
Kanth
  • 6,681
  • 3
  • 29
  • 41
  • well, I am very new to android development and this notification is giving me headache. Should I copy paste my code directly in else part or what ??? Please tell me. – Jeeten Parmar Jun 01 '13 at 09:38
  • @JeetenParmar You have got any issues? If so, post back. BTW you might need to accept one of our answers, if it helped you. – Kanth Jun 01 '13 at 09:40
  • when i assign HOUR - 15, MINUTE - 50 in firingCal.set. It dint fire notification, but when i set it as hour - 3, it fired. mobile timing is 12 hours based. Timing set is based on user. So what to do for that ? – Jeeten Parmar Jun 01 '13 at 10:23
  • What do you mean by 15? Actually the milliseconds are used in setting an alarm with alarm manager. Or please elaborate it clearly. – Kanth Jun 01 '13 at 16:07
  • @Appu... your code worked fine. But I got notification 3 times. What to do for it ? – Jeeten Parmar Jun 03 '13 at 06:48
  • @JeetenParmar Post back your full code by editing your question. – Kanth Jun 03 '13 at 08:37
  • I don't think I have found any flaw in your code but some redundancy. When do you get notification 3 times? Did you debug your code? – Kanth Jun 03 '13 at 10:21
  • it was nite time. and next day, i dint get any notification. I want to fire notification daily. – Jeeten Parmar Jun 04 '13 at 06:39
  • Use Calendar.HOUR_OF_DAY instead of Calendar.HOUR if you are setting it in 24 hour format. – Cadrick Loh Jan 27 '14 at 08:28
  • But where is notification message I also want to do same thing please explain – Neo Mar 07 '14 at 03:36
10

Here is pseudocode :

You have to write this code inside your Splash.java

Step 1 : Is_Alarm_Set(); [Boolean]

Step 2 : false [step 3]

Step 2 : true [step 8] (No need to set)

Step 3 : Get_Time() [User's Current Mobile Time]

Step 4 : Find_Time_Difference() [This function will find difference between user's mobile time and your Fix Time (8AM).]

Step 5 : Now set your alarm as per time difference.[i.e current time is 7 pm and date is 1-june then set alarm of 8 AM for next day.]

Step 6 : Set your repetition days setRepeating()

Step 7 : It will fire alarm as per your fix time 8 AM.

Step 8 : Switch Your Activity.

Chintan Khetiya
  • 15,962
  • 9
  • 47
  • 85
2

Appu answer helped me a lot but if you want a fixed version of it a bit more readable and shorted look at this:

PendingIntent pendingIntent = PendingIntent.getBroadcast(Splash.this, 0, myIntent, 0);
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
Calendar firingCal= Calendar.getInstance();
Calendar currentCal = Calendar.getInstance();

firingCal.set(Calendar.HOUR_OF_DAY, 8); // At the hour you wanna fire
firingCal.set(Calendar.MINUTE, 0); // Particular minute
firingCal.set(Calendar.SECOND, 0); // particular second

if(firingCal.compareTo(currentCal) < 0) { 
   firingCal.add(Calendar.DAY_OF_MONTH, 1);
} 
Long intendedTime = firingCal.getTimeInMillis();
alarmManager.setRepeating(AlarmManager.RTC, intendedTime , AlarmManager.INTERVAL_DAY, pendingIntent);
pescamillam
  • 131
  • 2
  • 8
2
// Retrieve a PendingIntent that will perform a broadcast
            Intent alarmIntent = new Intent(HomeContactActivity.this,
                    AlarmReceiver.class);
            pendingIntent = PendingIntent.getBroadcast(
                    HomeContactActivity.this, 0, alarmIntent, 0);

            AlarmManager manager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);

            // Set the alarm to start at 10:00 AM
            Calendar calendar = Calendar.getInstance();
            calendar.setTimeInMillis(System.currentTimeMillis());
            calendar.set(Calendar.HOUR_OF_DAY, 10);
            calendar.set(Calendar.MINUTE, 0);
            calendar.set(Calendar.SECOND, 0);




            manager.setRepeating(AlarmManager.RTC_WAKEUP,
                    calendar.getTimeInMillis(), 86400000, // for repeating
                                                            // in every 24
                                                            // hours
                    pendingIntent);
Pratibha Sarode
  • 1,819
  • 17
  • 17
  • 2
    Instead of using 86400000, using AlarmManager.INTERVAL_DAY is better. – ilkayaktas Dec 13 '17 at 18:38
  • i used the same above code but alaram is not triggering on current date ..if i change the date it triggers..i have posted question as well https://stackoverflow.com/questions/50577574/alarm-is-not-triggering-on-the-same-date – Gaju Kollur May 30 '18 at 06:53