I am creating a school application in which when i visit there for marketing i have to submit a report every time. If visit is Successful then there is no issue if visit is unsuccessful then i have to reschedule my meeting for next visit. When i set date and time for my next meeting it should remind me in early morning by sending a notification and it should alarm at that time so i can get reminder. here is my code for report Activity please help me
private void sendNotificationDetailsTwo() { /* 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, 30); // Particular minute firingCal.set(Calendar.SECOND, 0); // particular second Long intendedTime = firingCal.getTimeInMillis(); if (firingCal.compareTo(currentCal) < 0) { firingCal.add(Calendar.DAY_OF_MONTH, 1); } else { for (int i = 0; i < 10; ++i) { Intent newIntentTwo = new Intent(Report_Activity.this, MyReceiver.class); final int intent_id_two = (int) System.currentTimeMillis(); newIntentTwo.setFlags(Intent.FLAG_ACTIVITY_MULTIPLE_TASK); Bundle bundle = new Bundle(); bundle.putString("date", str_view_date); bundle.putString("time", str_view_time); bundle.putString("school_name", str_show_school_name); bundle.putInt("intent_id", intent_id_two); newIntentTwo.putExtras(bundle); AlarmManager alarmManager = (AlarmManager) Report_Activity.this.getSystemService(Report_Activity.this.ALARM_SERVICE); PendingIntent pendingIntent = PendingIntent.getBroadcast(Report_Activity.this, intent_id_two, newIntentTwo, PendingIntent.FLAG_ONE_SHOT); alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, intendedTime, AlarmManager.INTERVAL_DAY, pendingIntent); } }*/ /*alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE); alarmIntent = new Intent(context of current file, AlarmReceiver1.class); // AlarmReceiver1 = broadcast receiver pendingIntent = PendingIntent.getBroadcast( Menu.this, 0, alarmIntent, PendingIntent.FLAG_UPDATE_CURRENT); alarmIntent.setData((Uri.parse("custom://"+System.currentTimeMillis()))); alarmManager.cancel(pendingIntent); Calendar alarmStartTime = Calendar.getInstance(); Calendar now = Calendar.getInstance(); alarmStartTime.set(Calendar.HOUR_OF_DAY, 8); alarmStartTime.set(Calendar.MINUTE, 00); alarmStartTime.set(Calendar.SECOND, 0); if (now.after(alarmStartTime)) { Log.d("Hey","Added a day"); alarmStartTime.add(Calendar.DATE, 1); } alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, alarmStartTime.getTimeInMillis(), AlarmManager.INTERVAL_DAY, pendingIntent); Log.d("Alarm","Alarms set for everyday 8 am.");*/ Calendar firingCal = Calendar.getInstance(); Calendar now = Calendar.getInstance(); firingCal.set(Calendar.HOUR_OF_DAY,15); firingCal.set(Calendar.MINUTE,25); firingCal.set(Calendar.SECOND,0); while(now.getTimeInMillis()>firingCal.getTimeInMillis()){ firingCal.add(Calendar.DATE,1); } Intent myInten = new Intent(Report_Activity.this,MyReceiver.class); final int intent_id_two = (int) System.currentTimeMillis(); myInten.setFlags(Intent.FLAG_ACTIVITY_MULTIPLE_TASK); Bundle bundle = new Bundle(); bundle.putString("date", str_view_date); bundle.putString("time", str_view_time); bundle.putString("school_name", str_show_school_name); bundle.putInt("intent_id", intent_id_two); myInten.putExtras(bundle); //final int intent_id_two = (int) System.currentTimeMillis(); PendingIntent myPendingIntent = PendingIntent.getBroadcast(Report_Activity.this,intent_id_two,myInten,PendingIntent.FLAG_UPDATE_CURRENT); AlarmManager alarmManager = (AlarmManager)Report_Activity.this.getSystemService(Report_Activity.ALARM_SERVICE); alarmManager.setRepeating(AlarmManager.RTC_WAKEUP,firingCal.getTimeInMillis(),2000,myPendingIntent); }
MyAlarmService.java
public class MyAlarmService extends Service { private NotificationManager notificationManager; @Nullable @Override public IBinder onBind(Intent intent) { return null; } @Override public void onCreate() { super.onCreate(); boolean alarm =(PendingIntent.getBroadcast(this,0,new Intent("Alarm"),PendingIntent.FLAG_NO_CREATE)==null); if(alarm){ Intent mintent =new Intent("Alarm"); PendingIntent mpendingIntent = PendingIntent.getBroadcast(this,0,mintent,0); Calendar mcalender = Calendar.getInstance(); mcalender.setTimeInMillis(System.currentTimeMillis()); mcalender.add(Calendar.SECOND,3); AlarmManager malarmmanager = (AlarmManager)getSystemService(ALARM_SERVICE); malarmmanager.setRepeating(AlarmManager.RTC_WAKEUP,mcalender.getTimeInMillis(),6000,mpendingIntent); } } }
MyReceiver.java
public class MyReceiver extends BroadcastReceiver { int MID = 0; int m = (int) ((new Date().getTime() / 1000L) % Integer.MAX_VALUE); String date, time, school_name; PendingIntent pendingIntent; int intent_id; @Override public void onReceive(Context context, Intent intent) { // long when = System.currentTimeMillis(); Bundle bundle = intent.getExtras(); date = bundle.getString("date"); time = bundle.getString("time"); school_name = bundle.getString("school_name"); intent_id = bundle.getInt("intent_id"); NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE); Intent notificationIntent = new Intent(context, MainActivity.class); notificationIntent.setFlags(Intent.FLAG_ACTIVITY_MULTIPLE_TASK); //final int intent_id = (int) System.currentTimeMillis(); pendingIntent = PendingIntent.getActivity(context, intent_id, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT); Uri alarmSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION); NotificationCompat.Builder builder = (NotificationCompat.Builder) new NotificationCompat.Builder(context) .setContentTitle("Notification From Dextro") .setContentText("Meeting Schedule at " + school_name + " on " + date + " and time is " + time) .setSmallIcon(R.mipmap.dextro_customerlogo) .setContentIntent(pendingIntent) .setAutoCancel(true) .setSound(alarmSound) .setTicker("Notification From Dextro") .setStyle(new NotificationCompat.BigTextStyle() .bigText("Meeting Schedule at " + school_name + " on " + date + " and time is " + time)) .setVibrate(new long[]{100, 100, 100, 100}); notificationManager.notify(m, builder.build()); MID++; } }
1. In Simple way When i Reschedule my Work it should get notify me on next morning with alarm