multiple status bar notification, unique ID not working - android

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)));

Related

Notification Message Instantly Fired - 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);

Show multiple notifications per day in android using Alarm manager

I want to show 2 notifications per day in my application in 2 specific time, until now i'm just able to show one notification.
This is my code, how can i show multiple notification.
one at 7 AM , and the other at 6 PM for example?
Intent myIntent = new Intent(Calender.this, MyAlarmService.class);
int id = (int) System.currentTimeMillis();
pendingIntent = PendingIntent.getService(Calender.this, id,
myIntent, Notification.FLAG_ONLY_ALERT_ONCE);
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
Calendar timeToSet = Calendar.getInstance();
timeToSet.set(Calendar.HOUR_OF_DAY, hour);
alarmManager.set(AlarmManager.RTC_WAKEUP,
timeToSet.getTimeInMillis(), pendingIntent);
and i called this in MyAlarmService in the onStart method
final Calendar c = Calendar.getInstance();
Notification note = new Notification(R.drawable.icon,
getString(R.string.app_name), System.currentTimeMillis());
Intent intent = new Intent(this, Calender.class);
PendingIntent i = PendingIntent.getActivity(this, 0, intent,
Notification.FLAG_ONGOING_EVENT);
note.setLatestEventInfo(this, getString(R.string.app_name),
"Some String", i);
note.flags |= Notification.FLAG_AUTO_CANCEL;
NOTIFY_ME_ID = System.currentTimeMillis();
mgr.notify((int) NOTIFY_ME_ID, note);
You should set different keys for different notifications. If you use one key for several notifications it will rewrite the same one.

how to display two separate notifications at same time?

My app needs to display notifications in the status bar whenever it is time to take some pills. If two pills are to be taken at the same time, then two notifications shall appear. Right now my app can display separate notifications if the pill times are different. The problem is if they are at the same time it only displays the one that was created (when I press a button) most recently. However it still vibrates twice. Any suggestions?
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, 00);
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);
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);
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, 0);
// 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);
Is the id of the notification the same? If that's true I think that causes an override of the notification. Use different id's if you to show multiple notifications alongside each other.

android alarm sound not working

I have created an app where a user can insert various subjects and their day of exam.my app provides a time span for each subjects stored in database. when time allocated for each subject reaches,an alarm should play along with a notification.notification is coming properly,but alarm sound is not playing.please help me.
My alarm manager class is :
public class AlarmManager extends BroadcastReceiver {
sampleDatabase appdb;
SQLiteDatabase sqldb;
Cursor cursor;
int today,prev;
Intent in;
#Override
public void onReceive(Context context, Intent intent)
{
NotificationManager manger = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
Notification notification = new Notification(R.drawable.icon , "Yahrzeit" , System.currentTimeMillis());
in = new Intent(context,FirstAppActivity.class);
in.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent contentIntent = PendingIntent.getActivity(context, 0, in, 0);
notification.setLatestEventInfo(context, "ALERT!!!!", "Time Over" , contentIntent);
notification.flags = Notification.FLAG_INSISTENT;
appdb = new sampleDatabase(context);
sqldb = appdb.getReadableDatabase();
cursor = sqldb.query(sampleDatabase.TABLE_SEC, null, null, null, null, null, null);
cursor.moveToFirst();
Calendar cal = Calendar.getInstance();
cal.set(Calendar.SECOND, 0);
today = (int)(cal.getTimeInMillis()/1000);
Log.e("Naga","Alarm");
Log.e("today",Integer.toString(today));
prev = 0;
cursor.moveToPrevious();
while(cursor.moveToNext())
{
int dbdate = cursor.getInt(cursor.getColumnIndex(sampleDatabase.ALARMSET));
int id = cursor.getInt(cursor.getColumnIndex(sampleDatabase.ROW_ID));
Log.e("dbdate",Integer.toString(dbdate));
if((dbdate<=today)&&(dbdate>prev))
{
manger.cancelAll();
manger.notify(1, notification);
sqldb.execSQL("DELETE from " + sampleDatabase.TABLE + " where " + sampleDatabase.ROW_ID + "=" + id);
sqldb.execSQL("DELETE from "+sampleDatabase.TABLE_SEC + " where " + sampleDatabase.ROW_ID + "=" + id);
}
prev = dbdate;
}
cursor.close();
sqldb.close();
}
code which i used to call this alarm manager is like this:
alarmintent = new Intent(getApplicationContext(),com.nagainfo.firstAp.AlarmManager.class);
sender = PendingIntent.getBroadcast(getApplicationContext() , 0 , alarmintent , PendingIntent.FLAG_CANCEL_CURRENT | Intent.FILL_IN_DATA);
am = (AlarmManager) getSystemService(ALARM_SERVICE);
am.cancel(sender);
alarmintent = new Intent(getApplicationContext(), com.nagainfo.firstAp.AlarmManager.class);
//alarmintent.putExtra("note","Hebrew");
sender = PendingIntent.getBroadcast(getApplicationContext() , 0 , alarmintent , PendingIntent.FLAG_CANCEL_CURRENT | Intent.FILL_IN_DATA);
am = (AlarmManager) getSystemService(ALARM_SERVICE);
am.setRepeating(AlarmManager.RTC_WAKEUP, alarmtime, 60000, sender);
You have not specified to use sound. Use this :
notification.defaults=Notification.DEFAULT_SOUND|Notification.DEFAULT_VIBRATE;
If you do not want the vibrate remove it. The point is you have to bit wise OR them. So, if you want lights also -
notification.defaults=Notification.DEFAULT_SOUND|Notification.DEFAULT_VIBRATE|Notification.DEFAULT_LIGHTS;
If someone are using NotificationCompat.Builder, you can use:
Uri sound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
notifyBuilder.setSound(sound);

How to make notification on android schedule by date, month, hour and minute?

I have an application with notification, I want to schedule once every year.. Below my code:
private void checkAlarm() {
Calendar c = Calendar.getInstance();
int date = c.get(Calendar.DATE);
int month = c.get(Calendar.MONTH);
int year = c.get(Calendar.YEAR);
int hour = c.get(Calendar.HOUR_OF_DAY);
int minute = c.get(Calendar.MINUTE);
if (date==17 && month==8 && hour==6 && minute==0){
showNotification(R.drawable.sphere_notification, "hari ini 17 Agustus");
}
}
private void showNotification(int icon, String text) {
Notification notification = new Notification(icon, "Hari ini memperingati Hari Kemerdekaan RI", System.currentTimeMillis());
notification.flags = Notification.FLAG_AUTO_CANCEL;
notification.sound = Uri.parse("android.resource://app.vmh.main/"+R.raw.agustus);
PendingIntent facebook = PendingIntent.getActivity(this, 0,
new Intent(this, ShareOnFacebook.class),0);
PendingIntent twitter = PendingIntent.getActivity(this, 0,
new Intent(this, ShareOnTwitter.class),0);
Intent exit = new Intent (Intent.ACTION_MAIN);
exit.addCategory(Intent.CATEGORY_HOME);
PendingIntent mainIntent = PendingIntent.getActivity(this, 0,
exit,0);
if (Pengaturan.getSetFacebook(this)){
notification.setLatestEventInfo(this, "Kemerdekaan - Remainder",
text, facebook);
}else if((Pengaturan.getSetFacebook(this)) && (Pengaturan.getSetTwitter(this))){
notification.setLatestEventInfo(this, "Kemerdekaan - Remainder",
text, facebook);
}else if(Pengaturan.getSetTwitter(this)){
notification.setLatestEventInfo(this, "Kemerdekaan - Remainder",
text, twitter);
}else
notification.setLatestEventInfo(this, "Kemerdekaan - Remainder",
text, mainIntent);
// Send the notification.
mNM.notify(1, notification);
}
I have problem when I implement date and month, notification not working. If I use hour and minute my app is working.
Can u help me?
You can use AlarmManager to schedule your task.

Categories

Resources