Notification Message Instantly Fired - Android - android

I'm making an app that stores medicines data in an SQLite database in order to send to the user notifications when it's time to take them.
I already created the BroadcastReceiver class and managed the notification Intent.
The Calendar.set() function is called when I add the time (hh:mm:ss) in the database but the problem is that every time I set the time in the TimePicker dialog, the notification is sent instantly, at regardless from time.
Here is the setAlarm function from the activity where I store the time and the other stuff:
public void setAlarm()
{
String mName = NameFld.getText().toString();
String mFormat = FormatSpn.getSelectedItem().toString();
Calendar calendar = Calendar.getInstance();
char[] sTime = TimeBtn.getText().toString().toCharArray();
if(sTime[0] == '0')
{
calendar.set(Calendar.HOUR_OF_DAY, sTime[1]);
}
else
{
String tmp = "";
tmp += sTime[0];
tmp += sTime[1];
int hour = Integer.parseInt(tmp);
calendar.set(Calendar.HOUR_OF_DAY, hour);
}
if(sTime[3] == '0')
{
calendar.set(Calendar.MINUTE, sTime[4]);
}
else
{
String tmp = "";
tmp += sTime[3];
tmp += sTime[4];
int minute = Integer.parseInt(tmp);
calendar.set(Calendar.MINUTE, minute);
}
calendar.set(Calendar.SECOND, 0);
Intent intent = new Intent(this, AlarmReceiver.class);
intent.putExtra("mName", mName);
intent.putExtra("mFormat", mFormat);
sendBroadcast(intent);
PendingIntent pIntent = PendingIntent.getBroadcast(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager aManager = (AlarmManager) getSystemService(ALARM_SERVICE);
aManager.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pIntent);
The two lines of code below:
String mName = NameFld.getText().toString();
String mFormat = FormatSpn.getSelectedItem().toString();
Just takes data from the EditText fields then put in an Intent to manage them in the notification building.
In order to set the time to the Calendar variable, I take the text from the TimeBtn button that consists in the time string itself. I just set it when I pick the time from the TimePicker dialog.
Then I cast it in a char array in order to split hour and minute values and I put them in the calendar.set() function, distinguishing if the value starts with 0 to avoid an octal conversion when I cast them to int.
Once the time has been set, the AlarmReceiver class (extends BroadcastReceiver) does the following:
#Override
public void onReceive(Context context, Intent intent) {
String mName = intent.getStringExtra("mName");
String mFormat = intent.getStringExtra("mFormat");
NotificationCompat.Builder builder = new NotificationCompat.Builder(context);
builder.setAutoCancel(true);
builder.setTicker("It's pill time!");
builder.setContentTitle(mName);
builder.setContentText(mFormat);
builder.setSmallIcon(R.drawable.ic_launcher);
Notification notification = builder.build();
NotificationManager nManager = (NotificationManager) context.getSystemService(context.NOTIFICATION_SERVICE);
nManager.notify(0, notification);
}
There aren't compilation errors, I just can't spot the issue.
Thanks in advance for any help!

Try replacing
aManager.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pIntent);
with
aManager.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis()+calendar.getTimeInMillis(), pIntent);

Related

how to set multiple alarms using sqlite data?

I am new to android ,here I am developing an alarm app for my working knowledge .
I have completed the following :
creating alarms and storing it into sqlite database.
Fetching all the alarms which has the status as active .
I have tried many stackoverflow post and their solutions and other blog posts which related to my doubt but I can't get a solution for my problem .
What is my problem is I am receiving number of alarm timings from sqlite database which I have set it before and I want to set all the alarms on the stored time .
Here I don't know how to set it .
Can anyone help me to set the multiple alarms .
I am really looking for someone's help to learn and experience these things please help me .
Thanks.
You need Alarm Manager and Pending Intent more.
for (int i = 0; i < ActivemyAlarms.size(); i++) {
int mHour = 0,mMin=0;
String amPm = null;
int mAlarmId = ActivemyAlarms.get(i).getALARM_ID(); //each alarm has an unique Id ,for differentiate one from another
String mAlarmTime = ActivemyAlarms.get(i).getALARM_TIME(); // alarm time (11:12:AM)
if (!(mAlarmTime == null)) {
String mtime = mAlarmTime; // alarm time format is 12hr format (ex : 11:12:AM)
String[] time = mtime.split(":");
mHour = Integer.parseInt(time[0].trim()); // get 11 hour
mMin = Integer.parseInt(time[1].trim()); // get 12 min
amPm = ((time[2].trim()));
}
Calendar calendar = Calendar.getInstance();
calendar.set(calendar.HOUR_OF_DAY, amPm!=null && amPm.equalsIgnoreCase("pm")?(mHour+12):mHour);
calendar.set(calendar.MINUTE, mMin);
calendar.set(calendar.SECOND, 0);
calendar.set(calendar.MILLISECOND, 0);
Intent intent = new Intent(this,AlarmReceiver.class); //calling my Alarm service class which plays a music on the specific time
final int _id = (int) System.currentTimeMillis(); // get calendar instance
//Use Alarm manager and Pending Intent
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
PendingIntent alarmIntent = PendingIntent.getBroadcast(context, mAlarmId, intent, 0);
alarmManager.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), alarmIntent);
}
And to cancel any Alarm call alarmManager.cancel(PendingIntent) like;
Intent intent = new Intent(this,AlarmReceiver.class);
PendingIntent alarmIntent = PendingIntent.getBroadcast(context, mAlarmId, intent, 0);
alarmManager.cancel(alarmIntent);

Alarm notification not working using data from sqlite database

I need to make a notification where in the user will choose the date and time the notification will start. In order for that, I store the date, time and notes in the Database. Now I want to start the alarm using the data from the database and using the id of each alarm as a unique id for the PendingIntent.
This is the how I get the data from the database
db=new events_database(events_main.this);
c=db.queryData("select * from events_list ORDER BY event_ID ASC");
if(c != null && c.moveToNext())
{
int id=c.getInt(0);
String type=c.getString(1);
String date=c.getString(2);
String time=c.getString(3);
String note=c.getString(4);
String[] newDate=date.split("/");
int year=Integer.valueOf(newDate[0]);
int month=Integer.valueOf(newDate[1]);
int day=Integer.valueOf(newDate[2]);
String[] newTime=time.split(":");
int hour=Integer.valueOf(newTime[0]);
int min=Integer.valueOf(newTime[1]);
Alarms(id,year,month,day,hour,min,type,note);
}
After getting it I pass the data into the Alarms method
public void Alarms(int id, int year, int month, int day, int hour, int minute, String type, String note) {
Calendar calendar1 = Calendar.getInstance();
calendar1.set(Calendar.YEAR,year);
calendar1.set(Calendar.MONTH,month);
calendar1.set(Calendar.DAY_OF_MONTH,day);
calendar1.set(Calendar.HOUR_OF_DAY, hour);
calendar1.set(Calendar.MINUTE, minute);
calendar1.set(Calendar.MILLISECOND, 0);
if (calendar1.before(Calendar.getInstance())) {
calendar1.add(Calendar.DATE, 1);
}
Intent intent = new Intent(events_main.this, events_receiver.class);
intent.putExtra("type",type);
intent.putExtra("note",note);
intent.putExtra("id", id);
PendingIntent dog1 = PendingIntent.getBroadcast(this, id, intent, PendingIntent.FLAG_UPDATE_CURRENT);
final AlarmManager alarm1 = (AlarmManager) getSystemService(ALARM_SERVICE);
alarm1.setExact(AlarmManager.RTC_WAKEUP, calendar1.getTimeInMillis(), dog1);
Toast.makeText(events_main.this, "alarms",Toast.LENGTH_SHORT).show();
}
then in the Receiver
public class events_receiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
int id= Integer.valueOf(intent.getStringExtra("id"));
String note=intent.getStringExtra("note");
String type=intent.getStringExtra("type");
NotificationManager notificationManager2=(NotificationManager)context.getSystemService(context.NOTIFICATION_SERVICE);
Intent dog_intent2=new Intent(context,HomeActivity.class);
dog_intent2.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent dog_pending2=PendingIntent.getActivity(context, id,dog_intent2, PendingIntent.FLAG_ONE_SHOT);;
NotificationCompat.Builder builder2=new NotificationCompat.Builder(context)
.setContentIntent(dog_pending2)
.setSmallIcon(R.drawable.ic_action_prof_breed_icon)
.setSound(Uri.parse("android.resource://" +context.getPackageName()+ "/" + R.raw.dog))
.setContentTitle("Pet Guide 101")
.setContentText(type)
.setContentText(note)
.setVibrate(new long[] { 1000, 1000})
.setAutoCancel(true);
notificationManager2.notify(id,builder2.build());
}
}
But for some reason the alarm doesn't get triggered. When I test the code I do get the data from the database but the alarm still doesn't get executed. If you can look it up, please tell me if I'm doing it wrong especially the part of passing the unique id into the receiver.

Scheduling multiple future notifications

I am currently debugging an issue with notifications inside my application. For some context, what I'd like to do is schedule notifications that should popup whenever a rocket launch is occurring. What I was doing was, after getting a list of scheduled launches from an API, I would take the launch date (in milliseconds since Jan 1 1970) and subtract the System.currentTimeMillis() from it. I would then use the resulting time to schedule the notification in the future, represented as System.currentTimeMillis() + timeDifference. I noticed that for whatever reason, only 1 notification is ever displayed.
I've tried debugging by scheduling notifications at 2, 4, and 6 minutes in the future, however a notification is only displayed at the 6 minute mark.
Some relevant code is below:
public void scheduleNotifications(List<Launch> launches) {
for(int i = 0; i < launches.size(); i++) {
SimpleDateFormat format = new SimpleDateFormat("MMMM dd, yyyy HH:mm:ss z");
Date date = null;
try {
date = format.parse(launches.get(i).getWindowstart());
} catch (ParseException e) {
e.printStackTrace();
}
long timeBetween = date.getTime() - System.currentTimeMillis();
Integer id = Long.valueOf(date.getTime()).intValue();
Intent notificationIntent = new Intent(this, NotificationPublisher.class);
notificationIntent.putExtra(NotificationPublisher.NOTIFICATION_ID, id);
notificationIntent.putExtra(NotificationPublisher.NOTIFICATION, getNotification(launches.get(i).getRocket().getName(), launches.get(i).getLocation().getPads().get(0).getName()));
PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, notificationIntent, 0);
AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
//Debug. Schedule at 2, 4, 6 minutes.
if (i == 0) {
alarmManager.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + 120000, pendingIntent);
}
if (i == 1) {
alarmManager.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + 240000, pendingIntent);
}
if (i == 2) {
alarmManager.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + 360000, pendingIntent);
}
}
}
private Notification getNotification(String rocketName, String padName) {
Notification.Builder builder = new Notification.Builder(this);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, new Intent(this, MainActivity.class), 0);
builder.setContentIntent(pendingIntent);
builder.setContentTitle("Upcoming Launch");
builder.setContentText("A launch of a " + rocketName + " is about to occur at " + padName + ". Click for more info.");
builder.setSmallIcon(R.drawable.rocket_icon);
return builder.build();
}
Broadcast Receiver:
public class NotificationPublisher extends BroadcastReceiver {
public static String NOTIFICATION_ID = "notification_id";
public static String NOTIFICATION = "notification";
public void onReceive(Context context, Intent intent) {
NotificationManager notificationManager = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
Notification notification = intent.getParcelableExtra(NOTIFICATION);
int id = intent.getIntExtra(NOTIFICATION_ID, 0);
notificationManager.notify(id, notification);
}
}
I'd like to know why only a single notification is ever presented, as well as what I need to add to achieve the previously stated goal.
When you set an alarm using AlarmManager, it automatically cancels any existing alarm that has a matching PendingIntent. Since all your PendingIntents contain the same components, every time you set an alarm, the previously set ones are automatically cancelled.
If you want to set multiple alarms, you must make sure that each of the PendingIntents is unique. You can do this in one of the following ways:
Use a different requestCode (second parameter to PendingIntent.getBroadcast()) for each PendingIntent
Use a different ACTION in the Intent you pass to PendingIntent.getBroadcast() for each PendingIntent

Use calendar to set alarm notification android

I am trying to get a notification to pop up based on the user date and time they put in. Here is my code for getting the time values
// String GetRawDate Gets The User Value For Date//
String getRawDate = date.getText().toString();
// String SplitDate Splits The Date Into Three Separate Ints//
String[] splitDate = getRawDate.split("/");
// Int GetMonth Gets The Value Of The Month//
int getMonth = Integer.parseInt(splitDate[0]);
// Int GetDay Gets The Value Of The Day//
int getDay = Integer.parseInt(splitDate[1]);
// Int GetYear Gets The Value Of The Year//
int getYear = Integer.parseInt(splitDate[2]);
// Get Military Start Time//
String test = military_start_time;
// Split It//
String[] splitStartTime = test.split(":");
// Get Hour In Integer Form
int getHour = Integer.parseInt(splitStartTime[0]);
// Get Minute In Integer Form//
int getMinute = Integer.parseInt(splitStartTime[1]);
From here I add these values to calendar
// Gets Calendar Instance//
Calendar calendar = Calendar.getInstance();
Calendar cal = Calendar.getInstance();
cal.set(Calendar.MONTH, getMonth);
cal.set(Calendar.YEAR, getYear);
cal.set(Calendar.DAY_OF_MONTH, getDay);
cal.set(Calendar.HOUR_OF_DAY, getHour);
cal.set(Calendar.MINUTE, getMinute);
Then I set my alarm
// Intent To Start Notification After X Seconds//
Intent alertIntent = new Intent(this, ReminderService.class);
alertIntent.putExtra("name", name.getText().toString());
alertIntent.putExtra("time", starttime.getText().toString());
// Defines Alarm Manager//
AlarmManager alarmManager = (AlarmManager)
getSystemService(Context.ALARM_SERVICE);
// Sets Alarm Manager//
alarmManager.set(AlarmManager.RTC_WAKEUP,
calendar.getTimeInMillis(), PendingIntent.getBroadcast(this, 1,
alertIntent, PendingIntent.FLAG_UPDATE_CURRENT));
// Starts Activity ListView//
Intent b = new Intent(this, Reminders.class);
startActivity(b);
overridePendingTransition(R.anim.slid_in, R.anim.slid_out);
Say the user has the date of 6/4/15 and the Time 22:10 I want the notification to show up on this time. For some reason it shows up about 5 seconds after the code is run through. Anybody know what I am doing wrong with the alarm?
try using
alarmManager.setExact(AlarmManager.RTC_WAKEUP,
cal.getTimeInMillis(), PendingIntent.getBroadcast(this, 1,
alertIntent, PendingIntent.FLAG_UPDATE_CURRENT));
P.S: Needs min API:19

multiple status bar notification, unique ID not working

I have an app that sends notifications to the status bar as a reminder to take a pill. If two pills need to be taken at the same time. then two separate notifications shall be sent. I know you need to use an unique id for this to happen in the .notify(int id, notification notification) function. However it doesn't seem to be working. I know for sure my ids are different as I've tested them by displaying a toast. I would appreciate any suggestions. Heres my code:
Alarm class:
DatabaseHelper notiDb = new DatabaseHelper(this);
notiDb.open();
final String dataName = notiDb.getDataName(pushed_name);
String dataDaily = notiDb.getDataDaily(pushed_name);
String dataWeekly = notiDb.getDataWeekly(pushed_name);
String dataTwice = notiDb.getDataTwice(pushed_name);
String dataDosage = notiDb.getDataDosage(pushed_name);
String dataStart = notiDb.getDataStart(pushed_name);
String dataID = notiDb.getDataID(pushed_name);
notiDb.close();
ID = Integer.parseInt(dataID);
Calendar uncalendar = Calendar.getInstance();
String unID = dataID +uncalendar;
Toast toast=Toast.makeText(this, "Alarm class, Notification for " +dataName +" has been set id: " +ID, Toast.LENGTH_LONG);
toast.show();
Intent intent_unique = new Intent(this, NotiScenario.class);
intent_unique.putExtra("ID", ID);
intent_unique.setData(Uri.parse(intent_unique.toUri(Intent.URI_INTENT_SCHEME)));
PendingIntent pIntent = PendingIntent.getActivity(this, ID, intent_unique, 2);
// Build notification
Notification noti = new Notification.Builder(this)
.setContentTitle("MedScan")
.setContentText("3. You should take "+dataDosage +" pills of " +dataName)
.setSmallIcon(R.drawable.original)
.setContentIntent(pIntent)
.build();
NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
noti.defaults |= Notification.DEFAULT_ALL;
//Hide the notification after its selected
noti.flags |= Notification.FLAG_AUTO_CANCEL;
notificationManager.notify(ID, noti);
Info class:
DatabaseHelper notiDb = new DatabaseHelper(this);
notiDb.open();
final String dataName = notiDb.getDataName(pushed_name);
String dataDaily = notiDb.getDataDaily(pushed_name);
final String dataWeekly = notiDb.getDataWeekly(pushed_name);
String dataTwice = notiDb.getDataTwice(pushed_name);
final String dataDosage = notiDb.getDataDosage(pushed_name);
String dataStart = notiDb.getDataStart(pushed_name);
String dataID = notiDb.getDataID(pushed_name);
notiDb.close();
ID = Integer.parseInt(dataID);
int value_Weekly = Integer.parseInt(dataWeekly);
int value_Daily = Integer.parseInt(dataDaily);
int value_Twice = Integer.parseInt(dataTwice);
int value_Start = Integer.parseInt(dataStart);
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY, value_Start);
calendar.set(Calendar.MINUTE, 46);
calendar.set(Calendar.SECOND, 00);
int setTime = (value_Daily*value_Twice)/value_Weekly;
Intent intent_noti = new Intent(this,Alarm.class);
intent_noti.putExtra("ID", ID);
intent_noti.setData(Uri.parse(intent_noti.toUri(Intent.URI_INTENT_SCHEME)));
PendingIntent pendingIntent = PendingIntent.getActivity(this, ID, intent_noti, PendingIntent.FLAG_ONE_SHOT);
AlarmManager am = (AlarmManager)getSystemService(Activity.ALARM_SERVICE);
am.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), setTime, pendingIntent);
The problem isn't in your Alarm class which creates the notification but in your Info class which creates an alarm to call the Info class on a regular basis.
The two lines creating the pending intent for your alarm are:
Intent intent_noti = new Intent(this,Alarm.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this, ID, intent_noti, PendingIntent.FLAG_CANCEL_CURRENT);
Because the Intent is the same regardless of your ID, a newly created PendingIntent will just cancel the previously created one.
In order for your code to work the Intent has to be different according to the Intent.filterEquals() method: http://developer.android.com/reference/android/content/Intent.html#filterEquals(android.content.Intent)
To get a unique Intent you could do:
intent_noti.setAction(""+System.currentTimeMillis());
or:
intent_noti.putExtra("ID", ID);
intent_noti.setData(Uri.parse(intent_noti.toUri(Intent.URI_INTENT_SCHEME)));

Categories

Resources