I am developing an application where the user may schedule training date and time. The user may schedule multiple training dates and times while using the application. Say for example the user will schedule two training, TR1 and TR2. When the TR1 date and time matches, a notification will be sent to the user and onclicking the notification the user will be directed to the Training Activity. The same will happen for TR2 on reaching the date and time of the training.
For scheduling the notifications, I am using the AlarmManager. The problem which I am facing is that, if I schedule multiple training, I am getting notified for only the last one.
The activity for generating the notifications:
public class SettingsActivity extends Activity {
//objects for use
DateFormat fmtDateAndTime=DateFormat.getDateTimeInstance();
TextView dateAndTimeLabel;
Calendar dateAndTime=Calendar.getInstance();
ImageView confirm;
String dateTime;
EditText duration;
//Alarm manager object for notification
AlarmManager am;
//notification date and time
int hour;
int minute;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_settings_portrait);
duration=(EditText)findViewById(R.id.duration_editText);
dateAndTimeLabel=(TextView)findViewById(R.id.timeTxt);
updateLabel();
updateNotification();
am = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
// On Click Confirm
confirm=(ImageView)findViewById(R.id.confirm_imageView);
confirm.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
getDateTime();
setOneTimeAlarm();
}
});
}
//date picker
public void chooseDate(View v) {
new DatePickerDialog(SettingsActivity.this, d,
dateAndTime.get(Calendar.YEAR),
dateAndTime.get(Calendar.MONTH),
dateAndTime.get(Calendar.DAY_OF_MONTH))
.show();
}
//time picker
public void chooseTime(View v) {
new TimePickerDialog(SettingsActivity.this, t,
dateAndTime.get(Calendar.HOUR_OF_DAY),
dateAndTime.get(Calendar.MINUTE),
true)
.show();
}
private void updateLabel() {
dateAndTimeLabel.setText(fmtDateAndTime
.format(dateAndTime.getTime()));
}
DatePickerDialog.OnDateSetListener d=new DatePickerDialog.OnDateSetListener() {
public void onDateSet(DatePicker view, int year, int monthOfYear,
int dayOfMonth) {
dateAndTime.set(Calendar.YEAR, year);
dateAndTime.set(Calendar.MONTH, monthOfYear);
dateAndTime.set(Calendar.DAY_OF_MONTH, dayOfMonth);
updateLabel();
updateNotification();
}
};
TimePickerDialog.OnTimeSetListener t=new TimePickerDialog.OnTimeSetListener() {
public void onTimeSet(TimePicker view, int hourOfDay,
int minute) {
dateAndTime.set(Calendar.HOUR_OF_DAY, hourOfDay);
dateAndTime.set(Calendar.MINUTE, minute);
updateLabel();
updateNotification();
}
};
// Notification Function
// private void Notification(String notificationTitle, String notificationMessage)
// {
// NotificationManager notificationManager = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
// Notification notification = new Notification(R.drawable.ic_launcher, "Training Scheduled!", System.currentTimeMillis());
// //on click notification goto dinnerden activity
// Intent notificationIntent = new Intent(this, SettingsActivity.class);
// PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
//
// notification.setLatestEventInfo(SettingsActivity.this, notificationTitle, notificationMessage, pendingIntent);
// notificationManager.notify(10001, notification);
// }
//getting date time from textview
void getDateTime()
{
dateTime= dateAndTimeLabel.getText().toString()+" Duration HRS: "+duration.getText().toString();
makeAToast(dateTime);
//Notification("Training Scheduled at:",dateTime);
}
void updateNotification()
{
hour= dateAndTime.get(Calendar.HOUR_OF_DAY);
minute=dateAndTime.get(Calendar.MINUTE);
}
public void setOneTimeAlarm() {
// the time set
Calendar calendar = Calendar.getInstance();
// hour= dateAndTime.get(Calendar.HOUR_OF_DAY);
// minute=dateAndTime.get(Calendar.MINUTE);
// int hour= dateAndTime.get(Calendar.HOUR_OF_DAY);
// int minute=dateAndTime.get(Calendar.MINUTE);
// noon tomorrow
//calendar.add(Calendar.DAY_OF_YEAR, 1);
calendar.set(Calendar.HOUR_OF_DAY, hour);
calendar.set(Calendar.MINUTE, minute);
calendar.set(Calendar.SECOND, 0);
Intent intent = new Intent(this, TimeAlarm.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0,
intent, PendingIntent.FLAG_ONE_SHOT);
am.set(AlarmManager.RTC_WAKEUP,
calendar.getTimeInMillis(), pendingIntent);
}
public void setRepeatingAlarm() {
Intent intent = new Intent(this, TimeAlarm.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0,
intent, PendingIntent.FLAG_CANCEL_CURRENT);
am.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(),
(5 * 1000), pendingIntent);
}
public void makeAToast(String str) {
Toast toast = Toast.makeText(this,str, Toast.LENGTH_LONG);
toast.setGravity(Gravity.BOTTOM, 0, 0);
toast.setDuration(1000000);
toast.show();
}
}
The class for creating the notifications:
public class TimeAlarm extends BroadcastReceiver {
NotificationManager nm;
@Override
public void onReceive(Context context, Intent intent) {
nm = (NotificationManager) context
.getSystemService(Context.NOTIFICATION_SERVICE);
CharSequence from = "Scheduled Training";
CharSequence message = "Please attend the scheduled training";
// Intent welcomeIntent = new Intent(context, CustomAlarm.class);
PendingIntent contentIntent = PendingIntent.getActivity(context, 0,
new Intent(), 0);
Notification notif = new Notification(R.drawable.ic_launcher,
"Crazy About Android...", System.currentTimeMillis());
notif.setLatestEventInfo(context, from, message, contentIntent);
nm.notify(1, notif);
}
}
The layout for the activity:
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="488dp" >
<ImageView
android:id="@+id/confirm_imageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:src="@drawable/schedule_button" />
<TextView
android:id="@+id/timeTxt"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_marginTop="41dp"
android:text="@string/time_show" />
<Button
android:id="@+id/timeBtn"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/timeTxt"
android:layout_marginTop="34dp"
android:onClick="chooseTime"
android:text="@string/time" />
<Button
android:id="@+id/dateBtn"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/timeBtn"
android:layout_marginTop="38dp"
android:onClick="chooseDate"
android:text="@string/date" />
<EditText
android:id="@+id/duration_editText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_below="@+id/dateBtn"
android:layout_marginTop="36dp"
android:ems="10"
android:hint="@string/duration_hint"
android:inputType="number" />
<TextView
android:id="@+id/duration_textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/duration_editText"
android:layout_alignBottom="@+id/duration_editText"
android:layout_alignParentLeft="true"
android:text="@string/duration"
android:textAppearance="?android:attr/textAppearanceMedium" />
</RelativeLayout>
</LinearLayout>
</ScrollView>
Part of manifest.xml:
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<receiver android:name="com.project.nuroq.android.app.TimeAlarm" />
I want to fire the notifications at particular time(implemented) and dates(yet to implement) which is not happening now. The last is only getting fired!
Where am I going wrong? Thanks in advance!
You have to use different ID for each notification. If, as you do, all would use the same ID value, then, according to notify() docs, previous one gets replaced with new one: