1

I wanted to make daily automatically notification on saved by my time.

I've got in main activity onCreate:

Calendar calendar = Calendar.getInstance();
    calendar.set(Calendar.HOUR_OF_DAY, 16);
    calendar.set(Calendar.MINUTE, 35);
    calendar.set(Calendar.SECOND, 0);
    Intent intent1 = new Intent(ActivitySplashScreen.this, AlarmReceiver.class);
    PendingIntent pendingIntent = PendingIntent.getBroadcast(ActivitySplashScreen.this, 0,intent1, PendingIntent.FLAG_UPDATE_CURRENT);
    AlarmManager am = (AlarmManager) ActivitySplashScreen.this.getSystemService(ActivitySplashScreen.this.ALARM_SERVICE);
    am.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), AlarmManager.INTERVAL_DAY, pendingIntent);

And my AlarmReceiver:

public class AlarmReceiver extends BroadcastReceiver{

    @Override
    public void onReceive(Context context, Intent intent) {
        // TODO Auto-generated method stub

        long when = System.currentTimeMillis();
        NotificationManager notificationManager = (NotificationManager) context
                .getSystemService(Context.NOTIFICATION_SERVICE);

        Intent notificationIntent = new Intent(context, ActivitySplashScreen.class);
        notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

        PendingIntent pendingIntent = PendingIntent.getActivity(context, 0,
                notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);


        Uri alarmSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);

        NotificationCompat.Builder mNotifyBuilder = new NotificationCompat.Builder(
                context).setSmallIcon(R.drawable.ic_invite_friends)
                .setContentTitle("Yeah!")
                .setContentText("Do not forget to earn zeni!")
                .setSound(alarmSound)
                .setAutoCancel(true).setWhen(when)
                .setContentIntent(pendingIntent)
                .setVibrate(new long[]{1000, 1000, 1000, 1000, 1000});
        notificationManager.notify((int) ((new Date().getTime() / 1000L) % Integer.MAX_VALUE), mNotifyBuilder.build());


    }

}

The problem is, that this is showing everytime on app is creating and only in that day when the user open it. When the user not open app in next day it's not showing.

OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
dawidurusek
  • 113
  • 1
  • 1
  • 11
  • The code looks fine. I tried it with a shorter span (5 minute gap between 2 alarm instances) and it seems to work even when the App is not running and the device is in sleep mode. This is from Android doc - _Note: as of API 19, all repeating alarms are inexact. If your application needs precise delivery times then it must use one-time exact alarms, rescheduling each time as described above. Legacy applications whose targetSdkVersion is earlier than API 19 will continue to have all of their alarms, including repeating alarms, treated as exact._ In short it's totally upto OS to decide. – Dibzmania Jan 12 '17 at 23:32
  • One another thing with longer duration is that you have to consider the possibility that the user might have restarted the phone in that interval. Not all alarms survive device restarts (please read upon the Android behaviour on this). See this Q&A on stack - [link](http://stackoverflow.com/questions/12034357/does-alarm-manager-persist-even-after-reboot) . In such case as soon as user launches your app after a reboot, the alarm is scheduled and if it is already past the time of the day (as mentioned in your alarm creation) , the alarm would trigger immediately – Dibzmania Jan 12 '17 at 23:37

1 Answers1

0
  Calendar cal = Calendar.getInstance();
            cal.set(Calendar.HOUR_OF_DAY, 1);
            cal.set(Calendar.MINUTE, 00);



            Calendar cur_cal = new GregorianCalendar();
            cur_cal.setTimeInMillis(System.currentTimeMillis());//set the current time and date for this calendar


            Calendar cal = new GregorianCalendar();
            cal.add(Calendar.DAY_OF_YEAR, cur_cal.get(Calendar.DAY_OF_YEAR));
            cal.set(Calendar.HOUR_OF_DAY, 00);
            cal.set(Calendar.MINUTE, 02);
            cal.set(Calendar.SECOND, 0);
            cal.set(Calendar.MILLISECOND, 0);
            cal.set(Calendar.DATE, cur_cal.get(Calendar.DATE) + 1);
            cal.set(Calendar.MONTH, cur_cal.get(Calendar.MONTH));


            //Create a new PendingIntent and add it to the AlarmManager
            Intent intent = new Intent(getContext(), YourCustomBroadcast.class);
            PendingIntent pendingIntent = PendingIntent.getBroadcast(getContext(),
                    12345, intent, PendingIntent.FLAG_CANCEL_CURRENT);

            AlarmManager am =
                    (AlarmManager) getContext().getSystemService(Activity.ALARM_SERVICE);
            am.cancel(pendingIntent);
            am.setInexactRepeating(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), AlarmManager.INTERVAL_DAY, pendingIntent);

try using this code, provide your exact time of daily notification in Calendar instance create your own custom Broadcast class, like this

public class YourCustomBroadcast extends BroadcastReceiver {
private final String SOMEACTION = "packagename.ACTION";
public static int notId = 1;

@Override
public void onReceive(Context context, Intent intent) {

    Log.d("inside broadcast", "onReceive: ");

    //Toast.makeText(context, "call again", Toast.LENGTH_SHORT).show();

    Calendar c = Calendar.getInstance();
    c.setTimeInMillis(System.currentTimeMillis());


    NotificationCompat.Builder mBuilder =
            (NotificationCompat.Builder) new NotificationCompat.Builder(context)
                    .setSmallIcon(android.R.drawable.sym_def_app_icon)
                    .setContentTitle("Day changed")
                    .setContentText("" + c.getTime().toString());

    NotificationManager mNotificationManager =

            (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);


    mNotificationManager.notify(++notId, mBuilder.build());



}

}

Milind Mevada
  • 3,145
  • 1
  • 14
  • 22